What does "attempt to call a virtual method" mean?

I am an Android developer at the initial level, and every day there is a struggle with a lot of errors and their solutions. I noticed that I get quite often java.lang.NullPointerException: Attempt to invoke virtual method 'xxx' on a null object reference. Upto NullPointerExceptionseems familiar that something is null, but the problem starts with the following phrase. [follow code please for problem]

In my opinion, I found this very obscure, and since Android Studio, unlike Eclipse, continues to talk Attempt to invoke virtual method 'xxx' on a null object referencefor any problem, it is very difficult to find out where I miss the point. Whether for cursors, a database, an arraylist, and any object that you can assume, an Android studio, such as a crazy robot, always gives this error.

Can someone explain to me what this means, and what should be my approach in the general case, I get this error?

Here is one of the weird actions that I encounter in my application, and it drives me crazy.

This feature works great

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 an 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 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 the same format and are placed in one class, but why only the second gives an error? If I think of NPE, then this should also be the case with getAllRows()fucntion, but it is not.

Edited

The updateRow () function is called in another class that describes some different database functions. Here's 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 would be appreciated.

+4
source share
2 answers

As user2864740 clearly explained ,

- , ( "" , ); , null.foo()

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

, 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) . null? db, () query() null. , , , , , db null.

, , db. , , , db - .

+5

SQL .

, SQL ;,

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

-1

Source: https://habr.com/ru/post/1599592/


All Articles