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
4377
votes
66 answers

How do I avoid checking for nulls in Java?

I use x != null to avoid NullPointerException. Is there an alternative? if (x != null) { // ... }
Goran Martinic
  • 3,807
  • 4
  • 21
  • 14
1590
votes
8 answers

Is null check needed before calling instanceof?

Will null instanceof SomeClass return false or throw a NullPointerException?
Johan Lübcke
  • 19,666
  • 8
  • 38
  • 35
1307
votes
25 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
  • 13,471
  • 4
  • 19
  • 19
761
votes
21 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-
  • 75,269
  • 21
  • 115
  • 152
572
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
  • 44,224
  • 30
  • 113
  • 140
530
votes
13 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
  • 6,076
  • 2
  • 14
  • 24
434
votes
11 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
  • 5,177
  • 5
  • 24
  • 24
428
votes
12 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
  • 4,371
  • 2
  • 11
  • 7
325
votes
6 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
  • 16,044
  • 17
  • 67
  • 98
318
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
  • 15,461
  • 36
  • 117
  • 195
277
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
  • 3,230
  • 3
  • 18
  • 21
251
votes
6 answers

Filter values only if not null using lambda in Java8

I have a list of objects say car. I want to filter this list based on some parameter using Java 8. But if the parameter is null, it throws NullPointerException. How to filter out null values? Current code is as follows requiredCars =…
vaibhavvc1092
  • 3,067
  • 4
  • 19
  • 26
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…
225
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
  • 8,246
  • 16
  • 60
  • 92
225
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…
1
2 3
99 100