Questions tagged [nullpointerexception]

The Java exception thrown when an application attempts to use null in a case where an object is required.

This Java is thrown when an application attempts to use null in a case where an object is required.

These include:

  • Calling the instance method of a null object.
  • Accessing or modifying the field of a null object.
  • Taking the length of null as if it were an array.
  • Accessing or modifying the slots of null as if it were an array.
  • Throwing null as if it were a Throwable value.
  • Unboxing of null object.

Applications should throw instances of this exception to indicate other illegal uses of the null object.

It's often abbreviated as NPE

Practically all questions with this tag are duplicates of the canonical What is a NullPointerException and how do I fix it?.

15961 questions
209
votes
12 answers

What is a NullPointerException, and how do I fix it?

What are Null Pointer Exceptions (java.lang.NullPointerException) and what causes them? What methods/tools can be used to determine the cause so that you stop the exception from causing the program to terminate prematurely?
Ziggy
  • 21,845
  • 28
  • 75
  • 104
199
votes
8 answers

Android. Fragment getActivity() sometimes returns null

In developer console error reports sometimes I see reports with NPE issue. I do not understand what is wrong with my code. On emulator and my device application works good without forcecloses, however some users get NullPointerException in fragment…
194
votes
9 answers

Why explicitly throw a NullPointerException rather than letting it happen naturally?

When reading JDK source code, I find it common that the author will check the parameters if they are null and then throw new NullPointerException() manually. Why do they do it? I think there's no need to do so since it will throw new…
LiJiaming
  • 1,629
  • 2
  • 9
  • 5
190
votes
8 answers

Returning null as an int permitted with ternary operator but not if statement

Let's look at the simple Java code in the following snippet: public class Main { private int temp() { return true ? null : 0; // No compiler error - the compiler allows a return value of null // in a method signature…
Lion
  • 18,729
  • 22
  • 80
  • 110
178
votes
11 answers

How to use @Nullable and @Nonnull annotations more effectively?

I can see that @Nullable and @Nonnull annotations could be helpful in preventing NullPointerExceptions but they do not propagate very far. The effectiveness of these annotations drop off completely after one level of indirection, so if you only add…
Rylander
  • 19,449
  • 25
  • 93
  • 144
169
votes
5 answers

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference

I'm trying to put together a shopping list app, based on input fields, ArrayList, and ListView. The app will be based on Fragments. However, I have encountered a problem and I do not know how to solve it. I looked around on both Google and…
163
votes
10 answers

Can't find @Nullable inside javax.annotation.*

I want use @Nullable annotation to eliminate NullPointerExceptions. I found some tutorials on the net, I noticed that this annotation comes from the package javax.annotation.Nullable; but when I import it a compilation error is generated: cannot…
user2354035
140
votes
4 answers

Why does String.valueOf(null) throw a NullPointerException?

according to the documentation, the method String.valueOf(Object obj) returns: if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned. But how come when I try do…
user282886
  • 3,125
  • 8
  • 22
  • 11
137
votes
4 answers

Booleans, conditional operators and autoboxing

Why does this throw NullPointerException public static void main(String[] args) throws Exception { Boolean b = true ? returnsNull() : false; // NPE on this line. System.out.println(b); } public static Boolean returnsNull() { return…
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
125
votes
19 answers

Null check chain vs catching NullPointerException

A web service returns a huge XML and I need to access deeply nested fields of it. For example: return wsObject.getFoo().getBar().getBaz().getInt() The problem is that getFoo(), getBar(), getBaz() may all return null. However, if I check for null in…
121
votes
13 answers

NullPointerException accessing views in onCreate()

This is a canonical question for a problem frequently posted on StackOverflow. I'm following a tutorial. I've created a new activity using a wizard. I get NullPointerException when attempting to call a method on Views obtained with findViewById() in…
laalto
  • 150,114
  • 66
  • 286
  • 303
116
votes
5 answers

Boolean.valueOf() produces NullPointerException sometimes

I have this code: package tests; import java.util.Hashtable; public class Tests { public static void main(String[] args) { Hashtable modifiedItems = new Hashtable(); System.out.println("TEST…
David E
  • 1,197
  • 2
  • 8
  • 6
113
votes
19 answers

Best way to check for null values in Java?

Before calling a function of an object, I need to check if the object is null, to avoid throwing a NullPointerException. What is the best way to go about this? I've considered these methods. Which one is the best programming practice for Java? //…
JamieGL
  • 1,392
  • 2
  • 9
  • 13
112
votes
3 answers

Why does int num = Integer.getInteger("123") throw NullPointerException?

The following code throws NullPointerException: int num = Integer.getInteger("123"); Is my compiler invoking getInteger on null since it's static? That doesn't make any sense! What's happening?
user282886
  • 3,125
  • 8
  • 22
  • 11
97
votes
11 answers

Eclipse / Android : "Errors running builder 'Android Pre Compiler' on project..."

Attempting to do some work on an Android project I haven't worked on for a couple of months, yet every time I attempt to build the project Eclipse throws up a dialog saying: 'Building workspace' has encountered a problem Errors occurred during the…
Mick Byrne
  • 14,394
  • 16
  • 76
  • 91