-2

Since this is my second app, and my first app was 99% designing, this could be a duplicate because i might not be using the proper keywords for my searches, but i'm searching for 3 hours now for the solution, which is probably very simple, and i can't seem to find it.

When I try to save information to my database with 2 TextViews, 1 Spinner, and a Button, i get this error message:

FATAL EXCEPTION: main
                                                                   Process: nl.pluuk.gelduren, PID: 29876
                                                                   java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
                                                                       at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
                                                                       at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                                       at nl.pluuk.gelduren.Add_client.saveData(Add_client.java:76)
                                                                       at nl.pluuk.gelduren.Add_client$3.onClick(Add_client.java:67)
                                                                       at android.view.View.performClick(View.java:5233)
                                                                       at android.view.View$PerformClick.run(View.java:21209)
                                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:152)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5497)
                                                                       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)

This is my code which i'm currently using to not save any data

public void save(){
        save = (Button)findViewById(R.id.button_save_client);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("Save Button Clicked");
                Client_textView = (TextView)findViewById(R.id.inputform_client_name);
                Rate_textView = (TextView)findViewById(R.id.inputform_rate);
                Pay_Period_textView = (Spinner)findViewById(R.id.spinner_pay_period);
                Client = "" + Client_textView.getText();
                Rate = Integer.parseInt("" + Rate_textView.getText());
                Pay_Period = "" + Pay_Period_textView.getSelectedItem();

                saveData();
            }
        });
    }



    public void saveData(){
        // Gets the data repository in write mode
        SQLiteDatabase db = mDbHelper.getWritableDatabase();

// Create a new map of values, where column names are the keys
        ContentValues values = new ContentValues();
        values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_CLIENT_NAME, Client);
        System.out.println("Yes, i'm in your log");
        values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_RATE, Rate);
        values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_PAY_PERIOD, Pay_Period);

// Insert the new row, returning the primary key value of the new row
        long newRowId;
        newRowId = db.insert(
                FeedReaderContract.FeedEntry.TABLE_NAME,
                null,
                values);
    }

This code is all inside Add_client. The error Add_client.java:76 is referring to the line: SQLiteDatabase db = mDbHelper.getWritableDatabase(); The error Add_client.java:67 is referring to the line: saveData(); Which is probably caused by line 76.

I made sure that there are columns inside the database, by executing System.out.println("Column count:" + c.getColumnCount());

This told me that there where 3 columns, which is what I was expecting. I also checked if there was any data inside the columns with:

Boolean rowExists;
        if (c.moveToFirst())
        {
            System.out.println(c.getColumnName(0));
            rowExists = true;

        } else
        {
            System.out.println("Nothing to see here");
            rowExists = false;
        }

This gave me the output: Nothing to see here, which is was also expecting because the database starts empty.

Where is the mistake in my code which keeps smashing me these errors?

Is there is any other information needed, I will be happily include it in an edit.

  • is mDbHelper initialised? Perhaps it is null when calling saveData(). Log it to check. – Sanj Jun 20 '16 at 15:57

1 Answers1

2

It seems like the context you passed into your SQliteOpenHelper, in this case, mDBHelper, is null.

Charles Li
  • 1,835
  • 3
  • 24
  • 40