Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ArrayList中的NPException?JAVA_Java - Fatal编程技术网

ArrayList中的NPException?JAVA

ArrayList中的NPException?JAVA,java,Java,所以我在运行拼写检查器时不断得到NPException。异常发生在我的“编辑”方法中。下面是该方法的代码: public ArrayList<String> edits (String word){ ArrayList<String> result = new ArrayList<String>(); result.add(word.substring(1)); result.add(word.substring(0, wor

所以我在运行拼写检查器时不断得到NPException。异常发生在我的“编辑”方法中。下面是该方法的代码:

    public ArrayList<String> edits (String word){
    ArrayList<String> result = new ArrayList<String>();

    result.add(word.substring(1));
    result.add(word.substring(0, word.length()-1));
    for (char c = 'a'; c <= 'z'; c++){
        result.add(String.valueOf(c) + word);
    }
    for (char c = 'a'; c <= 'z'; c++){
        result.add(word + String.valueOf(c));
    }

    for (int i = 0; i < word.length()-1; i++){
        char[] c = word.toCharArray();
        char temp = c[i];
        c[i] = c[i+1];
        c[i+1] = temp;
        String swappedword = new String(c);
        result.add(swappedword);
    }
    return result;
}
公共数组列表编辑(字符串字){
ArrayList结果=新建ArrayList();
结果.添加(字.子串(1));
添加(word.substring(0,word.length()-1));
for(char c='a';c
错误发生在第4行,“result.add(word.substring(1));”

然后
word
必须为空。
result
显然不是空的。您应该查看调用此代码的内容,并找出它传入空引用的原因。您可能还需要添加一些验证:

public ArrayList<String> edits (String word) {
    if (word == null) {
        throw new NullPointerException();
    }
    ...
}
公共数组列表编辑(字符串字){
if(word==null){
抛出新的NullPointerException();
}
...
}
这样一来,从堆栈跟踪中就可以立即发现问题所在。当然,这是假设您不想接受空引用——如果您接受,那么只需更改上面的代码以返回您想要的任何内容即可。(我个人要求
word
为非空。)

错误发生在第4行,“result.add(word.substring(1));”

然后
word
必须为空。
result
显然不是空的。您应该查看调用此代码的内容,并找出它传入空引用的原因。您可能还需要添加一些验证:

public ArrayList<String> edits (String word) {
    if (word == null) {
        throw new NullPointerException();
    }
    ...
}
公共数组列表编辑(字符串字){
if(word==null){
抛出新的NullPointerException();
}
...
}
这样一来,从堆栈跟踪中就可以立即发现问题所在。当然,这是假设您不想接受空引用——如果您接受,那么只需更改上面的代码以返回您想要的任何内容即可。(我个人要求
word
为非空。)

NPE的唯一可能性是您的
word
必须为
null

请对此采取必要的步骤,如检查
null
或通过抛出
异常通知
方法
调用方

NPE的唯一可能性是您的
word
必须为
null


请对此采取必要的步骤,如检查
null
或通过抛出
异常通知
方法调用方
在我看来,异常发生是因为您没有检查
word
是否为null。

在我看来,异常发生是因为您没有检查
word
是否为null空。

试试这个

public ArrayList<String> edits (String word){
  if (word == null) { 
    return new ArrayList<String>(); // Or null.
  }
  // as before.
}
公共数组列表编辑(字符串字){
如果(word==null){
返回新的ArrayList();//或null。
}
//和以前一样。
}
试试这个

public ArrayList<String> edits (String word){
  if (word == null) { 
    return new ArrayList<String>(); // Or null.
  }
  // as before.
}
公共数组列表编辑(字符串字){
如果(word==null){
返回新的ArrayList();//或null。
}
//和以前一样。
}

检查您的单词是否为
null

edits("hello");
        edits(""); // fails here java.lang.StringIndexOutOfBoundsException
        edits(null); // fails here  NullPointerException
可能您可以在
edit
方法中添加验证

if(word==null || word.isEmpty()){
            return  Collections.emptyList();
        }

检查您的单词是否为
null

edits("hello");
        edits(""); // fails here java.lang.StringIndexOutOfBoundsException
        edits(null); // fails here  NullPointerException
可能您可以在
edit
方法中添加验证

if(word==null || word.isEmpty()){
            return  Collections.emptyList();
        }