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
96
votes
2 answers

Why do Double.parseDouble(null) and Integer.parseInt(null) throw different exceptions?

Why do Double.parseDouble(null) and Integer.parseInt(null) throw different exceptions? Is this a historical accident or intentional? The documentation clearly states two types of exceptions for Double.parseDouble(...) and one for Integer.parseInt(),…
pho79
  • 1,755
  • 1
  • 15
  • 27
91
votes
11 answers

Check chains of "get" calls for null

Let's say I'd like to perform the following command: house.getFloor(0).getWall(WEST).getDoor().getDoorknob(); To avoid a NullPointerException, I'd have to do the following if: if (house != null && house.getFloor(0) &&…
user321068
89
votes
7 answers

Why is this not throwing a NullPointerException?

Nead clarification for following code: StringBuilder sample = new StringBuilder(); StringBuilder referToSample = sample; referToSample.append("B"); System.out.println(sample); This will print B so that proves sample and referToSample objects refer…
commit
  • 4,777
  • 15
  • 43
  • 70
87
votes
3 answers

Android: Pass data(extras) to a fragment

I'm new to Android programming and I'm having problems while passing an ArrayList of a Parcelable to a fragment. This is the Activity that is launched(working well!) where feedlist is an ArrayList of a parcelable Music. Intent in = new…
pluralism
  • 1,487
  • 2
  • 16
  • 22
86
votes
8 answers

How do you tell if a checkbox is selected in Selenium for Java?

I am using Selenium in Java to test the checking of a checkbox in a webapp. Here's the code: private boolean isChecked; private WebElement e; I declare e and assign it to the area where the checkbox is. isChecked =…
jamesfzhang
  • 4,403
  • 8
  • 32
  • 43
86
votes
7 answers

Why comparing Integer with int can throw NullPointerException in Java?

It was very confusing to me to observe this situation: Integer i = null; String str = null; if (i == null) { //Nothing happens ... } if (str == null) { //Nothing happens } if (i == 0) { //NullPointerException ... } if…
Roman
  • 64,384
  • 92
  • 238
  • 332
83
votes
32 answers

java.lang.NullPointerException: Missing required view with ID:

Android Studio 3.6 in app/build.gradle: android { viewBinding.enabled = true Here my xml:
Alexei
  • 14,350
  • 37
  • 121
  • 240
78
votes
1 answer

Which @NonNull Java annotation to use

What is the best 'NonNull' annotation? "Best" in the sense of Standard way e.g. future proofness (e.g. support by standard jdk etc.) Support of IDE's (shows up in java doc to indicate the usage for developers) Support by static analysis tools like…
Lonzak
  • 9,334
  • 5
  • 57
  • 88
77
votes
12 answers

Is it a bad idea if equals(null) throws NullPointerException instead?

The contract of equals with regards to null, is as follows: For any non-null reference value x, x.equals(null) should return false. This is rather peculiar, because if o1 != null and o2 == null, then we have: o1.equals(o2) // returns…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
72
votes
11 answers

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=0, data=null} to activity

My app allows the user to press a button, it opens the camera, they can take a photo and it will show up in an ImageView. If the user presses back or cancel while the camera is open I get this force close - Failure delivering result…
dabious
  • 927
  • 3
  • 10
  • 15
69
votes
21 answers

JavaFX Location is not set error message

I have problem when trying to close current scene and open up another scene when menuItem is selected. My main stage is coded as below: public void start(Stage primaryStage) throws Exception { primaryStage.setTitle("Shop Management"); …
user2424370
67
votes
3 answers

Catching exceptions thrown from native code running on Android

The project I'm currently working on requires me to code up the android portion of a cross platform program implementation. A core set of functionality is built and included in my app through android-ndk. I've found that any exception/crash which…
Graeme
  • 25,714
  • 24
  • 124
  • 186
66
votes
3 answers

Loading resources like images while running project distributed as JAR archive

I am having a error for my GUI. Trying to set title bar icon then be included in a Runnable JAR. BufferedImage image = null; try { image = ImageIO.read(getClass().getClassLoader().getResource("resources/icon.gif")); } catch (IOException e) { …
exlux15
  • 909
  • 1
  • 9
  • 16
65
votes
4 answers

How to serialize null value when using Parcelable interface

regarding my code example down, what shold I do if one Locable's variables is null? In example, now if l.getZoom() returns null, I got NullPointerException. @Override public void writeToParcel(Parcel parcel, int arg1) { parcel.writeInt(count); …
igor.beslic
  • 954
  • 2
  • 9
  • 16
63
votes
1 answer

Getting NullPointerException at onFilterTouchEventForSecurity

I have built app in which I integrate YouTube API and it is working fine without any crash but on Fabric I checked some crash whis is ipf.onFilterTouchEventForSecurity. Here is the full logs of the crash: java.lang.NullPointerException: at…
Faisal Shaikh
  • 3,900
  • 5
  • 40
  • 77