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
49
votes
6 answers

Null values of Strings and Integers in Java

public class Test { public static void main(String[] args) { String s = null; String s1 = null; Integer i = null; Integer i1 = null; System.out.println(s+i); System.out.println(i+s); …
tania
  • 1,086
  • 2
  • 12
  • 31
48
votes
2 answers

Unwanted NullPointerException in ternary operator - Why?

While executing the following code, I am getting a NullPointerException at line: value = condition ? getDouble() : 1.0; In earlier lines when I use null instead of getDouble() everything works and this is strange. public class Test { static…
T-Bag
  • 10,916
  • 3
  • 54
  • 118
48
votes
8 answers

Android - NullPointerException on SearchView in Action Bar

I've got a problem trying to add a SearchView widget to the ActionBar in my activity - I get a null value when calling getActionView to get my SearchView object. I've been following the Android Developer guide and also went through a ton of SO…
JakeP
  • 1,736
  • 4
  • 23
  • 31
48
votes
3 answers

How to solve java.lang.NullPointerException error?

When I run my Java program, it gives me an error on this line compiler.getTask(null, null, new DiagnosticCollector(), null, null, compilationUnits); Error I am getting is: Exception in thread "main" java.lang.NullPointerException …
Justin k
  • 1,104
  • 5
  • 22
  • 33
47
votes
1 answer

java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference

I am trying to save player's name in shared preference and make it display in another activity by getting it again in shared preference but my app crash. FATAL EXCEPTION: main Process: plp.cs4b.thesis.drawitapp, PID: 1970 …
ryckiazai
  • 481
  • 1
  • 4
  • 5
46
votes
5 answers

NullPointerException that doesn't point to any line in my code

I am working on a game where the player can drag and drop things around the screen. I've got a private method which allows me to simulate a drag/drop event for any of the items that the player can move around. For the dragging I am actually leaving…
FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
45
votes
4 answers

getActionBar() returns Null (AppCompat-v7 21)

My app is crashing the minute I run it after I changed my AppCompat-v7 to 21.0.0 and Compiled with no problem. It gives me the error: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setIcon(int)' on a…
user3184899
  • 3,019
  • 6
  • 30
  • 38
43
votes
7 answers

Safe dereferencing in Python

Groovy has a nice operator for safe dereferencing, which helps to avoid NullPointerExceptions: variable?.method() The method will only be called, if variable is not null. Is there a way to do the same in Python? Or do I have to write if variable:…
deamon
  • 89,107
  • 111
  • 320
  • 448
43
votes
6 answers

How to handle nulls when using Java collection sort

When using Collection.sort in Java what should I return when one of the inner objects is null Example: Collections.sort(list, new Comparator() { public int compare(MyBean o1, MyBean o2) { return…
Giancarlo Corzo
  • 1,976
  • 5
  • 24
  • 36
43
votes
5 answers

NullPointerException on getActivity().runOnUiThread(new Runnable(){

I know there are many different causes for NPE but mine is slightly weird (At least to me). So I have converted my Activities to Fragments successfully, but my problem appears to be coming from the function that displays the date. When the…
Robin
  • 577
  • 1
  • 7
  • 13
41
votes
6 answers

Long.getLong() failing, returning null to valid string

I've spent the past two hours debugging what seems extremely unlikely. I've stripped the method of a secondary Android Activity to exactly this: public void onClick(View v) { String str = "25"; long my_long = Long.getLong(str); } //…
SMBiggs
  • 11,034
  • 6
  • 68
  • 83
39
votes
5 answers

OnActivityResult sometimes not called after ACTION_GET_CONTENT intent

I'm working on an image editing Android application. In one of my activities I call an intent to pick an image from the gallery in onCreate() like this: Intent intent = new…
WPMed
  • 1,414
  • 1
  • 13
  • 25
39
votes
6 answers

if statement checks for null but still throws a NullPointerException

In this code. public class Test { public static void testFun(String str) { if (str == null | str.length() == 0) { System.out.println("String is empty"); } else { System.out.println("String is not…
Shades88
  • 7,934
  • 22
  • 88
  • 130
38
votes
3 answers

How is NullPointerException in Java 14 different from its predecessor?

One of the important features that were introduced with Java SE 14 was the Helpful NullPointerExceptions which is related to the usability of the NullPointerException. What makes NullPointerException in Java SE 14 more usable than its predecessor?
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
38
votes
7 answers

Android Api 23 Change Navigation View headerLayout textview

I am testing the Navigation Drawer sample project in android and i have a problem setting the text in navigation view profile header. This is my code: MainActivity.java @Override protected void onCreate(Bundle savedInstanceState) { …
user3065901
  • 4,678
  • 11
  • 30
  • 52