Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
Hello people,
I found a program on internet that reads 2 text files "maze.txt" and "randomMaze.txt" in which we can find *, space, S (for start) and E (for end). It resolves this maze by using DFS algorithm.
My problem is that it does not run, it says there is a NullPoint exception error. Can you fix for me the problem please ? Here is the code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.Random;
class Maze extends JFrame
{
public static void main(String args[])
{
Maze f= new Maze();
f.setBounds(150,50,500,500);
f.setVisible(true);
}
int maze[][]; //save the maze
Point mouse=new Point(); //the position of the mouse
jpMainPanel mp=new jpMainPanel(); //the firstMaze panel
jpRandomPanel rp=new jpRandomPanel(); //the randomMaze panel
JButton jbDFS=new JButton("DFS"); //the Buttons
JButton jbBFS=new JButton("BFS");
JButton jbASTAR=new JButton("A*");
JButton jbNewState=new JButton("New State");
JButton jbIDS=new JButton("IDS");
JLabel jlPart1=new JLabel("Part I"); //the Labels
JLabel jlPart2=new JLabel("Part II");
Random rand;
public Maze() //the constructor of maze class
{
super("AI assignment");
JPanel jpCtrlBar=new JPanel(); //GUI setting
jpCtrlBar.setLayout(new FlowLayout());
jpCtrlBar.add(jlPart1);
jpCtrlBar.add(jbDFS);
jpCtrlBar.add(jbBFS);
jpCtrlBar.add(jbASTAR);
jpCtrlBar.add(jlPart2);
jpCtrlBar.add(jbNewState);
jpCtrlBar.add(jbIDS);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(mp, BorderLayout.CENTER);
getContentPane().add(jpCtrlBar, BorderLayout.SOUTH);
mp.setBackground(Color.WHITE);
//set the listener
this.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent evt) {
mp=null;
System.exit(0);
}
}
);
jbBFS.addActionListener(
new ActionListener()
{ public void actionPerformed(ActionEvent event)
{ jbBFSActionPerformed(event);
}
}
);
jbDFS.addActionListener(
new ActionListener()
{ public void actionPerformed(ActionEvent event)
{ jbDFSActionPerformed(event);
}
}
);
jbASTAR.addActionListener(
new ActionListener()
{ public void actionPerformed(ActionEvent event)
{ jbASTARActionPerformed(event);
}
}
);
jbIDS.addActionListener(
new ActionListener()
{ public void actionPerformed(ActionEvent event)
{ jbIDSActionPerformed(event);
}
}
);
jbNewState.addActionListener(
new ActionListener()
{ public void actionPerformed(ActionEvent event)
{ jbNewStateActionPerformed(event);
}
}
);
initGame();
}
public void initGame() //load firstMaze
{
getMaze();
getContentPane().add(mp, BorderLayout.CENTER);
mp.setMaze(maze);
mp.setMouse(mouse);
jbDFS.setEnabled(true);
jbBFS.setEnabled(true);
jbASTAR.setEnabled(true);
jbIDS.setEnabled(true);
jbNewState.setEnabled(true);
mp.setVisible(true);
rp.setVisible(false);
mp.repaint();
}
public void initRandomGame() //load second random Maze
{ getRandomMaze();
getContentPane().add(rp, BorderLayout.CENTER);
rp.setBackground(Color.WHITE);
rp.setMaze(maze);
rp.setMouse(mouse);
jbDFS.setEnabled(true);
jbBFS.setEnabled(true);
jbASTAR.setEnabled(true);
jbIDS.setEnabled(true);
jbNewState.setEnabled(true);
rp.repaint();
}
public void getMaze() //get the maze from the text file
{
String s[]=new String[20];
int line_num=0;
try{
BufferedReader br=new BufferedReader(new FileReader("maze.txt"));
for (int i=0;i<s.length;i++){
s[i]=br.readLine();
if (s[i] !=null)
line_num++;
else break;
}
}catch (Exception e){}
maze=new int[line_num+2][s[0].length()+2];
for (int i=0; i<maze.length; i++)
for (int j=0; j<maze[0].length; j++)
{
if (i==0 || i==line_num+1)
maze[i][j]=1; //up and down
else if (j==0 || j==s[0].length()+1)
maze[i][j]=1; //right and left
else if (s[i-1].charAt(j-1)=='*')
maze[i][j]=1; //wall
else if (s[i-1].charAt(j-1)=='S'){ //starter
mouse.setLocation(j,i);
maze[i][j]=10;
}
else if (s[i-1].charAt(j-1)=='E')
maze[i][j]=11; //end
else maze[i][j]=0; //road
}
}
public void getRandomMaze() //get maze from another text file
{ String s[]=new String[20];
int line_num=0;
Random rand = new Random();
int n =0, m=0;
try{
BufferedReader br=new BufferedReader(new FileReader("randomMaze.txt"));
for (int i=0;i<s.length;i++){
s[i]=br.readLine();
if (s[i] !=null)
line_num++;
else break;
}
}catch (Exception e){}
maze=new int[line_num+2][s[0].length()+2];
for (int i=0; i<maze.length; i++)
for (int j=0; j<maze[0].length; j++)
{
if (i==0 || i==line_num+1)
maze[i][j]=1; //up and down
else if (j==0 || j==s[0].length()+1)
maze[i][j]=1; //right and left
else if (s[i-1].charAt(j-1)=='*')
maze[i][j]=1; //wall
//else if (s[i-1].charAt(j-1)=='S'){ //starter
// mouse.setLocation(j,i);
// maze[i][j]=10;
//}
//else if (s[i-1].charAt(j-1)=='E')
// maze[i][j]=11; //end
else maze[i][j]=0; //road
}
do{
m = rand.nextInt(10);
n = rand.nextInt(10);
//System.out.println(m+1);
//System.out.println(n+1);
}
while (s[m].charAt(n)=='*' || s[n].charAt(m)=='*');
mouse.setLocation(n+1,m+1);
maze[m+1][n+1]=10;
do{
m = rand.nextInt(10);
n = rand.nextInt(10);
//System.out.println(m+1);
//System.out.println(n+1);
}
while (s[m].charAt(n)=='*' || s[n].charAt(m)=='*');
maze[m+1][n+1]=11;
}
public void jbDFSActionPerformed(ActionEvent event)
{ initGame();
DepthFirstSearch dfs=new DepthFirstSearch();
jbDFS.setEnabled(false);
jbBFS.setEnabled(false);
jbASTAR.setEnabled(false);
jbIDS.setEnabled(false);
jbNewState.setEnabled(false);
dfs.start();
}
public void jbBFSActionPerformed(ActionEvent event)
{ initGame();
BreadthFirstSearch bfs=new BreadthFirstSearch();
jbDFS.setEnabled(false);
jbBFS.setEnabled(false);
jbASTAR.setEnabled(false);
jbIDS.setEnabled(false);
jbNewState.setEnabled(false);;
bfs.start();
}
public void jbASTARActionPerformed(ActionEvent event)
{ initGame();
ASTAR astar=new ASTAR();
jbDFS.setEnabled(false);
jbBFS.setEnabled(false);
jbASTAR.setEnabled(false);
jbIDS.setEnabled(false);
jbNewState.setEnabled(false);
astar.start();
}
public void jbIDSActionPerformed(ActionEvent event)
{ IterativeDeepSearch ids=new IterativeDeepSearch();
jbDFS.setEnabled(false);
jbBFS.setEnabled(false);
jbASTAR.setEnabled(false);
jbIDS.setEnabled(false);
jbNewState.setEnabled(false);
ids.start();
}
public void jbNewStateActionPerformed(ActionEvent event)
{ NewState newstate=new NewState();
mp.setVisible(false);
initRandomGame();
rp.setVisible(true);
jbDFS.setEnabled(false);
jbBFS.setEnabled(false);
jbASTAR.setEnabled(false);
jbIDS.setEnabled(false);
jbNewState.setEnabled(false);
newstate.start();
}
class BreadthFirstSearch extends Thread //BFS class
{
public void run()
{ findExit();
}
public boolean findExit()
{ //till the end
if (maze[mouse.y][mouse.x]==11){
JOptionPane.showMessageDialog((Component)null," ^-^ Reach the goal","Game Over",JOptionPane.INFORMATION_MESSAGE);
initGame();
return true;
}
maze[mouse.y][mouse.x]=50; //mark has visited
//delay
try{
Thread.sleep(500);
}
catch (Exception e){}
//right
if (step(1,0))
if (findExit()==true)
return true;
else mouse.x--;
up
if (step(0,-1))
if (findExit()==true)
return true;
else mouse.y++;
//down
if (step(0,1))
if (findExit()==true)
return true;
else mouse.y--;
//can not find the exit
maze[mouse.y][mouse.x]=51; //mark it is not through
return false;
}
public boolean step(int x,int y)
{
int target=maze[mouse.y+y][mouse.x+x];
if (target ==0 || target ==11 || target ==10){
mouse.x+=x;
mouse.y+=y;
repaint();
return true;
}
else
return false;
}
}
class DepthFirstSearch extends Thread //DFS class
{
public void run()
{
findExit();
}
public boolean findExit()
{
//till the end
if (maze[mouse.y][mouse.x]==11){
JOptionPane.showMessageDialog((Component)null," ^-^ Reach the goal","Game Over",JOptionPane.INFORMATION_MESSAGE);
initGame();
return true;
}
maze[mouse.y][mouse.x]=50; //mark has visited
//delay
try{
Thread.sleep(500);
}
catch (Exception e){}
//down
if (step(0,1))
if (findExit()==true)
return true;
else mouse.y--;
//right
if (step(1,0))
if (findExit()==true)
return true;
else mouse.x--;
//can not find the exit
maze[mouse.y][mouse.x]=51; //mark it is not through
return false;
}
public boolean step(int x,int y)
{
int target=maze[mouse.y+y][mouse.x+x];
if (target ==0 || target ==11 || target ==10){
mouse.x+=x;
mouse.y+=y;
repaint();
return true;
}
else
return false;
}
}
class IterativeDeepSearch extends Thread //IDS class
{
public void run()
{
findExit();
}
public boolean findExit()
{
//till the end
if (maze[mouse.y][mouse.x]==11){
JOptionPane.showMessageDialog((Component)null," ^-^ Reach the goal","Game Over",JOptionPane.INFORMATION_MESSAGE);
initRandomGame();
return true;
}
maze[mouse.y][mouse.x]=50; //mark has visited
//delay
try{
Thread.sleep(500);
}
catch (Exception e){}
//left
if (step(-1,0))
if (findExit()==true)
return true;
else mouse.x++;
//right
if (step(1,0))
if (findExit()==true)
return true;
else mouse.x--;
//up
if (step(0,-1))
if (findExit()==true)
return true;
else mouse.y++;
//down
if (step(0,1))
if (findExit()==true)
return true;
else mouse.y--;
//can not find the exit
maze[mouse.y][mouse.x]=51; //mark it is not through
return false;
}
public boolean step(int x,int y)
{
int target=maze[mouse.y+y][mouse.x+x];
if (target ==0 || target ==11 || target ==10){
mouse.x+=x;
mouse.y+=y;
repaint();
return true;
}
else
return false;
}
}
class ASTAR extends Thread //A* search class
{
public void run()
{ findExit();
}
public boolean findExit()
{ //till the end
if (maze[mouse.y][mouse.x]==11){
JOptionPane.showMessageDialog((Component)null," ^-^ Reach the goal","Game Over",JOptionPane.INFORMATION_MESSAGE);
initGame();
return true;
}
maze[mouse.y][mouse.x]=50; //mark has visited
//delay
try{
Thread.sleep(500);
}
catch (Exception e){}
//right
if (step(1,0))
if (findExit()==true)
return true;
else mouse.x--;
//down
if (step(0,1))
if (findExit()==true)
return true;
else mouse.y--;
//can not find the exit
maze[mouse.y][mouse.x]=51; //mark it is not through
return false;
}
public boolean step(int x,int y)
{
int target=maze[mouse.y+y][mouse.x+x];
if (target ==0 || target ==11 || target ==10){
mouse.x+=x;
mouse.y+=y;
repaint();
return true;
}
else
return false;
}
}
class NewState extends Thread //get a new state and implement a DFS
{
public void run()
{
findExit();
}
public boolean findExit()
{ //till the end
if (maze[mouse.y][mouse.x]==11){
JOptionPane.showMessageDialog((Component)null," ^-^ Reach the goal","Game Over",JOptionPane.INFORMATION_MESSAGE);
initRandomGame();
return true;
}
maze[mouse.y][mouse.x]=50; //mark has visited
//delay
try{
Thread.sleep(500);
}
catch (Exception e){}
//down
if (step(0,1))
if (findExit()==true)
return true;
else mouse.y--;
//right
if (step(1,0))
if (findExit()==true)
return true;
else mouse.x--;
//left
if (step(-1,0))
if (findExit()==true)
return true;
else mouse.x++;
//up
if (step(0,-1))
if (findExit()==true)
return true;
else mouse.y++;
//can not find the exit
maze[mouse.y][mouse.x]=51; //mark it is not through
return false;
}
public boolean step(int x,int y)
{
int target=maze[mouse.y+y][mouse.x+x];
if (target ==0 || target ==11 || target ==10){
mouse.x+=x;
mouse.y+=y;
repaint();
return true;
}
else
return false;
}
}
class jpMainPanel extends JPanel //the first Maze panel
{
int maze[][];
Point mouse;
///let the main draw gui program get the maze setting
public void setMaze(int arry[][])
{
this.maze=arry;
}
///let the main draw gui program get the mouse position
public void setMouse(Point arg)
{
mouse=arg;
}
///main draw gui program
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int stuff=100;
//draw the maze
for (int i=1;i<maze.length-1;i++)
for (int j=1;j<maze[i].length-1;j++){
if (maze[i][j]==1){
//wall
g.drawString("*",j*20+stuff,i*20+stuff);
}
else if (maze[i][j]==0){
//road
}
else if (maze[i][j]==10){
//starter
}
else if (maze[i][j]==11){
//end
g.setColor(new Color(0,255,0));
g.drawString("O",j*20+stuff,i*20+stuff);
g.setColor(new Color(0,0,0));
}
else{}
}
//draw the position of the mouse
g.setColor(new Color(0,0,255));
g.drawString("O",mouse.x*20+stuff,mouse.y*20+stuff);
g.setColor(new Color(0,0,0));
}
}
class jpRandomPanel extends JPanel //the second maze panel
{
int maze[][];
Point mouse;
///let the main draw gui program get the maze setting
public void setMaze(int arry[][])
{
this.maze=arry;
}
///let the main draw gui program get the mouse position
public void setMouse(Point arg)
{
mouse=arg;
}
///main draw gui program
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int stuff=100;
//draw the maze
for (int i=1;i<maze.length-1;i++)
for (int j=1;j<maze[i].length-1;j++){
if (maze[i][j]==1){
//wall
g.drawString("*",j*20+stuff,i*20+stuff);
}
else if (maze[i][j]==0){
//road
}
else if (maze[i][j]==10){
//starter
}
else if (maze[i][j]==11){
//end
g.setColor(new Color(0,255,0));
g.drawString("O",j*20+stuff,i*20+stuff);
g.setColor(new Color(0,0,0));
}
else{}
}
//draw the position of the mouse
g.setColor(new Color(0,0,255));
g.drawString("O",mouse.x*20+stuff,mouse.y*20+stuff);
g.setColor(new Color(0,0,0));
}
}
}No. No one wants to "fix for you" some code you copied off the internet.
If you post the exact error message, which will include the line on which the error is occurring, perhaps someone will help you solve the issue yourself.
Here's how to copy from the command prompt console:
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.