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).