Follow Us
Contribute
  • Register

Java.lang.NullPointerException

0 votes

This error message is appeared in Java when a null object has been tried to access or modify. This is one of the most common exceptions in Java.
String p = null;
p.charAt(0);

or

String[] strArray = new String[5];
strArray[0].charAt(0);

This kind of usage returns java.lang.NullPointerException.

requested 1 year ago by errorbase (169,970 points)
edited 1 year ago by onur

1 Solution

0 votes

In order to solve the problem you must find the line that causes this exception. Java shows the line number of the error in the error message.
Exception in thread "main" java.lang.NullPointerException
at SomePackage.SomeClass.main(SomeClass.java:6)

In most of the IDEs (Integrated Development Environment) you can click the line number and reach the error immediately. By this way you can see which variable is null and you can initialize that variable. Of course this is not the only solution for this exception, since there are lots of causes for java.lang.NullPointerException, however most of the problems can be solved by this approach.

solved 1 year ago by errorbase (169,970 points)
edited 1 year ago by onur

Related errors