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
25
votes
9 answers

Gracefully avoiding NullPointerException in Java

Consider this line: if (object.getAttribute("someAttr").equals("true")) { // .... Obviously this line is a potential bug, the attribute might be null and we will get a NullPointerException. So we need to refactor it to one of two choices: First…
Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
25
votes
2 answers

Multiple overloaded methods: Does null equal NullPointerException?

public class TestMain { public static void methodTest(Exception e) { System.out.println("Exception method called"); } public static void methodTest(Object e) { System.out.println("Object method called"); } …
Anil Gowda
  • 446
  • 3
  • 12
25
votes
4 answers

Spring Boot - Environment @Autowired throws NullPointerException

I have a project setup using Spring Boot 0.5.0.M5. In one of the configuration files I am trying to @Autowire Environment but that fails with a NullPointerException. Here's what I have so…
imme
  • 299
  • 1
  • 4
  • 8
25
votes
2 answers

Android NullPointerException in Instrumentation.execStartActivity

I keep getting the bellow exception from some users: java.lang.NullPointerException at android.app.Instrumentation.execStartActivity(Instrumentation.java:1414) at android.app.Activity.startActivityForResult(Activity.java:2880) at…
Adrian
  • 717
  • 11
  • 18
24
votes
8 answers

My current location always returns null. How can I fix this?

I am trying to find my current location for an android project. When the application is loaded my current location is always null. I have set up the permissions in the manifest etc. When I find the current location I intend to use the coordinates to…
devinefergal
  • 287
  • 1
  • 4
  • 10
24
votes
9 answers

Clean way of avoiding NullPointerException in equals checks

I have an address object that I want to create an equals method for. I could have made this quite simple by doing something like the following (shortened a bit): public boolean equals(Object obj) { if (this == obj) return true; if…
Svish
  • 152,914
  • 173
  • 462
  • 620
24
votes
3 answers

Java NullPointerException when adding to ArrayList?

My code is throwing a NullPointerException, even though the object seems to properly exist. public class IrregularPolygon { private ArrayList myPolygon; public void add(Point2D.Double aPoint) { …
waiwai933
  • 14,133
  • 21
  • 62
  • 86
24
votes
6 answers

Null Object Pattern

There seems to be a growing community of people saying that you should never return null and should always use the Null Object Pattern instead. I can see the usefullness of the NOP when using a collection/map/array or calling boolean functions such…
TigerBear
  • 2,479
  • 1
  • 21
  • 24
24
votes
4 answers

Cause for NullPointerException android.support.v7.widget.RecyclerView.onMeasure

I am getting following Exception 01-27 11:15:15.756 18348-18348/com.example.pnimje.newswipelistview E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException at…
T_C
  • 3,148
  • 5
  • 26
  • 46
24
votes
9 answers

Why is NullPointerException not declared as a checked exception

This was the question asked in an interview. NullPointerException is very common; why is it not declared as a checked exception? I googled but did not get a proper answer.
giri
  • 26,773
  • 63
  • 143
  • 176
24
votes
2 answers

Failure delivering result ResultInfo

There are many people who have encountered the same error on stackoverflow, but I haven't been able to find any relevant resolution in those posts. My MainActivity is starting a new activity (SecondActivity) with startActivityForResult();…
timeshift117
  • 1,720
  • 2
  • 18
  • 21
24
votes
10 answers

When is it OK to catch NullPointerException?

Effective java recommends that we shouldn't catch NullPointerException. Is it always right? In many cases of catching NullPointerException, catch body only calls printStackTrace(). If I don't catch NullPointerException and call printStackTrace(),…
500004dolkong
  • 725
  • 3
  • 12
  • 19
24
votes
3 answers

A good way to debug nullPointerException

I am using eclipse and programing in java. Sometimes I come across a nullPointerException that will plague me for hours. Is there anyway to debug nullPointerExceptions better and figure out what variable values are and other things that would cause…
Noah Huppert
  • 4,028
  • 6
  • 36
  • 58
24
votes
5 answers

AutosizeColumns on SXSSFWorkbook

Is it possible to autoSizeColumns on a streaming SXSSFWorkbook? I implemented an export functionality to export a list of objects to excel. At first I used the XSSFWorkbook (not streaming) and after all the cells were created, I autosized all the…
sterym
  • 543
  • 1
  • 3
  • 9
23
votes
7 answers

Error invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference

All I am trying to do is let the user get a list of the places of types he likes. For example if the input was hospital my application would open google maps with the search string "Hospital". But as suggested in the documentation using the geocode…
KISHORE_ZE
  • 1,466
  • 3
  • 16
  • 26