I am having problem with DynamoDB. I dont have any idea on how to get all items in table (in this case, DynamoDBTable) of DynamoDB and display it. I tried to use Scan but it gives me error of null exception.
ScanTable
public void ScanTable() throws Exception {
DynamoDBMapper mapper = new DynamoDBMapper(client);
System.out.println("Scanning Tesis");
DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
List<DynamoDBTable> result = mapper.scan(DynamoDBTable.class, scanExpression);
System.out.println(result.toString());
for (DynamoDBTest tesis : result) {
System.out.println(tesis.toString());
}
DynamoDBTable
@DynamoDBTable(tableName = "Tesis")
public class DynamoDBTest {
private String Title;
private String id;
private String Author;
private String Year;
private String Supervisor;
private String Program;
@DynamoDBHashKey(attributeName = "Id")
@DynamoDBAutoGeneratedKey
public String getId ()
{
return id;
}
public void setId (String id)
{
this.id = id;
}
@DynamoDBAttribute(attributeName = "Title")
public String getTitle()
{
return Title;
}
public void setTitle (String Title)
{
this.Title = Title;
}
@DynamoDBAttribute(attributeName = "Author")
public String getAuthor ()
{
return Author;
}
public void setAuthor (String Author)
{
this.Author = Author;
}
@DynamoDBAttribute(attributeName = "Year")
public String getYear ()
{
return Year;
}
public void setYear (String Year)
{
this.Year = Year;
}
@DynamoDBAttribute(attributeName = "Supervisor")
public String getSupervisor ()
{
return Supervisor;
}
public void setSupervisor (String Supervisor)
{
this.Supervisor = Supervisor;
}
@DynamoDBAttribute(attributeName = "Program")
public String getProgram ()
{
return Program;
}
public void setProgram (String Program)
{
this.Program = Program;
}
}
6-28 21:35:47.816 10939-10939/com.example.user.test2 I/System.out﹕ Scanning Tesis
06-28 21:35:47.826 10939-10939/com.example.user.test2 W/System.err﹕ java.lang.NullPointerException
06-28 21:35:47.826 10939-10939/com.example.user.test2 W/System.err﹕ at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.scan(DynamoDBMapper.java:2007)
06-28 21:35:47.826 10939-10939/com.example.user.test2 W/System.err﹕ at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.scan(DynamoDBMapper.java:1971)
06-28 21:35:47.826 10939-10939/com.example.user.test2 W/System.err﹕ at com.example.user.test2.DynamoGetItem.ScanTable(DynamoGetItem.java:53)
06-28 21:35:47.826 10939-10939/com.example.user.test2 W/System.err﹕ at com.example.user.test2.DynamoGetItem.onCreate(DynamoGetItem.java:30)
06-28 21:35:47.826 10939-10939/com.example.user.test2 W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5411)
06-28 21:35:47.826 10939-10939/com.example.user.test2 W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-28 21:35:47.826 10939-10939/com.example.user.test2 W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
06-28 21:35:47.826 10939-10939/com.example.user.test2 W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
06-28 21:35:47.826 10939-10939/com.example.user.test2 W/System.err﹕ at android.app.ActivityThread.access$800(ActivityThread.java:139)
06-28 21:35:47.826 10939-10939/com.example.user.test2 W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
06-28 21:35:47.826 10939-10939/com.example.user.test2 W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
06-28 21:35:47.826 10939-10939/com.example.user.test2 W/System.err﹕ at android.os.Looper.loop(Looper.java:149)
06-28 21:35:47.826 10939-10939/com.example.user.test2 W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5257)
06-28 21:35:47.826 10939-10939/com.example.user.test2 W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
06-28 21:35:47.826 10939-10939/com.example.user.test2 W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
06-28 21:35:47.826 10939-10939/com.example.user.test2 W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-28 21:35:47.826 10939-10939/com.example.user.test2 W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
06-28 21:35:47.826 10939-10939/com.example.user.test2 W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
Is there anything I did wrong?
AmazonDynamoDB db is declared on this line of the most recent version of Android SDK. Your NPE is happening on the call to the scan method of db field. It appears that the AmazonDynamoDB field may be null and is not set. Did you create a DynamoDB client and inject it in the Mapper? In other words, is the client variable above set? Finally, it seems like the initial snippet of your call instantiation List<DynamoDBTable> result = mapper.scan(DynamoDBTable.class, scanExpression);
should use DynamoDBTest.class
instead of DynamoDBTable.class
.