Question: I'm always having nullpointer exception when i added the test method. but when i remove the test method and just print it. It's not empty
I'm always having nullpointer exception when i added the test method.
but when i remove the test method and just print it. It's not empty

MazeSolver class import java.util.LinkedList; import java.io.*;
public class MazeSolver { private char [][] maze ; private static reader maze1, maze2; final static char X='X', V='*';
//=========================================== // object reader //========================================== public static void read()throws FileNotFoundException,IOException{ maze1 = new reader(new FileReader("maze1.txt")); maze2 = new reader(new FileReader("maze2.txt")); } //=================================== // Print //=================================== //maze1 public static void print(){
System.out.println("Maze 1: "); for (int i=0; i
System.out.println(" Maze 2: "); //maze2 for (int i=0; i
//================================ // test //===============================
public static void test(int i, int j){
//maze1 = object
//maze[i][j]=variable from reader if(maze1.maze[i][j]==X){ System.out.print("FOUND X"); }
System.out.print("From test"+maze1.maze[i][j]); } //=========================================== // Main //==========================================
public static void main(String[] args)throws FileNotFoundException, IOException{ test(0,0); read(); print();
}//end main
}//endclass
reader class
import java.io.*; import java.util.*;
public class reader{ static char [][] maze ;
public reader(FileReader newFile) throws FileNotFoundException,IOException { BufferedReader sc= new BufferedReader((newFile)); int row =7; int column =7; //insert text file into array maze = new char [row][column];
for(int i=0; i String line= sc.readLine(); for(int j=0; j maze[i][j]= line.charAt(j); } } }
}
maze1.txt
GOOOOXO XXOXOOX OXOOOXX XXXOOXO XXXXOXX SOOOOOX XXXXXXX
maze2.txt
XOOOOXO XXOXOOG OXOOOXX XXXOOOX XXXXOXX SOOOOOX XXXXXXX
also maze1 is not showing up right, but maze2 is correct
Exception in thread "main" java.lang.NullPointerException at MazeSolver.test(MazeSolver.java:47) at MazeSolver.main(MazeSolver.java:57) OGXXXXX OGXXXXX XOXOXOX XOXOXOX OOOXXOX OXXXXOX XXOXXSX OOOXXOX OXXXXOX XXOXXSX
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
