gpt4 book ai didi

java - 线程 "main"java.lang.NullPointerException : program compiles fine, 中出现异常,但运行时显示错误

转载 作者:行者123 更新时间:2023-12-02 08:49:16 26 4
gpt4 key购买 nike

public void traverseRowLR(String [] list, String [] key, String pool)
{
pool = "left to right";
ArrayList<Location> obj = new ArrayList<Location>();
for(int i = 0; i<key.length; i++)
{
for(int a=0;a<list.length;a++)
if(list[a].indexOf(key[i])>-1)
finalList.add(new Location(key[i],a+1,list[a].indexOf(key[i]),pool));
}

}

上面就是方法了。错误出现在第二个 for 循环的行上。

这里提到了错误的另一个地方:

 public MazeAH(String [] key, String [][] wordBank)
{
maze = wordBank;
answer = key;
}

//other methods

public ArrayList<Location> solve()
{
mergeRow(maze, rowWords);
mergeCol(maze, colWords);
traverseRowLR(rowWords, answer, horizontal);
traverseRowRL(rowWords, answer, horizontal);
traverseRowTB(colWords, answer, vertical);
traverseRowBT(colWords, answer, vertical);

return finalList;
}

错误所在行:

traverseRowLR(rowWords, answer, horizontal);

最后,这是驱动程序,抱歉,如果我发布了太多代码,我不确定需要什么:该程序的目标是将行和列合并到字符串中,然后在“关键”词中搜索单词银行。我最初使用非 void 方法完成了这个项目,但我的教授希望我使用 void 方法重做它。

import java.util.*;

public class SolveWordSearchAH
{

public static void main(String [] args)
{

String [][] letters = {{"e","p","l","i","r","q","k","o","s","a"},
{"u","k","i","e","s","y","x","c","n","f"},
{"c","p","o","z","g","f","q","a","y","r"},
{"p","e","m","c","z","a","n","t","e","i"},
{"g","u","g","q","t","a","b","r","h","e"},
{"g","i","q","v","b","e","d","b","s","s"},
{"c","o","f","f","e","e","i","l","r","q"},
{"h","r","e","t","a","w","b","d","e","l"},
{"y","q","o","k","w","v","p","x","h","z"},
{"c","d","n","t","f","q","k","b","p","w"}};

String [] key = {"coffee", "water", "taco", "hershey", "fries"};
//can have another key array if needed-depends on your implementation

MazeAH m=new MazeAH(key, letters);
ArrayList<Location> answer=m.solve();
System.out.println(answer);

}
}

我的变量在这里声明:

private ArrayList<Location> finalList = new ArrayList<Location>();
private String [][] maze;
private String [] answer;
private String vertical="";
private String horizontal = "";
private String [] rowWords;
private String [] colWords;

这是编译日志中的错误:

Exception in thread "main" java.lang.NullPointerException
at MazeAH.mergeRow(MazeAH.java:117)
at MazeAH.solve(MazeAH.java:26)
at SolveWordSearchAH.main(SolveWordSearchAH.java:24)

这里合并行:

 public void mergeRow(String[][] letters, String [] words)
{
String []temp = new String [letters.length];
for(int i=0; i<letters.length; i++)
{
temp[i]="";
for(int a=0; a<letters[0].length; a++)
{
temp[i]+=letters[i][a];
}
}
for(int s = 0; s<temp.length; s++)
{
temp[s] = words[s];
}

}

这是更新后的跟踪:

Exception in thread "main" java.lang.NullPointerException
at MazeAH.mergeRow(MazeAH.java:117)
at MazeAH.solve(MazeAH.java:26)
at SolveWordSearchAH.main(SolveWordSearchAH.java:24)

最佳答案

您声明 rowWords 和 colWords 但从未向它们分配任何内容。这意味着它们将保留默认值 null,并且任何访问数组元素的尝试都将失败。

我不知道您需要在 rowWords 和 colWords 中存储多少个元素,但如果 100 个就足够了,您可以使用以下方法初始化它们:

 public MazeAH(String [] key, String [][] wordBank)
{
maze = wordBank;
answer = key;
rowWords = new String[100];
colWords = new String[100];
}

关于java - 线程 "main"java.lang.NullPointerException : program compiles fine, 中出现异常,但运行时显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60891835/

26 4 0
文章推荐: java - 读取一个单词并打印该单词中元音的数量。 Java/Netbeans
文章推荐: java - 如何根据List java中的分数获取前7名