I created a Data Access Object Class (dao) to get a data from database. When I try to get a List on an Activity I get a NullPointerException, but when I do a debug, it does not enter in the function.
The problem is exactly on that line of the activity at OnCreate, the detail is a TextView:
detail.setText(dao.getStatusList(package.getId()).get(0).getDetalhe());
The variable package was instantiated by the intent and other of his attributes were used normally.
package = (Package) getIntent().getSerializableExtra("package");
In debug appears exactly the value of the package Id is 1 (including I've tried to put getStatusList (1) to see if it at least entered the getStatusList function, but without sucesse). When I Step Into the function:
public long getId() {
return id;
}
It shows exactly the value 1 in debug, and the value is returned normally, so the value exists. But in time to enter the getStatusList function (long PackageID); The debug goes to a function:
public boolean onException(Object obj, Throwable e) {
return false;
}
On a Instrumentation.Java an not getStatusList() on dao:
public List<Status> getStatusList(long packageId) {
List<Status> statusList = new ArrayList<>();
String[] columns = new String[]{KEY_ID, KEY_DATA_TIME, KEY_LOCATION, KEY_ACTION, KEY_DETAIL, KEY_PACKAGE_ID};
Cursor cursor = db.query(TB_STATUS, columns, KEY_PACKAGE_ID + " = " + packageId, null, null, null, KEY_DATA_TIME + " DESC");
if (cursor.getCount() > 0) {
cursor.moveToFirst();
do {
Status status = new Status();
status.setId(cursor.getLong(0));
status.setSDateTime(cursor.getString(1));
status.setLocal(cursor.getString(2));
status.setAcao(cursor.getString(3));
status.setDetalhe(cursor.getString(4));
status.setPackageId(cursor.getLong(5));
statusList.add(status);
} while (cursor.moveToNext());
}
return statusList;
}
I do not understand, if the value exists, and it appears in debug, why am I getting NullPointerException?
Error log:
09-11 17:20:14.877 14866-14866/com.example.rafael.encomendasmaterial E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.rafael.encomendasmaterial, PID: 14866
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rafael.encomendasmaterial/com.example.rafael.encomendasmaterial.activities.PackageView}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.example.rafael.encomendasmaterial.util.db.DataAcessObject.getStatusList(long)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.example.rafael.encomendasmaterial.util.db.DataAcessObject.getStatusList(long)' on a null object reference
at com.example.rafael.encomendasmaterial.activities.PackageView.onCreate(PackageView.java:50)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)