java - 线程“main”java.lang.NullPointerException 中的异常:程序编译正常,但运行时显示错误
问题描述
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 - Spring 外部化配置 yml
- email - IMAP:在一个命令中返回多个标头
- c# - 如何跟踪哪一行引发了异常
- office365 - 无法在 Office 365 中自动加载已部署的加载项
- angular - 如何将包用作插件 - Angular
- css - 如何使用 SASS 覆盖引导程序“btn-primary”活动背景颜色?
- python - Pandas:对各种列执行操作并创建、重命名新列
- c# - C#:Outlook 集成插件
- ruby - Puppet:安装 gem 包时如何重新加载环境 - rbenv 版本未激活
- openshift - openshift how imagestream track the image changes?