Results 1 to 4 of 4
- 01-31-2013, 08:41 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 2
- Rep Power
- 0
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
And the mazeJava 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
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 )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
Any ideas ?
Thanks a lot
- 02-01-2013, 09:28 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,480
- Rep Power
- 16
Re: Maze Solver Error in Getting instance variable
Which lines are 117 and 118?
117 is:
so no 'if' statement.Java Code:in neighbours = 0;
118 is:
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.Java Code:int column = theCell.column();
Please do not ask for code as refusal often offends.
- 02-01-2013, 06:22 PM #3
Member
- Join Date
- Jan 2013
- Posts
- 2
- Rep Power
- 0
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
By commenting that out from the code there is no run time error.Java Code:if(oneNeighbor.value() == 0) setTheValue(moveCount+1,oneNeighbor); else;
---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 moreLast edited by jijiaremere; 02-01-2013 at 06:27 PM.
- 02-04-2013, 10:41 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,480
- Rep Power
- 16
Similar Threads
-
Creating an instance variable in one class that connects to another instance variable
By SpicyElectricity in forum New To JavaReplies: 1Last Post: 04-21-2012, 06:03 PM -
The Maze solver Problem
By robbins in forum Advanced JavaReplies: 17Last Post: 11-23-2010, 07:14 PM -
Instance Variable In Servlet
By javarishi in forum Java ServletReplies: 3Last Post: 06-14-2008, 08:28 AM -
Instance variable
By Jack in forum New To JavaReplies: 2Last Post: 07-04-2007, 04:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks