5

I am a beginner level Android developer and every day is a fight with a lot of errors and their solutions. I have noticed that I quite many times get java.lang.NullPointerException: Attempt to invoke virtual method 'xxx' on a null object reference. Upto NullPointerException, it sounds familiar that something has value null but problem starts from next phrase. [follow the code please for problem]

In my opinion, I found this very unclear and as Android Studio unlike Eclipse only keeps saying Attempt to invoke virtual method 'xxx' on a null object reference for any sort of problem, it's really hard to get know where I am missing point. Whether for cursors, database, arraylist and any object you can suppose, Android studio like crazy robot gives this error always.

Can anyone explain me what that really mean and what should be my approach in a general case I get this error?

Here is one of strange behaviour I am facing in my app and it is driving me crazy.

This function works fine

ArrayList<HashMap<String, String>> getAllRows() {

    Cursor c = db.query(TABLE_NAME, ALL_KEYS, null, null, null, null, null); // THIS LINE IS MY POINT

    //
     ...
    //

    c.close();

    return profilesArray;
}

But another function gives me error

java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String)' on a null object reference

public boolean updateRow(long ID,  int tracks) {
    String where = COLUMN_NAME_ID + " = " + ID;

    Cursor c =  db.query(TABLE_NAME, ALL_KEYS,
            where, null, null, null, null); // At this line. What happened to this now, if it's working fine in getAllRows()
    String title = null;
    if (c != null) {
        c.moveToFirst();
        title = c.getString(1);
    }
    c.close();

    //
     ...
    //

    return db.update(TABLE_NAME, newValues, where, null) != 0;
}

Both functions have alike format and placed in same class, but why only second one gives error? If I think about NPE then it should also be the case with getAllRows() fucntion but it is not.

Edited

Function updateRow() is called in another class which describe some different database function, Here it is how -

Playlist allPlaylistDB;

public PlaylistSongs(Context ctx) {
    this.context = ctx;
    allPlaylistDB = new Playlist(context); // We have initiated another database class
    myDBHelper = new ReaderDB(context);
}

Detailed help will be appreciated.

serv-inc
  • 35,772
  • 9
  • 166
  • 188
  • 2
    A virtual method is a method resolved polymorphically (a private method would is not 'virtual' nor is a final method nor are static methods); there is nothing 'special' about it and the cause, which is `null.foo()`. Simpy read it as "Attempt to invoke method .. on a null reference .." – user2864740 Jul 24 '15 at 22:46
  • Thanks for shortening the error statement. Help with the cause of problem will be much more appreciated, thanks! –  Jul 24 '15 at 22:52
  • 1
    Can you paste how you are creating the SQLiteDatabase object and where you are creating it ? – Bajji Jul 24 '15 at 23:02
  • Check the edit @Ashwin –  Jul 24 '15 at 23:13
  • Mostly likely I guess its the timing of PlaylistSongs creation that is causing your issue (assuming ReaderDB is where you have your earlier code). In terms of Activity's lifecycle where are you creating the PlaylistSongs object, is it during onCreate of the Activity ? – Bajji Jul 24 '15 at 23:20
  • Thanks @Ashvin. Well titmings was not the issue but you help me to get the spot where the error was. I forget to open the database `allPlaylistDB` before calling its functions. Really a great piece of help. –  Jul 24 '15 at 23:22

2 Answers2

5

As clearly explained by user2864740,

A virtual method is a method resolved polymorphically (a private method would is not 'virtual' nor is a final method nor are static methods); there is nothing 'special' about it and the cause, which is null.foo()

You're getting the error at the following line

Cursor c =  db.query(TABLE_NAME, ALL_KEYS,
            where, null, null, null, null);

The error clearly mentions that android.database.Cursor android.database.sqlite.SQLiteDatabase.query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String) is being called by a null reference. In your code what can be null? It has to be db, because you're trying to call a (virtual) method query() on a null reference. Other thing to figure out whats going wrong is to place a break point at the above line and then you will see that db would be null.

To fix it, make sure you initialize the db variable properly. You also say that its working fine in some other place, make sure that you are not nullifying the db object somewhere else in the code.

Community
  • 1
  • 1
Antrromet
  • 15,294
  • 10
  • 60
  • 75
  • 1
    Yes, thanks you are right. I have already solved problem and it was really what you said - *missed opening db*. I quickly closed the db after one of operations and next time didn't opened. Thanks for your answer to help me understanding better. –  Jul 24 '15 at 23:27
-1

Your SQL statement appears to be invalid.

You probably need to close a SQL statement with a ;,

i.e. String where = COLUMN_NAME_ID + " = " + ID + ";"

StephenG
  • 2,851
  • 1
  • 16
  • 36
  • 3
    That should cause an error to be returned from the server. In this case the error is an NPE at the method invocation labeled by the OP. – nanofarad Jul 24 '15 at 22:44