0

I am at a point, where I need some help. I stuck to this problem for a couple of days now. (I am new to Android Programming)

So I am at my first app and want to create a ListView which is filled with entries out of a SQLite DB.

Unfortunately I got a NullPointerException when I try to set my Adapter.

I hope you guys have an idea, of how to fix this or a hint where i can look after a solution. For this code I went through a couple of Tutorials, so i might missed some important part or made a rather "silly" mistake, due I am a complete newbie.

Heres my Code:

Method, which populates the ListView:

//Populates the List View
public void readDB(View view){

    Log.d("ReadDB","Start");

    //Read DB
    Cursor myCursor = dbTimer.rawQuery("Select * from Times", null);
    myCursor.moveToFirst();

    //Outputs DB-Entries in Console (for checking if there are entries)
    printDB(myCursor);

    //Find ListView to populate
    lvItems = (ListView) findViewById(R.id.listView_timer);

    //Setup Cursor Adapter with the ResultSet
    TimerCursorAdapter myAdapter = new TimerCursorAdapter(this, myCursor);

    //Attach Cursor adapter to the ListView
    lvItems.setAdapter(myAdapter);
    Log.d("ReadDB","Finish");

}

Adapter Class

public class TimerCursorAdapter extends CursorAdapter {

public TimerCursorAdapter(Context context, Cursor cursor){
    super(context, cursor, 0);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent){
    return LayoutInflater.from(context).inflate(R.layout.item_list_view, parent, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor){

    // Find TextFields for pupolation
    TextView tvTitle = (TextView) view.findViewById(R.id.textView_title);
    TextView tvCup = (TextView) view.findViewById(R.id.textView_cup);
    TextView tvGram = (TextView) view.findViewById(R.id.textView_gram);
    TextView tvMilliliter = (TextView) view.findViewById(R.id.textView_milliliter);
    TextView tvBlooming = (TextView) view.findViewById(R.id.textView_blooming);
    TextView tvSteeping = (TextView) view.findViewById(R.id.textView_steeping);
    TextView tvPressing = (TextView) view.findViewById(R.id.textView_pressing);

    // Extract properties from Cursor
    String strTitle = cursor.getString(cursor.getColumnIndexOrThrow("Title"));
    String intCup = cursor.getString(cursor.getColumnIndexOrThrow("Cup"));
    String intGram = cursor.getString(cursor.getColumnIndexOrThrow("Gram"));
    String intMilliliter = cursor.getString(cursor.getColumnIndexOrThrow("Milliliter"));
    String intBlooming = cursor.getString(cursor.getColumnIndexOrThrow("Blooming"));
    String intSteeping = cursor.getString(cursor.getColumnIndexOrThrow("Steeping"));
    String intPressing = cursor.getString(cursor.getColumnIndexOrThrow("Pressing"));

    //Sets TextVies in the Layout
    tvTitle.setText(strTitle);
    tvCup.setText(intCup);
    tvGram.setText(intGram);
    tvMilliliter.setText(intMilliliter);
    tvBlooming.setText(intBlooming);
    tvSteeping.setText(intSteeping);
    tvPressing.setText(intPressing);
}

Error Code

10-06 12:03:16.064 862-862/clausing.sebastian.de.eztimer E/AndroidRuntime: FATAL EXCEPTION: main
Process: clausing.sebastian.de.eztimer, PID: 862
Theme: themes:{}
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:289)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21158)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5204) 
at android.view.View$PerformClick.run(View.java:21158) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5461) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at clausing.sebastian.de.eztimer.MainActivity.readDB(MainActivity.java:114)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) 
at android.view.View.performClick(View.java:5204) 
at android.view.View$PerformClick.run(View.java:21158) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5461) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
USER9561
  • 1,084
  • 3
  • 15
  • 41
Sebastian
  • 27
  • 2
  • did you try debug this line `lvItems = (ListView) findViewById(R.id.listView_timer);` ? it looks like `lvItems` has a null value – Aram Tchekrekjian Oct 06 '16 at 10:49
  • The id "listView_timer" and the corresponding ListView are located in the content_main xml which is included in the activity_main.xml. – Sebastian Oct 06 '16 at 10:53
  • lvItems is null so the view is not connected to the layout that contains R.id.listView_timer which should have been set in onCreate – k3b Oct 06 '16 at 12:24
  • It worked, i just put " lvItems = (ListView) findViewById(R.id.listView_timer); " in the onCreate Method and thats it. So simple. Can you explain to me why it did not work before. Why had the ID to be connected in the onCreate Method, should i do this with all IDs? Or give me a Link, where I can read it up :) Thanks for your advice! – Sebastian Oct 10 '16 at 17:48

0 Answers0