Reputation: 41
My next Problem.
I wanna display the data in a listview and i get a Nullpointerexception. My Datasource is a SQLite Database. In the following Text is the Activity and the LogCat.
Please help me!
public class ListHaltestelleActivity extends Activity {
private SimpleAdapter simpleAdapter;
private SQLiteDatabase db;
protected static final String TAG = "DataAdapter";
private SQLiteDatabase mDb;
DatabaseHelper myDbHelper = new DatabaseHelper(this);
//put the table name and column in constants
public static final String COLUMN_NAME = "scotty_haltestelle";
Intent intent;
.
.
.
.
public void setListView()
{
try
{
myDbHelper.getReadableDatabase();
// Alle Daten der Datenbank abrufen mithilfe eines Cursors
Cursor cursor = db.rawQuery("Select name from scotty_haltestelle",null);
cursor.moveToFirst();
ListView listview = (ListView)findViewById(R.id.listView);
List<Map<String, String>> haltestellenList = new ArrayList<Map<String, String>>();
while (cursor.moveToNext()){
//in this string we get the record for each row from the column "name"
String name = cursor.getString(0);
String outPut = name;
haltestellenList.add(createHaltestelle("haltestelle", outPut));
}
simpleAdapter = new SimpleAdapter(ListHaltestelleActivity.this, haltestellenList,
android.R.layout.simple_list_item_1,
new String[] { "haltestelle" }, new int[] { android.R.id.text1 });
listview.setAdapter(simpleAdapter);
//here we close the cursor because we do not longer need it
//}
cursor.close();
}
catch (SQLException mSQLException)
{
Log.e(TAG, "getTestData >>"+ mSQLException.toString());
throw mSQLException;
}
}
// Hash - Map in der die Haltestelle mit Name und dann Number gespeichert wird
private HashMap<String, String> createHaltestelle(String name, String number) {
HashMap<String, String> haltestelleNameNo = new HashMap<String, String>();
haltestelleNameNo.put(name, number);
return haltestelleNameNo;
Here is the logcat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{at.atn.android/at.atn.android.ListHaltestelleActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.access$700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at at.atn.android.ListHaltestelleActivity.setListView(ListHaltestelleActivity.java:76)
at at.atn.android.ListHaltestelleActivity.onCreate(ListHaltestelleActivity.java:61)
at android.app.Activity.performCreate(Activity.java:5243)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
Thanks for your Help
Upvotes: 0
Views: 70
Reputation: 10262
Instead of
myDbHelper.getReadableDatabase();
do
db=myDbHelper.getReadableDatabase();
Upvotes: 1
Reputation: 152827
You forgot to assign a value to the db
member variable, throwing out the result of myDbHelper.getReadableDatabase()
. This causes the NPE as db
is not initialized.
Later in the code you will skip the first row as moveToFirst()
is followed by moveToNext()
.
Upvotes: 1