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
63
votes
3 answers

NullPointer on toLowerCase but I don't use that method anywhere

I'm getting the following crash dump in Firebase Crash Reports: Exception java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toLowerCase(java.util.Locale)' on a null object reference bnp.run…
60
votes
19 answers

Is it okay to throw NullPointerException programmatically?

When there is a post-condition, that return value of a method must not be null, what can be done? I could do assert returnValue != null : "Not acceptable null value"; but assertions could be turned off! So is it okay to do if(returnValue==null) …
stratwine
  • 3,663
  • 2
  • 26
  • 32
60
votes
4 answers

Unboxing Null-Object to primitive type results in NullPointerException, fine?

This snippet throws an NullPointerException due to the fact that its unboxed to a primitive type and Long.longValue() is called, right? Thats even easy to see if you have a snippet like this: long value = (Long) null; But the NullPointerException…
Christopher Klewes
  • 11,181
  • 18
  • 74
  • 102
60
votes
3 answers

NullPointerException stack trace not available without debug agent

I have recently found a bug that causes a NullPointerException. The exception is caught and logged using a standard slf4j statement. Abridged code below: for(Action action : actions.getActions()) { try { context =…
omerkudat
  • 9,371
  • 4
  • 33
  • 42
59
votes
2 answers

java.lang.NullPointerException is thrown using a method-reference but not a lambda expression

I've noticed something weird about unhandled exceptions using Java 8 method reference. This is my code, using the lambda expression () -> s.toLowerCase(): public class Test { public static void main(String[] args) { testNPE(null); …
59
votes
11 answers

ButterKnife 8.0.1 not working

I am using butterknife 8.0.1, but a nullpointerexception is appearing. This line is on my build.grade file: compile 'com.jakewharton:butterknife:8.0.1' this is my Main Class: (I wrote the includes properly) import…
Sebastian Corradi
  • 1,353
  • 2
  • 14
  • 25
59
votes
2 answers

NPE while inflating layout (Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference)

I keep getting a java.lang.NullPointerException when I try to use ScrollView in an activity. The weird thing is that I've used the exact same Scrollview setup in other activities. And all of a sudden I'm getting errors. I've tried cleaning the…
59
votes
12 answers

Null pointer Exception - findViewById()

Can anyone help me to find out what can be the issue with this program. In the onCreate() method the findViewById() returns null for all ids and this causes a null pointer exception later. I can not figure out why the findViewById() can not find the…
user2629828
  • 685
  • 1
  • 8
  • 13
57
votes
18 answers

Is Catching a Null Pointer Exception a Code Smell?

Recently a co-worker of mine wrote in some code to catch a null pointer exception around an entire method, and return a single result. I pointed out how there could've been any number of reasons for the null pointer, so we changed it to a defensive…
Drew
  • 15,158
  • 17
  • 68
  • 77
54
votes
6 answers

How to avoid checking for null values in method chaining?

I need to check if some value is null or not. And if its not null then just set some variable to true. There is no else statement here. I got too many condition checks like this. Is there any way to handle this null checks without checking all…
user9521067
53
votes
5 answers

Why static fields are not initialized in time?

The following code prints null once. class MyClass { private static MyClass myClass = new MyClass(); private static final Object obj = new Object(); public MyClass() { System.out.println(obj); } public static void main(String[]…
The Student
  • 27,520
  • 68
  • 161
  • 264
53
votes
4 answers

Difference between null and empty string

What is the difference between a null string (String s = null) and an empty string (String s = "")? This is what I have: String s1 = ""; //print statement does not print any thing for s1 but s1.length()=0 String s2 = null;//print statement prints…
Shakeeb Ayaz
  • 6,200
  • 6
  • 45
  • 64
52
votes
2 answers

Hibernate query.list() method is returning empty list instead of null value

When there are no rows, both query.list() and criteria.list() are returning empty list instead of a null value. What is the reason behind this?
Reddy
  • 8,737
  • 11
  • 55
  • 73
52
votes
3 answers

What is the difference between "(Object)null" and "null" in Java?

Take a look at the following example: class nul { public static void main (String[] args) { System.out.println (String.valueOf((Object)null)); System.out.println (String.valueOf(null)); } } The first println writes null but the second…
ceving
  • 21,900
  • 13
  • 104
  • 178
50
votes
5 answers

How come invoking a (static) method on a null reference doesn't throw NullPointerException?

I wrote this program in Java public class Why { public static void test() { System.out.println("Passed"); } public static void main(String[] args) { Why NULL = null; NULL.test(); } } I read that invoking a method on a null…
Artjem
  • 511
  • 1
  • 4
  • 4