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));
}
}
That above is the method. The error is on the line of the second for loop.
Heres another place the error is mentioned:
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;
}
The error is in the line:
traverseRowLR(rowWords, answer, horizontal);
Finally, heres the driver program, sorry if im posting too much code, im not sure what's necessary: The goal of this program is to merge rows and columns together into Strings, and then search for words in the "key" word bank. I originally completed this project using non-void methods, but my professor wants me to redo it with void methods.
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);
}
}
My variables are declared here:
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;
Here is the error in the compile log:
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)
Heres MergeRow:
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];
}
}
Heres the updated trace:
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)