What does "Attempt to Invoke Virtual Method" really mean?

22,963

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.

Share:
22,963
Gavin
Author by

Gavin

I am a Computer Science student and have great enthusiasm about coding websites and android applications. It's been a while I am self learning all the techniques and methods to become a successful programmer. Instead coding, I also like to do Maths. I love to play with numbers and solving Calculus problems. Cheers!

Updated on November 05, 2021

Comments

  • Gavin
    Gavin over 2 years

    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.