Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dereference may produce NullPointerException when using getter

Tags:

java

android

I'm using a custom View class with an instance reference which serves as an Editor. View is only used in a fragment. I need the instance reference so I can always get custom parameters of the custom View.

public static StoryView instance;
private Story story;

public static Story getCurrentStory(){
    if(instance == null) return null;
    else return instance.story;
}

However, when I'm using this getter method to change the contents of Navigation Drawer, I'm getting a warning:

enter image description here

In here:

private static IDrawerItem[] drawerEditorItems(){
    Story s = StoryView.getCurrentStory();
    SectionDrawerItem section_editor = new SectionDrawerItem()
         .withName(str("placeholder_story_by", s.name, s.author))
         .withDivider(false);
    return new IDrawerItem[]{ section_editor };
}

str(String id, Object... args) is a static method that basically formats i18n strings.


My guess is that the reference s is getting destroyed at the end of the function scope maybe by assigning s = null? And maybe that might destroy the actual instance.story from my custom View?

like image 609
user7401478 Avatar asked Apr 30 '17 09:04

user7401478


People also ask

What is the most likely cause of NullPointerException?

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.

In which case the NullPointerException will be thrown?

NullPointerException is thrown when program attempts to use an object reference that has the null value. These can be: Invoking a method from a null object. Accessing or modifying a null object's field.

How do you handle the NullPointerException using try-catch 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.


2 Answers

When you call

public static Story getCurrentStory(){
    if(instance == null) return null;
    else return instance.story;
}

You check to make sure instance isn't null. If it is, you return null. What may be the case here is that instance is always null(never initialized). Meaning you have to ensure instance is initialized before calling it if you want to get the current story.

Also, this is technically not necessary. Returning a null instance is equivalent to checking if it's null, then returning null. you can also use @NotNull and @Nullable to help both the compiler, yourself, and anyone else working on the code/interacting with it.

Further, it may still return null in some cases, so you want to add a check to ensure it isn't null. This can be done using an if-statement:

if(s != null){
    //Do whatever
}

But the reason you are getting that warning is (in my experience) when it is almost guaranteed you will get an exception. take this for an instance:

View v = null;
v.setText("");

That shows the exact same warning as you are getting. So, most likely, your method will return null no matter what. So you have to make sure instance is initialized, and have an if-statement to make sure the app doesn't crash if it is null. Initializing instance is a way to ensure you get a reference that isn't null

like image 106
Zoe stands with Ukraine Avatar answered Oct 05 '22 02:10

Zoe stands with Ukraine


It is just a warning for possible NPE (NullPointerException). What you should do is write a null check for s before dereferencing it. That's it.

if(s != null){
// Call to s.method();

}

Ultimately, you have to ensure that any reference you are getting must not be de-referenced when it is NULL.

Personally, I have encountered several situations of not checking before dereferencing and these turn out to be bugs most of the times. Google, when developed Guava took the NPE problem into consideration and put it into their design for robustness.

like image 20
Manish Kumar Sharma Avatar answered Oct 05 '22 03:10

Manish Kumar Sharma