-3

我被卡住了。我在buttonClick上得到了錯誤NullPointerException。我無法確定null是什麼。下面是我的代碼,供您參考。現在我已經連續兩週遇到這個錯誤。 :(Android拋出java.lang.NullPointerException

public class DynamoDB extends Activity{ 
private String tableName = "Tesis_FTSM"; 
private AmazonDynamoDBClient client; 

public void DynamoDB() throws IOException 
{ 
    AWSCredentials credentials = new PropertiesCredentials(DynamoDB.class.getResourceAsStream("Credentials.properties")); 
    client = new AmazonDynamoDBClient(credentials); 
    client.setRegion(Region.getRegion(Regions.US_WEST_2)); 
} 

public void putItem(String Title, String Author, String Year, String Supervisor, String Program) 
{ 
    Map<String, AttributeValue> item = new HashMap<>(); 

    item.put("Id", new AttributeValue().withN("1")); 
    item.put("Title", new AttributeValue().withS(Title)); 
    item.put("Author", new AttributeValue().withS(Author)); 
    item.put("Year", new AttributeValue().withS(Year)); 
    item.put("Supervisor", new AttributeValue().withS(Supervisor)); 
    item.put("Program", new AttributeValue().withS(Program)); 

    PutItemRequest itemRequest = new PutItemRequest().withTableName(
      tableName).withItem(item); 
    client.putItem(itemRequest); 
    item.clear(); 
} 

} 

MainActivity.java

public class MainActivity extends Activity 
{ 
    /////////////////////////////////////////////////////////////////////////////////////////////// 
    final EditText title = (EditText)findViewById(R.id.title_View); 
    final EditText author = (EditText)findViewById(R.id.author_View); 
    final EditText year = (EditText)findViewById(R.id.year_View); 
    final EditText supervisor = (EditText)findViewById(R.id.super_View); 
    final Spinner prog = (Spinner)findViewById(R.id.prog_Spinner); 

    final String tajuk = title.getText().toString(); 
    final String au = author.getText().toString(); 
    final String tahun = year.getText().toString(); 
    final String selia = supervisor.getText().toString(); 
    final String program = prog.getSelectedItem().toString(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button upload = (Button)findViewById(R.id.upload_Btn); 

     upload.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       try { 
        Toast.makeText(getApplicationContext(), "Save to Table", Toast.LENGTH_SHORT).show(); 
        Task obj = new Task(); 
        obj.execute(); 

       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 


     }); 


    } 

    public class Task extends AsyncTask<String, Void, String> { 



     @Override 
     protected String doInBackground(String... params) { 
      Looper.prepare(); 

      try { 

       DynamoDB db = new DynamoDB(); 

       db.putItem(tajuk,au,tahun,selia,program); 


      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      Looper.loop(); 
      return null; 

     } 

     protected void onPostExecute(String page) { 
      // onPostExecute 
     } 
    } 


} 

logcat的錯誤

06-24 12:53:25.321 23051-23079/com.example.user.test2 W/System.err﹕ java.lang.NullPointerException 
06-24 12:53:25.331 23051-23079/com.example.user.test2 W/System.err﹕ at com.example.user.test2.DynamoDB.putItem(DynamoDB.java:61) 
06-24 12:53:25.331 23051-23079/com.example.user.test2 W/System.err﹕ at com.example.user.test2.MainActivity$Task.doInBackground(MainActivity.java:66) 
06-24 12:53:25.331 23051-23079/com.example.user.test2 W/System.err﹕ at com.example.user.test2.MainActivity$Task.doInBackground(MainActivity.java:44) 
06-24 12:53:25.331 23051-23079/com.example.user.test2 W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288) 
06-24 12:53:25.331 23051-23079/com.example.user.test2 W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
06-24 12:53:25.331 23051-23079/com.example.user.test2 W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
06-24 12:53:25.331 23051-23079/com.example.user.test2 W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
06-24 12:53:25.331 23051-23079/com.example.user.test2 W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
06-24 12:53:25.331 23051-23079/com.example.user.test2 W/System.err﹕ at java.lang.Thread.run(Thread.java:841) 
+1

你什麼在Dyn有第61行amoDB.java – SathishKumar

+1

在您的數據庫代碼中提供您的DynamoDB類 –

+0

pr – Mano

回答

3

你必須在onCreate方法你setContentView後,將這些代碼。

final EditText title = (EditText)findViewById(R.id.title_View); 
final EditText author = (EditText)findViewById(R.id.author_View); 
final EditText year = (EditText)findViewById(R.id.year_View); 
final EditText supervisor = (EditText)findViewById(R.id.super_View); 
final Spinner prog = (Spinner)findViewById(R.id.prog_Spinner); 

final String tajuk = title.getText().toString(); 
final String au = author.getText().toString(); 
final String tahun = year.getText().toString(); 
final String selia = supervisor.getText().toString(); 
final String program = prog.getSelectedItem().toString(); 
+0

也不要保留變量local 。在你的活動中創建它們,然後在創建中找到它們。 – user765

相關問題