Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method invocation may produce java NullpointerException

I have a code:

public String getNameUpdateEvent(long id) {
    Cursor mCursor =
            db.rawQuery("select name from events WHERE _id=" + id + ";", null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    String updateNameEvent;
    updateNameEvent = mCursor.getString(mCursor.getColumnIndex("name"));
    return updateNameEvent;
}    

and I´m getting a warning

    Warning:(173, 45) Method invocation 'mCursor.getColumnIndex("name")' may produce 'java.lang.NullPointerException'

How i can fix it pls?

like image 342
Raddino Avatar asked Dec 23 '14 23:12

Raddino


People also ask

How do I fix method invocation may produce NullPointerException?

Method invocation may produce NullPointerException warning while it's checked before. What steps will reproduce the problem? Try to add toString to a variable that has been checked for null before, to reproduce the issue this checking has to be in a private method, like in the screenshot.

What causes NullPointerException in Java?

What Causes NullPointerException. The NullPointerException occurs due to a situation in application code where an uninitialized object is attempted to be accessed or modified. Essentially, this means the object reference does not point anywhere and has a null value.

How do I fix NullPointerException in Java?

In Java, the java. lang. NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

Can we catch NullPointerException?

It is generally a bad practice to catch NullPointerException. Programmers typically catch NullPointerException under three circumstances: The program contains a null pointer dereference. Catching the resulting exception was easier than fixing the underlying problem.

How to effectively handle NullPointerException in Java?

Java NullPointerException – How to effectively handle null pointer in Java. 3.1. Ternary Operator. This operator results to the value on the left hand side if not null else right hand side is evaluated. It has syntax like : 3.2. Use apache commons StringUtils for String operations. 3.3. Check Method ...

Why do I get a null pointer exception when calling a method?

If you are dealing with static variables or static method than you won’t get null pointer exception even if you have your reference variable pointing to null because static variables and method call are bonded during compile time based on class name and not associated with object

What happens if an object is null in the second method?

Even if object is null in second method, it will not give exception and will prints ‘null’ to output stream. 3.7. Avoid returning null from your methods An awesome tip to avoid NPE is to return empty strings or empty collections rather than a null.

Are all method invocations illegal in Java?

Joshua bloch in effective java says that “Arguably, all erroneous method invocations boil down to an illegal argument or illegal state, but other exceptions are standardly used for certain kinds of illegal arguments and states.


1 Answers

Your cursor can not be null, it will always have any value. But cursor can be empty, so you should firstly go to first row in cursor with method moveToFirst(), and if it returns true - it means, that cursor has at least one row, so you can do with it all you want, if it returns false - it means, that there is nothing for your query, so you have not any rows to get data from. Your code should look like this:

public String getNameUpdateEvent(long id) {
    Cursor mCursor =
        db.rawQuery("select name from events WHERE _id=" + id + ";", null);

    String updateNameEvent = null;
    if (mCursor != null && mCursor.moveToFirst()) {
        updateNameEvent = mCursor.getString(mCursor.getColumnIndex("name"));
    }
    return updateNameEvent;
}  
like image 147
Orest Savchak Avatar answered Oct 04 '22 10:10

Orest Savchak