Results 1 to 4 of 4
  1. #1
    jijiaremere is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default Maze Solver Error in Getting instance variable

    Hello everyone,

    I have been programming in Java for some time and my task now is to create a program that will create a double dimensional array from a txt file which will represent a maze and then solve it.
    I created some instance methods and 2 objects:
    ->MazeSolve<- being the actual maze
    ->Cell<- Which represents a single cell with it's coordinates, values, and symbol


    This is the cell
    Java Code:
    public class Cell
    {
    	private int value;
    	private int column;
    	private int row;
    	private char symbol;
    	private int nuValue;
    	
    	private static final int SPACE = 1;
    	private static final int HEDGE =2;
    	private static final int ENTRANCE = 3;
    	private static final int EXIT = 4;
    	
    	public Cell(int columnTo, int rowTo, char symbolTo, int valueTo)
    	{
    		column = columnTo;
    		row = rowTo;
    		symbol = symbolTo;
    		value = valueTo;
    		
    		if(symbol == '#')
    		{
    			nuValue = HEDGE;
    			
    		}else if(symbol == ' ')
    		{
    			nuValue = SPACE;
    		}else if(symbol == '?')
    		{
    			nuValue = ENTRANCE;
    		}else if(symbol == '!')
    		{
    			
    			nuValue = EXIT;
    		}
    		
    	}// constructor
    	
    	public int value()
    	{
    		return value;
    		
    	}
    	public int nuValue()
    	{
    		return nuValue;
    	}
    	public char symbol()
    	{
    		return symbol;
    	}
    	public int column()
    	{
    		return column;
    	}//column
    	public int row()
    	{
    		return row;
    	}//row
    	
    	public String toString()
    	{
    		return symbol + "";
    		
    	}
    	
    	
    }//class
    And the maze

    Java Code:
    import java.util.Scanner;
    import java.io.File;
    
    public class MazeSolve
    {
    	private static final String NLS = System.getProperty("line.separator");
    
    	private static final int INITIAL_ARRAY_SIZE = 1;
    	private static final int ARRAY_RESIZE_FACTOR = 1;
    	
    	private static final int SPACE = 1;
    	private static final int HEDGE =2;
    	private static final int ENTRANCE = 3;
    	private static final int EXIT = 4;
    	
    	private static final char SPACE_REP = ' ';
    	private static final char HEDGE_REP = '#';
    	private static final char ENTRANCE_REP = '?';
    	private static final char EXIT_REP = '!';
    	
    	private static final int WIDTH = 20;
    	private static final int HEIGHT = 10;
    	
    	private Cell[][] maze = new Cell[WIDTH][HEIGHT];
    	
    	
    	public static void main(String[] args) throws Exception
    	{
    		for(String filename : args)
    		{
    			System.out.println(new MazeSolve(new Scanner(new File(filename))));
    		}
    	}
    		
    	public void solve()
    	{
    		Cell[] entrance = findCellWithValue(250);
    		Cell[] neighborOf = findNeighbors(entrance[0]);
    		setTheValue(1,neighborOf[0]);
    		boolean foundExit = false;
    
    		int moveCount = 1;
    		while(!foundExit && moveCount < WIDTH * HEIGHT)
    		{
    			Cell[] theCells = findCellWithValue(moveCount);
    			for(Cell cell:theCells)
    			{
    				Cell[] neighbors = findNeighbors(cell);
    				for(Cell oneNeighbor:neighbors)
    				{
    					if(oneNeighbor.symbol() == '!')
    				{
    					foundExit = true;
    					markPath(moveCount);
    				}else
    				{	
    					 if(oneNeighbor.value() == 0)
    					setTheValue(moveCount+1,oneNeighbor);
    					else;
    				}
    				}
    
    			}
    			moveCount++;
    		}
    		System.out.println(moveCount + " ");
    
    
    	}
    
    	public void markPath(int moveCount)
    	{
    		System.out.println("Found  " + moveCount);
    	}
    	public void setTheValue(int value, Cell someCell)
    	{
    		int column = someCell.column();
    		int row = someCell.row();
    		char symbol = someCell.symbol();
    		maze[column][row] = new Cell(column, row, symbol, value);
    	}
    	
    	public Cell[] findCellWithValue(int theValue)
    	{
    		int currentNoOfCells = 0;
    		Cell[] theCells = new Cell[INITIAL_ARRAY_SIZE];
    			for(int row = 0; row < HEIGHT; row++)
    			{
    				for(int column = 0; column < WIDTH; column++)
    				{
    					if(maze[column][row].value() == theValue)
    					{
    						if(currentNoOfCells == theCells.length)
    						{
    							Cell[] biggerArray = new Cell[theCells.length + ARRAY_RESIZE_FACTOR];
    							for(int index = 0; index < theCells.length; index++)
    							{
    								biggerArray[index] = theCells[index];
    							}
    							theCells = biggerArray;
    							
    						}
    						theCells[currentNoOfCells] = maze[column][row];
    						currentNoOfCells++;
    						
    					}
    					
    				}
    				
    			}
    		return theCells;
    	}
    	
    	public Cell[] findNeighbors(Cell theCell)
    		{
    			Cell neighborsCell[] = new Cell[1];
    			int neighbors = 0;
    			int column = theCell.column();
    			int row = theCell.row();
    			try{
    			if(maze[column-1][row].symbol() == ' ')
    			{
    				if(neighborsCell.length == neighbors)
    				{
    					Cell biggerArray[] = new Cell[neighborsCell.length + 1];
    					for(int index = 0; index < neighborsCell.length; index++ )
    					{
    						biggerArray[index] = neighborsCell[index];
    					}
    					neighborsCell = biggerArray; 
    				}
    					
    					neighborsCell[neighbors] = maze[column-1][row];
    					neighbors++;
    				
    				}
    			}
    			catch(Exception exception)
    			{}
    			try{
    			if(maze[column+1][row].symbol() == ' ')
    			{
    				if(neighborsCell.length == neighbors)
    				{
    					Cell biggerArray[] = new Cell[neighborsCell.length + 1];
    					for(int index = 0; index < neighborsCell.length; index++ )
    					{
    						biggerArray[index] = neighborsCell[index];
    					}
    					neighborsCell = biggerArray; 
    				}
    					
    					neighborsCell[neighbors] = maze[column+1][row];
    					neighbors++;
    			}
    		}
    		catch(Exception exception)
    		{}
    		try{
    			if(maze[column][row-1].symbol() == ' ')
    			{
    				if(neighborsCell.length == neighbors)
    				{
    					Cell biggerArray[] = new Cell[neighborsCell.length + 1];
    					for(int index = 0; index < neighborsCell.length; index++ )
    					{
    						biggerArray[index] = neighborsCell[index];
    					}
    					neighborsCell = biggerArray; 
    				}
    					
    					neighborsCell[neighbors] = maze[column][row-1];
    					neighbors++;
    			}
    		}
    		catch(Exception exception)
    		{}
    		try{
    			if(maze[column][row+1].symbol() == ' ')
    			{
    				if(neighborsCell.length == neighbors)
    				{
    					Cell biggerArray[] = new Cell[neighborsCell.length + 1];
    					for(int index = 0; index < neighborsCell.length; index++ )
    					{
    						biggerArray[index] = neighborsCell[index];
    					}
    					neighborsCell = biggerArray; 
    				}
    					
    					neighborsCell[neighbors] = maze[column][row+1];
    					neighbors++;
    				}
    			}
    			catch(Exception exception)
    			{}
    			return neighborsCell;
    		}
    
    	
    		public MazeSolve(Scanner input)
    		{
    			
    			for(int row = 0; row < HEIGHT; row++)
    			{
    				String mazeLine = input.nextLine();
    				for(int column = 0; column < WIDTH; column++)
    				{
    					
    					char character = mazeLine.charAt(column);
    					switch(character)
    					{
    					case SPACE_REP: maze[column][row] = new Cell(column, row, SPACE_REP, 0);break;
    					case HEDGE_REP: maze[column][row] = new Cell(column, row, HEDGE_REP, 0);break;
    					case ENTRANCE_REP: maze[column][row] = new Cell(column, row, ENTRANCE_REP, 250);break;
    					case EXIT_REP: maze[column][row] = new Cell(column, row, EXIT_REP, 0);break;
    					
    					}//switch
    				}//for
    				}//for
    			
    			solve();			
    			}//constructor
    		
    		public String toString()
    		{
    			String result = "";
    			for(int row = 0; row < HEIGHT; row++)
    			{
    				for(int column = 0; column < WIDTH; column++)
    					switch(maze[column][row].nuValue())
    					{
    					case HEDGE: result += HEDGE_REP; break;
    					case SPACE: result += SPACE_REP; break;
    					case ENTRANCE: result += ENTRANCE_REP; break;
    					case EXIT: result += EXIT_REP; break;
    					
    					}//switch
    					result += NLS;
    					
    				
    			}//for
    			return result;
    			
    		}//toString
    			
    		}//class
    Now the error that I get is a NullPointer exception at line 118. Even more strange is that if I take the if statement from 117 away the moveCount is 200 (which again is not good, but ignore that just yet )
    Any ideas ?

    Thanks a lot

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: Maze Solver Error in Getting instance variable

    Which lines are 117 and 118?
    117 is:
    Java Code:
    in neighbours = 0;
    so no 'if' statement.
    118 is:
    Java Code:
    int column = theCell.column();
    So theCell is null. But that presumes that is actually the line throwing the exception. Which is suspect since 117 is not an 'if' statement.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jijiaremere is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default Re: Maze Solver Error in Getting instance variable

    Hello Tolls,

    Thanks for your reply. Thanks for pointing that out. What I meant was that in the function solve() where is checking the value of the cell

    Java Code:
    if(oneNeighbor.value() == 0)
                        setTheValue(moveCount+1,oneNeighbor);
                        else;
    By commenting that out from the code there is no run time error.
    ---Edit---

    Also I am not sure how the column of any cell in this maze is not set since the constructor of the cell requires a column.


    Thanks once more
    Last edited by jijiaremere; 02-01-2013 at 06:27 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: Maze Solver Error in Getting instance variable

    If that line in my post above is 118 then it's not column that's null, it's theCell.
    You'll need to stick some debugging statements in there to see what values are being passed around.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 04-21-2012, 06:03 PM
  2. The Maze solver Problem
    By robbins in forum Advanced Java
    Replies: 17
    Last Post: 11-23-2010, 07:14 PM
  3. Instance Variable In Servlet
    By javarishi in forum Java Servlet
    Replies: 3
    Last Post: 06-14-2008, 08:28 AM
  4. Instance variable
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 04:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •