What does “java.lang.NullPointerException: Attempt to invoke virtual method '…' on a null object reference” mean, and how do I solve it? [duplicate]

余生长醉 提交于 2020-07-28 05:12:56

问题


I'm getting the NullPointerException described in the title, but I don't understand how to use the information in the error message to debug it. How do I know what's null, and what are common causes of that?


If you're seeing an error like java.lang.NullPointerException: Attempt to invoke virtual method '...' on a null object reference, this question aims to help solve your problem.

This also applies to all the following similar errors:

  • java.lang.NullPointerException: Attempt to invoke interface method '...' on a null object reference
  • java.lang.NullPointerException: Attempt to read from field '...' on a null object reference
  • java.lang.NullPointerException: Attempt to read from null array
  • java.lang.NullPointerException: Attempt to get length of null array

Note: this is intended as a canonical self-answered question to provide a single place to point the many users who have questions about NullPointerExceptions on Android that contains Android-specific advice.

For the non-Android Java version of this question, or for more information, see What is a NullPointerException, and how do I fix it?


回答1:


What is a NullPointerException?

First off, if you don't understand what null is, or what a NullPointerException means, take a look at What is a NullPointerException, and how do I fix it?. The rest of this answer will assume you've read that post.

How to investigate the cause

Now that you know the basics, you need to figure out what line your error is on. Take a look at What is a stack trace, and how can I use it to debug my application errors? for information on how to do that.

Once you know what line the error is on, the next thing you need to do is figure out what variable, exactly, is null. The error message will help you with that.

If your error looks like (the first ... may be either virtual or interface—it's not important which):

java.lang.NullPointerException: Attempt to invoke ... method 'ReturnTypeOfMethod package.name.SomeClass.someMethod(...)' on a null object reference

That means that the variable of type package.name.SomeClass on which you tried to call someMethod was null. It will also list the parameter types of the method, if any, where the (...) is in the example.

So, if you have this code:

MyClass myVariable = new MyClass();
String logMessage = "some other text: " + myVariable.toString() + someOtherVariable.method();

and you get this error message:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.mypackagename.MyClass.toString()' on a null object reference

You know that myVariable is null, because that's the only variable of type MyClass that had toString() called on it. The java.lang.String just means that toString() returns a String.

If the error is Attempt to read from field..., then apply the above advice, but looking for reading a field reference (MyClass.someProperty) rather than calling a method (MyClass.someMethod()).

If the error is Attempt to read from null array, look for anything on that line that reads from an array by index (for example, someArrayVariable[i]).

If the error is Attempt to get length of null array, look for a .length call on an array variable.

Common root causes and fixes

findViewById before setting up View in Activity/Fragment

One common problem is calling findViewById before the View is actually set up. For example, calling it in a member variable declaration:

public class MyActivity extends Activity {
    private Button submitButton = findViewById(R.id.submitButton);
    // ... rest of Activity code ...
}

or before setContentView:

public class MyActivity extends Activity {
    private Button submitButton;
    public void onCreate(Bundle savedInstanceState) {
        submitButton = findViewById(R.id.submitButton);
        setContentView(R.layout.activity_main);
    }
    // ... rest of Activity code ...
}

This doesn't work, because the findViewById is executed before you call setContentView in your onCreate method (for an Activity) or onCreateView (for a Fragment). To fix this, make sure you call it after setContentView. For Fragments, you can either put the code in onViewCreated, or ensure that you are calling it on the View returned from the LayoutInflater.inflate call.

Incorrect layout ID/view ID/view not in layout

The problem is sometimes that there is no view with the ID passed to findViewById in the given layout. Double check to make sure that your setContentView or LayoutInflater.inflate calls have the right layout ID (matches the XML filename) and that there is a View with that ID in that layout XML file (or one that it <include>s).



来源:https://stackoverflow.com/questions/62867557/what-does-java-lang-nullpointerexception-attempt-to-invoke-virtual-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!