我被困住了。我在buttonClick上收到错误NullPointerException
。我无法确定null的位置。我的代码是您的参考。我已经坚持了这个错误2周了。 :(
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 ERROR
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)
答案 0 :(得分:3)
您必须在setContentView
方法的onCreate
之后移动这些代码。
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();