-2

my app is Insert/Update/Delete/Show database app. I have successfully managed to develop the Insert part so far, but my app crashes when trying to show database contents, although it builds successfully.

I have one separate activity called DatabaseHelper, and the other one is the one where the data should be shown in the ActionDialog.

It crashes when I hit the "Show data" button which is in my MainActivity

DatabaseHelper.java

public DatabaseHelper (Context context) {
    super (context, DATABASE_NAME, null, 1);
    //SQLiteDatabase db = this.getWritableDatabase();
}

...

public Cursor getAllData(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("SELECT * FROM "+TABLE_NAME, null);
    return res;
}

and Activity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pregled);

    buttonPregled = (Button)findViewById(R.id.BtnPregled);
    viewAll();
}

//
public void viewAll(){
    buttonPregled.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Cursor res = myDb.getAllData();
            if(res.getCount() == 0){
                //show message
                showMessage("Error", "Table empty");
                return;
            }

            StringBuffer buffer = new StringBuffer();
            while (res.moveToNext()){
                buffer.append("ID:"+ res.getString(0)+"\n");
                buffer.append("Naslov:"+ res.getString(1)+"\n");
                buffer.append("Datum:"+ res.getString(2)+"\n");
                buffer.append("Kontakt osoba:"+ res.getString(3)+"\n");
                buffer.append("Kontakt broj:"+ res.getString(4)+"\n");
                buffer.append("Lokacija:"+ res.getString(5)+"\n");
                buffer.append("Pogodba:"+ res.getString(6)+"\n");
                buffer.append("Zarada:"+ res.getString(7)+"\n\n");
            }

            //Show all data
            showMessage("Svirke", buffer.toString());
        }
    });
}

public void showMessage (String title, String Message){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(Message);
    builder.show();
}

Stack trace

05-09 15:20:07.935 4137-4137/com.example.vramba.evis17 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: com.example.vramba.evis17, PID: 4137
                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vramba.evis17/com.example.vramba.evis17.PregledActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2702)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)
                                                                         at android.app.ActivityThread.access$900(ActivityThread.java:177)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:145)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5951)
                                                                         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:1400)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
                                                                      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                         at com.example.vramba.evis17.PregledActivity.viewAll(PregledActivity.java:25)
                                                                         at com.example.vramba.evis17.PregledActivity.onCreate(PregledActivity.java:20)
                                                                         at android.app.Activity.performCreate(Activity.java:6289)
                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2655)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767) 
                                                                         at android.app.ActivityThread.access$900(ActivityThread.java:177) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:145) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5951) 
                                                                         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:1400) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 

1 Answers1

0

Try this one:

Cursor res = myDb.getAllData();
res.moveToFirst(); // OR you can also write res.moveToPosition(0);
if(res.getCount() == 0)
{
   //show message
   showMessage("Error", "Table empty");
   return;
}

If Your DB is properly initialize then this work so before do this initialize your DB.

Andy Developer
  • 3,071
  • 1
  • 19
  • 39
  • Still crashes, I messed sth up in viewAll class, something that has to do with "Attempt to invoke virtual method on a null object reference" error, but I don't know what. – Mike Litoris May 09 '17 at 14:12
  • @MikeLitoris please verify "R.id.BtnPregled" in your layout.xml whether it is same or not. – Andy Developer May 09 '17 at 14:16