Access to null pointer: a variable can only be null in this place

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));
}

An exception:

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

why does this code throw a null pointer exception

warning: null access pointer: strSet1 variable can only be null in this place Access to null pointer: strSet2 variable can only be null in this place

+4
source share
1 answer

Are strSet1and strSet2initialized before that? If they are zero, you will get NullPointerException.

* EDIT *

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

StringBuffer strSet1 = new StringBuffer();
StringBuffer strSet2 = new StringBuffer();
+7
source

All Articles