Aprian
Aprian

Reputation: 1728

Logcat got Null Pointer Exception?

i got some problems with my history listview. on logcat it stated the cause of the problem is new_item.

this is my code

c.mDbHelper.fetchAllNotes();
    Item new_item;    
for (i=0; i<c.getCount(); i++) {

    new_item.set(c.getString(1), c.getString(2), c.getInt(4), Uri.parse(c.getString(5)));
    item.add(new_item);
    c.moveToNext();
}

Item is a class I made. this is the set() method.

public void set(String name, String code, int qty, Uri imageuri){
    this.name = name;
    this.code = code;
    this.qty = qty;
    this.imageuri = imageuri;
}

this is my logcat

06-28 09:12:49.528: E/AndroidRuntime(818): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.avs.QRscanner/com.android.avs.QRscanner.ViewStock}: java.lang.NullPointerException
06-28 09:12:49.528: E/AndroidRuntime(818):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-28 09:12:49.528: E/AndroidRuntime(818):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-28 09:12:49.528: E/AndroidRuntime(818):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-28 09:12:49.528: E/AndroidRuntime(818):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-28 09:12:49.528: E/AndroidRuntime(818):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 09:12:49.528: E/AndroidRuntime(818):  at android.os.Looper.loop(Looper.java:123)
06-28 09:12:49.528: E/AndroidRuntime(818):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-28 09:12:49.528: E/AndroidRuntime(818):  at java.lang.reflect.Method.invokeNative(Native Method)
06-28 09:12:49.528: E/AndroidRuntime(818):  at java.lang.reflect.Method.invoke(Method.java:507)
06-28 09:12:49.528: E/AndroidRuntime(818):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-28 09:12:49.528: E/AndroidRuntime(818):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-28 09:12:49.528: E/AndroidRuntime(818):  at dalvik.system.NativeStart.main(Native Method)
06-28 09:12:49.528: E/AndroidRuntime(818): Caused by: java.lang.NullPointerException
06-28 09:12:49.528: E/AndroidRuntime(818):  at     com.android.avs.QRscanner.CustomAdapter.getCount(CustomAdapter.java:24)
06-28 09:12:49.528: E/AndroidRuntime(818):  at android.widget.ListView.setAdapter(ListView.java:454)
06-28 09:12:49.528: E/AndroidRuntime(818):  at com.android.avs.QRscanner.ViewStock.onCreate(ViewStock.java:36)
06-28 09:12:49.528: E/AndroidRuntime(818):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-28 09:12:49.528: E/AndroidRuntime(818):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-28 09:12:49.528: E/AndroidRuntime(818):  ... 11 more

DbHelper has 5 columns, which take string, string, string, int, string(for storing Uri)

thanks in advance for the help.

edit : add new_item to List item

Upvotes: 0

Views: 464

Answers (2)

Alex Lockwood
Alex Lockwood

Reputation: 83303

You should use a CursorAdapter to display the contents of your Cursor, and then bind it to your ListView with setListAdapter.

Upvotes: 1

JScoobyCed
JScoobyCed

Reputation: 10413

Do you need to initialize new_item, i.e. (line 3 below)

c.mDbHelper.fetchAllNotes();
Item new_item;  // <-- Add this (thx jack57)
for (i=0; i<c.getCount(); i++) {
 new_item = new Item();    // <-- Initialize here to prevent NullPointerException
 new_item.set(c.getString(1), c.getString(2), c.getInt(4), Uri.parse(c.getString(5)));
 c.moveToNext();
}

And you might want to use the Item object somewhere. Here it seems it is lost after the loop go to next i value.

Upvotes: 1

Related Questions