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
28
votes
4 answers

NullPointerException on list.add

I am getting a NullPointerException at the modelData.add(i, es) method. I know from debugging that es isn't null. I'm really confused, thanks. public class EventTableModel extends AbstractTableModel { //private int rowCount = 0; protected…
novicePrgrmr
  • 18,647
  • 31
  • 81
  • 103
28
votes
8 answers

Glassfish server does not start. NullPointeException

I just downloaded the GlassFish 5.0 archive - Full Platform, unzipped it, I run it through the command line. asadmin start-domain problem: Exception in thread "main" java.lang.NullPointerException at…
Oleksandr
  • 450
  • 1
  • 6
  • 13
28
votes
3 answers

Java: null pointer exception when unboxing Integer?

This code is causing a null pointer exception. I have no idea why: private void setSiblings(PhylogenyTree node, Color color) throws InvalidCellNumberException { PhylogenyTree parent = node.getParent(); for (PhylogenyTree sibling :…
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
28
votes
5 answers

Catching nullpointerexception in Java

I tried using try-catch block to catch NullPointerException but still the following program is giving errors. Am I doing something wrong or is there any other way to catch NullPointerException in the following program. Any help is highly…
jags
  • 527
  • 3
  • 6
  • 15
28
votes
9 answers

Java HashMap get method null pointer exception

My code is similar to the following: public class A { private HashMap myMap; public A() { myMap = new HashMap(); String mychars = "asdfzxcvqwer"; for (char…
user277465
27
votes
6 answers

NullPointerException while using put method of HashMap

The following code is giving me a NullPointerException. The problem is on the following line: ... dataMap.put(nextLine[0], nextLine[6]); What is strange is that I have run this code without the above line and the call to nextLine[0] and…
Ankur
  • 50,282
  • 110
  • 242
  • 312
27
votes
14 answers

Java: How to check for null pointers efficiently

There are some patterns for checking whether a parameter to a method has been given a null value. First, the classic one. It is common in self-made code and obvious to understand. public void method1(String arg) { if (arg == null) { throw new…
Roland Illig
  • 40,703
  • 10
  • 88
  • 121
27
votes
11 answers

How to trace a NullPointerException in a chain of getters

If I get a NullPointerException in a call like this: someObject.getSomething().getSomethingElse(). getAnotherThing().getYetAnotherObject().getValue(); I get a rather useless exception text like: Exception in thread "main"…
Lena Schimmel
  • 7,203
  • 5
  • 43
  • 58
27
votes
7 answers

Why does Java compiler allow static variable access through null object?

I was pointing some tricks and came across this. In following code: public class TestClass1 { static int a = 10; public static void main(String ar[]){ TestClass1 t1 = null ; System.out.println(t1.a); // At this line …
Not a bug
  • 4,286
  • 2
  • 40
  • 80
26
votes
4 answers

NullPointerException with autoboxing in ternary expression

Run the following Java code: boolean b = false; Double d1 = 0d; Double d2 = null; Double d = b ? d1.doubleValue() : d2; Why is there a NullPointerException?
Alexander
  • 482
  • 1
  • 5
  • 14
26
votes
12 answers

Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

The problem is as follows. I have a login activity (in Android Studio) which worked fine a few days before. I don't remember changing anything but when I run this one the previous time the app closed right after I clicked the login button. The last…
Olesya
  • 272
  • 1
  • 3
  • 8
26
votes
2 answers

Why does this assignment cause NPE?

public class Npe { static class Thing { long value; } public static Map map; public static void main(String[] args) { Thing thing = new Thing(); method(null); // returns -1 …
David Wasser
  • 93,459
  • 16
  • 209
  • 274
26
votes
4 answers

String.split returning null when using a dot

I got this simple code: String ip = "1.2.3.4"; String[] ipArray = ip.split("."); System.out.println(ipArray[1]); And ipArray is null by the time it hits System.out.println (throws null pointer exception). My question is why does ipArray stay null…
CyanPrime
  • 5,096
  • 12
  • 58
  • 79
26
votes
2 answers

java.lang.NullPointerException: println needs a message

I get the errror: java.lang.NullPointerException: println needs a message when I call this method: lst_info = new HashMap(); SystemDatabaseHandler db = new SystemDatabaseHandler(getApplicationContext()); lst_info =…
Laire
  • 1,290
  • 6
  • 22
  • 49
26
votes
7 answers

How to add a SearchWidget to the ActionBar?

I'm trying to add a Search-ActionView to my application (as explained here http://developer.android.com/guide/topics/search/search-dialog.html#UsingSearchWidget). Unfortunately I keep getting a NullPointerException and I'm having a hard time…
Taig
  • 6,718
  • 4
  • 44
  • 65