0
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)

2 Answers2

1

You declare rowWords and colWords but never assign anything to them. This means they will hold the default value of null, and any attempt to access array elements is going to fail.

I don't know how many elements you need to store in rowWords and colWords, but if 100 is enough you can initialize them with:

 public MazeAH(String [] key, String [][] wordBank)
   {
      maze = wordBank;
      answer = key; 
      rowWords = new String[100];
      colWords = new String[100];
   }
Joni
  • 108,737
  • 14
  • 143
  • 193
0

"The error is on the line of the second for loop", clearly refers to the line;

for(int a=0;a<list.length;a++)

[Although stack trace to support this statement is missing. Try to print the correct most meaningful part of the stack trace in your next question.]

Now, if this is throwing a NullPointerException, it means at run time of your code, the value of list is null.

You can easily check this by writing a System.out.println(line); before your loop.

Based on the little code you have shared, I can only say that this is a result of you sending a null value as rowWords in you traverseRowLR call. Perhaps you are never initializing the variable rowWords

I hope this helps!

Shivam Puri
  • 1,578
  • 12
  • 25