Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null pointer access: The variable can only be null at this location

for(int i=0;i<n;i++){
  for(int j=0;j<26;j++){
    if(str.charAt(i)== strChar.charAt(j) )
    * strSet1.append(str.charAt(i));
  }
    * strSet2.append(str.charAt(i));
}

Exception:

Exception in thread "main" java.lang.NullPointerException
  at AterSeries.main(AterSeries.java:33)

why this code gives null pointer exception

warning: Null pointer access: The variable strSet1 can only be null at this location Null pointer access: The variable strSet2 can only be null at this location

like image 513
sky Avatar asked Nov 17 '13 19:11

sky


People also ask

WHAT IS NULL pointer access?

In computing, a null pointer or null reference is a value saved for indicating that the pointer or reference does not refer to a valid object.

WHAT IS NULL pointer access in Java?

NullPointerException is a runtime exception in Java that occurs when a variable is accessed which is not pointing to any object and refers to nothing or null. Since the NullPointerException is a runtime exception, it doesn't need to be caught and handled explicitly in application code.


1 Answers

Are strSet1 and strSet2 initialized before this? If they are null, you'd get a NullPointerException.

* EDIT *

You cannot call .append() (or any other method) on a variable that is null. Initialize them as:

StringBuffer strSet1 = new StringBuffer();
StringBuffer strSet2 = new StringBuffer();
like image 153
t0mppa Avatar answered Sep 27 '22 18:09

t0mppa