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

15513 questions
4189
votes
65 answers

Avoiding NullPointerException in Java

I use object != null a lot to avoid NullPointerException. What is an alternative to: if (someobject != null) { someobject.doCalc(); }
Goran Martinic
  • 3,757
  • 4
  • 19
  • 14
1440
votes
7 answers

Is null check needed before calling instanceof?

Will null instanceof SomeClass return false or throw a NullPointerException?
Johan Lübcke
  • 17,508
  • 8
  • 35
  • 34
1119
votes
22 answers

Which @NotNull Java annotation should I use?

I'm looking to make my code more readable as well as use tooling like IDE code inspection and/or static code analysis (FindBugs and Sonar) to avoid NullPointerExceptions. Many of the tools seem incompatible with each others'…
jaxzin
  • 11,591
  • 4
  • 16
  • 19
654
votes
22 answers

Why is my Spring @Autowired field null?

Note: This is intended to be a canonical answer for a common problem. I have a Spring @Service class (MileageFeeCalculator) that has an @Autowired field (rateService), but the field is null when I try to use it. The logs show that both the…
chrylis -cautiouslyoptimistic-
  • 67,584
  • 19
  • 106
  • 140
558
votes
26 answers

IllegalArgumentException or NullPointerException for a null parameter?

I have a simple setter method for a property and null is not appropriate for this particular property. I have always been torn in this situation: should I throw an IllegalArgumentException, or a NullPointerException? From the javadocs, both seem…
Mike Stone
  • 42,897
  • 27
  • 110
  • 137
392
votes
12 answers

NullPointerException in Collectors.toMap with null entry values

Collectors.toMap throws a NullPointerException if one of the values is null. I don't understand this behaviour, maps can contain null pointers as value without any problems. Is there a good reason why values cannot be null for…
Jasper
  • 4,676
  • 2
  • 11
  • 23
368
votes
10 answers

NullPointerException in Java with no StackTrace

I've had instances of our Java code catch a NullPointerException, but when I try to log the StackTrace (which basically ends up calling Throwable.printStackTrace() ), all I get is: java.lang.NullPointerException Has anyone else come across this? I…
Edward Shtern
  • 4,507
  • 5
  • 22
  • 22
315
votes
7 answers

Why can I throw null in Java?

When running this: public class WhatTheShoot { public static void main(String args[]){ try { throw null; } catch (Exception e){ System.out.println(e instanceof NullPointerException); …
bharal
  • 14,067
  • 31
  • 108
  • 182
281
votes
11 answers

Why should one use Objects.requireNonNull()?

I have noted that many Java 8 methods in Oracle JDK use Objects.requireNonNull(), which internally throws NullPointerException if the given object (argument) is null. public static T requireNonNull(T obj) { if (obj == null) throw new…
user4686046
  • 2,901
  • 2
  • 9
  • 7
261
votes
4 answers

Why use Optional.of over Optional.ofNullable?

When using the Java 8 Optional class, there are two ways in which a value can be wrapped in an optional. String foobar = ; Optional.of(foobar); // May throw NullPointerException Optional.ofNullable(foobar); // Safe from…
whirlwin
  • 14,425
  • 17
  • 65
  • 90
249
votes
10 answers

No Exception while type casting with a null in java

String x = (String) null; Why there is no exception in this statement? String x = null; System.out.println(x); It prints null. But .toString() method should throw a null pointer exception.
Vicky
  • 2,910
  • 3
  • 16
  • 20
229
votes
11 answers

Best explanation for languages without null

Every so often when programmers are complaining about null errors/exceptions someone asks what we do without null. I have some basic idea of the coolness of option types, but I don't have the knowledge or languages skill to best express it. What is…
210
votes
10 answers

Android - How To Override the "Back" button so it doesn't Finish() my Activity?

I currently have an Activity that when it gets displayed a Notification will also get displayed in the Notification bar. This is so that when the User presses home and the Activity gets pushed to the background they can get back to the Activity via…
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
  • 20,298
  • 23
  • 71
  • 99
208
votes
3 answers

Converting a Date object to a calendar object

So I get a date attribute from an incoming object in the form: Tue May 24 05:05:16 EDT 2011 I am writing a simple helper method to convert it to a calendar method, I was using the following code: public static Calendar DateToCalendar(Date date…
Will
  • 7,184
  • 15
  • 55
  • 81
1
2 3
99 100