Results 1 to 10 of 10
- 08-14-2011, 05:51 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 5
- Rep Power
- 0
Problems with a program but I don't get what's wrong with it.
So, I'm writing a program for a Vroomba which basically moves a virtual Roomba around a room uploaded from a text file. I'm trying to write my Vroomba Class so it will map the room as it moves around, and so I wrote two additional classes, a square class to represent each floor space that the Vroomba lands on, and a RoomMap class to store the floor spaces in an array. I have all three classes written and they compile, but when I try to run it I get a NullPointer Exception Error. I thought it might have something to do with my attempt to use a LinkedBlockingDeque in order to store each move the Vroomba makes so it can backtrack out of corners, but I'm not sure. If anyone can tell me what's wrong I would appreciate it. I apologize if the program is a little redundant at parts, I'm new to programming. Here are the three programs.
The errors are being thrown at
Line 20 of MyVroomba, and Line 15 of RoomMap.
Java Code:import java.util.*; import java.util.concurrent.LinkedBlockingDeque; import java.lang.InterruptedException; public class MyVroomba extends Vroomba { private Direction currentDir = Direction.N; // private Direction theMove = Direction.HERE; private Direction theMove = currentDir; //private Direction Reverser; private int countX = 0; private int countY = 0; private boolean dirtSeek = true; private LinkedBlockingDeque<Direction> MoveMem = new LinkedBlockingDeque<Direction>(); public Direction move(char[] s) { Square mapMark = new Square(countX, countY, true); RoomMap.Adder(mapMark); if(s[Direction.N.ordinal()] != Room.DROP && s[Direction.N.ordinal()] != Room.WALL) { if(RoomMap.ContentCheck(countX, countY + 1) == false) { countY++; theMove = Direction.N; } } if(s[Direction.NE.ordinal()] != Room.DROP && s[Direction.NE.ordinal()] != Room.WALL) { if(RoomMap.ContentCheck(countX + 1, countY + 1) == false) { countX++; countY++; theMove = Direction.NE; } } if(s[Direction.E.ordinal()] != Room.DROP && s[Direction.E.ordinal()] != Room.WALL) { if(RoomMap.ContentCheck(countX + 1, countY) == false) { countX++; theMove = Direction.E; } } if(s[Direction.SE.ordinal()] != Room.DROP && s[Direction.SE.ordinal()] != Room.WALL) { if(RoomMap.ContentCheck(countX + 1, countY - 1) == false) { countX++; countY--; theMove = Direction.SE; } } if(s[Direction.S.ordinal()] != Room.DROP && s[Direction.S.ordinal()] != Room.WALL) { if(RoomMap.ContentCheck(countX, countY - 1) == false) { countY--; theMove = Direction.S; } } if(s[Direction.SW.ordinal()] != Room.DROP && s[Direction.SW.ordinal()] != Room.WALL) { if(RoomMap.ContentCheck(countX - 1, countY - 1) == false) { countX--; countY--; theMove = Direction.SW; } } if(s[Direction.W.ordinal()] != Room.DROP && s[Direction.W.ordinal()] != Room.WALL) { if(RoomMap.ContentCheck(countX - 1, countY) == false) { countX--; theMove = Direction.W; } } if(s[Direction.NW.ordinal()] != Room.DROP && s[Direction.NW.ordinal()] != Room.WALL) { if(RoomMap.ContentCheck(countX - 1, countY + 1) == false) { countX--; countY++; theMove = Direction.NW; } else { if(MoveMem.peek() != null) { try { theMove = MoveMem.takeLast(); } catch(InterruptedException exception) { System.out.println("Interrupted Exception has been thrown"); } theMove = theMove.reverse(); } else theMove = Direction.HERE; //if(MoveMem.peek() == null) //theMove = Direction.HERE; //else //{ //theMove = MoveMem.takeLast(); //theMove = theMove.reverse(); //} } } MoveMem.add(theMove); return theMove; } public void reset() { currentDir = Direction.N; theMove = Direction.HERE; theMove = currentDir; //Reverser; countX = 0; countY = 0; dirtSeek = true; MoveMem.clear(); } } import java.util.ArrayList; public class RoomMap { private static ArrayList<Square> grid; public RoomMap() { grid = new ArrayList<Square>(); } public static void Adder(Square box) { grid.add(box); } public static boolean ContentCheck(int Xcheck, int Ycheck) { Square check = new Square(Xcheck, Ycheck, true); if(grid.contains(check) == true) return true; else return false; } } public class Square { private int x; private int y; private boolean Visited = false; public Square(int ReceivedX, int ReceivedY) { this.x = ReceivedX; this.y = ReceivedY; } public Square(int ReceivedX, int ReceivedY, boolean isVisited) { this.x = ReceivedX; this.y = ReceivedY; this.Visited = isVisited; } }Last edited by BadWolfe; 08-14-2011 at 06:08 AM.
-
- Which line throws the NPE?
- Can you edit the post above to add code tags to your code post? Place the tag [code] above your code block and the tag [/code] below your block.
- 08-14-2011, 06:08 AM #3
Member
- Join Date
- Aug 2011
- Posts
- 5
- Rep Power
- 0
K fixed the post. And mentioned the lines throwing the errors at the top.
-
- 08-14-2011, 06:27 AM #5
Member
- Join Date
- Aug 2011
- Posts
- 5
- Rep Power
- 0
In MyVroombs it's the RoomMap.Adder(mapMark); line and in RoomMap it's the grid.add(box); line.
-
I think I see your error: you're using static inappropriately. Don't use static unless you have a very strong reason for doing so, and nothing should be static in this code. Nothing. Because of your use of statics, you're trying to add to a grid that hasn't been initialized yet, that is "null".
- 08-14-2011, 06:31 AM #7
Member
- Join Date
- Aug 2011
- Posts
- 5
- Rep Power
- 0
I tried to do non-static like I usually do but then it kept telling me I couldn't use non-static variable in a static manner, which I wasn't but it kept saying that. Any recommendations?
-
- 08-14-2011, 07:09 AM #9
Member
- Join Date
- Aug 2011
- Posts
- 5
- Rep Power
- 0
Actually that got it running now. The algorithm's off but at least it's running. Thanks!
-
Similar Threads
-
What is wrong with this program!?
By flecdorbee in forum New To JavaReplies: 5Last Post: 02-26-2011, 01:11 AM -
what the wrong in my program ><
By MSs.Java in forum New To JavaReplies: 4Last Post: 05-03-2010, 01:28 PM -
What's wrong in my program...?
By Annatar in forum Java SoftwareReplies: 3Last Post: 10-31-2008, 06:03 AM -
what is wrong with this program ?
By Poor Bee in forum New To JavaReplies: 1Last Post: 05-07-2008, 07:23 PM -
Image problems, what's wrong here.
By Bluefox815 in forum Java AppletsReplies: 1Last Post: 03-07-2008, 02:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks