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

让人想犯罪 __ 提交于 2020-01-13 03:53:09

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/20035122/null-pointer-access-the-variable-can-only-be-null-at-this-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!