Reputation: 4885
Query:
public List<Pontos> selectSpecific(String id) {
List<Pontos> list = new ArrayList<Pontos>();
Cursor cursor = this.db.query(TABLE_NAME, new String[] { "id", "name" },
"id"+" = ?", new String[] { id }, null, null, null);
if (cursor == null) {
return list;
}
if (cursor.moveToFirst()) {
do {
Pontos p = new Pontos();
p.id = cursor.getLong(0);
p.name = cursor.getString(1);
list.add(p);
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
return list;
}
Fetching the result:
public class Showplace extends Activity {
private String id;
DataHelper dh = new DataHelper(Showplace.this);
TextView txtTitle = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showplace);
Bundle extras = getIntent().getExtras();
id = Integer.toString(extras.getInt("id"));
txtTitle = (TextView) findViewById(R.id.txtTitle);
List<Pontos> list = this.dh.selectSpecific(id);
for(Pontos p : list){
txtTitle.setText(p.getName().toString());
}
}
}
It's giving me an NPE, I think that's in this line:
Cursor cursor = this.db.query(TABLE_NAME, new String[] { "id", "name" },
"id"+" = ?", new String[] { id }, null, null, null);
I tried various ways of doing this, searched a lot over StackOverflow and I can't seem to find a solution to this.
Edit:
LogCat:
03-02 10:22:09.244: E/AndroidRuntime(341): Uncaught handler: thread main exiting due to uncaught exception
03-02 10:22:09.304: E/AndroidRuntime(341): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dotpro.android.bikevento/com.dotpro.android.bikevento.Showplace}: java.lang.NullPointerException
03-02 10:22:09.304: E/AndroidRuntime(341): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2324)
03-02 10:22:09.304: E/AndroidRuntime(341): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
03-02 10:22:09.304: E/AndroidRuntime(341): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
03-02 10:22:09.304: E/AndroidRuntime(341): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
03-02 10:22:09.304: E/AndroidRuntime(341): at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 10:22:09.304: E/AndroidRuntime(341): at android.os.Looper.loop(Looper.java:123)
03-02 10:22:09.304: E/AndroidRuntime(341): at android.app.ActivityThread.main(ActivityThread.java:4203)
03-02 10:22:09.304: E/AndroidRuntime(341): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 10:22:09.304: E/AndroidRuntime(341): at java.lang.reflect.Method.invoke(Method.java:521)
03-02 10:22:09.304: E/AndroidRuntime(341): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-02 10:22:09.304: E/AndroidRuntime(341): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
03-02 10:22:09.304: E/AndroidRuntime(341): at dalvik.system.NativeStart.main(Native Method)
03-02 10:22:09.304: E/AndroidRuntime(341): Caused by: java.lang.NullPointerException
03-02 10:22:09.304: E/AndroidRuntime(341): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:193)
03-02 10:22:09.304: E/AndroidRuntime(341): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
03-02 10:22:09.304: E/AndroidRuntime(341): at com.dotpro.android.bikevento.DataHelper.<init>(DataHelper.java:28)
03-02 10:22:09.304: E/AndroidRuntime(341): at com.dotpro.android.bikevento.Showplace.<init>(Showplace.java:14)
03-02 10:22:09.304: E/AndroidRuntime(341): at java.lang.Class.newInstanceImpl(Native Method)
03-02 10:22:09.304: E/AndroidRuntime(341): at java.lang.Class.newInstance(Class.java:1472)
03-02 10:22:09.304: E/AndroidRuntime(341): at android.app.Instrumentation.newActivity(Instrumentation.java:1097)
03-02 10:22:09.304: E/AndroidRuntime(341): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
03-02 10:22:09.304: E/AndroidRuntime(341): ... 11 more
Upvotes: 2
Views: 340
Reputation: 6797
this line is wrong:
DataHelper dh = new DataHelper(Showplace.this);
you should move dh = new DataHelper(Showplace.this);
to onCreate method
why? because if you initialize field dh as you do Activity is not fully created and the context is not valid
Upvotes: 3