Results 1 to 10 of 10
  1. #1
    BadWolfe is offline Member
    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    Default 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 07:08 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    • 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.

  3. #3
    BadWolfe is offline Member
    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    Default

    K fixed the post. And mentioned the lines throwing the errors at the top.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by BadWolfe View Post
    K fixed the post. And mentioned the lines throwing the errors at the top.
    Um, which lines are those?

  5. #5
    BadWolfe is offline Member
    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    Default

    In MyVroombs it's the RoomMap.Adder(mapMark); line and in RoomMap it's the grid.add(box); line.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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".

  7. #7
    BadWolfe is offline Member
    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    Default

    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?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by BadWolfe View Post
    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?
    You're fixing the wrong thing then. The solution is not to make things static that shouldn't be static but rather to only call the methods on an object of the class, not the class itself.

  9. #9
    BadWolfe is offline Member
    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    Default

    Actually that got it running now. The algorithm's off but at least it's running. Thanks!

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. What is wrong with this program!?
    By flecdorbee in forum New To Java
    Replies: 5
    Last Post: 02-26-2011, 02:11 AM
  2. what the wrong in my program ><
    By MSs.Java in forum New To Java
    Replies: 4
    Last Post: 05-03-2010, 02:28 PM
  3. What's wrong in my program...?
    By Annatar in forum Java Software
    Replies: 3
    Last Post: 10-31-2008, 07:03 AM
  4. what is wrong with this program ?
    By Poor Bee in forum New To Java
    Replies: 1
    Last Post: 05-07-2008, 08:23 PM
  5. Image problems, what's wrong here.
    By Bluefox815 in forum Java Applets
    Replies: 1
    Last Post: 03-07-2008, 03:45 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
  •