Results 1 to 7 of 7
  1. #1
    dsager is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Post NullPointerExceptor Help

    for some reason we are getting a null pointer exceptor in tictactoe at the win method. does anyone know why? please help. would be appreciated if its done in the next 3-5 days



    XML Code:
    import java.awt.Color;
    import java.util.ArrayList;
    import info.gridworld.grid.BoundedGrid;
    import info.gridworld.grid.Grid;
    import info.gridworld.grid.Location;
    import info.gridworld.world.World;
    import java.awt.event.*;
    import java.lang.Iterable;
    public class TicTacToe extends World<Piece>
    {
       private TicTacToeGame game; 
       private Color currentColor;
       private boolean gameOver=false; 
       public int g = 0;
       private Grid<Piece> gr; 
    	ArrayList<Piece> neighbors = null;
       private int [] winsPerPlayer = {0,0};
       public static void main(String[] args)
       {
          TicTacToe world = new TicTacToe(3, 3);
          world.show();
          
       }
      
       
       public TicTacToe(int rows, int cols)   
       {
          super(new BoundedGrid<Piece>(rows, cols));
          game = new TicTacToeGame(getGrid());
          currentColor = Color.WHITE;
          flipTurn();
       }
    
       //changes turns
       public void flipTurn()
       {
          if (currentColor.equals(Color.WHITE))
          {
             currentColor = Color.BLACK;
             setMessage("X's turn");
             g++;
          }
          else 
          {
             currentColor = Color.WHITE;
             setMessage("O's turn");
             g++;
          }         
       }
       
     //determines if the game is over yet
    	public boolean win(Piece piece)
    
    	{	
    		gr = getGrid();
    
    		Location pieceLoc=piece.getLocation();
    		Location adjacent;
    		while (g>=5){
    
    		//for(int angle=0; angle<360; angle+=15)
    		//{	adjacent= pieceLoc.getAdjacentLocation(angle);
    		//	if(gr.get(adjacent) instanceof Piece)
    		//		neighbors.add(gr.get(adjacent));
    		//}
    		//get all neighbors of piece just played
    	
    		//if(gr.getNeighbors(piece.getLocation())!= null)
    	//	{
    		neighbors= gr.getNeighbors(pieceLoc);
    	        
    		    Location center= new Location(1,1); //location of center
    		
    		
    		if (piece instanceof x) //determines if there are 3 X's in a row
    		{	for (Piece p:neighbors)
    				if(pieceLoc!=center)		
    				{if (p instanceof x)
    				{int dir=pieceLoc.getDirectionToward(p.getLocation());
    				if (gr.get(p.getLocation().getAdjacentLocation(dir)) instanceof x)
    					return true;
    				}}
    				else
    					{if (p instanceof x)
    					{int dir=pieceLoc.getDirectionToward(p.getLocation());
    					if (gr.get(pieceLoc.getAdjacentLocation(dir-180)) instanceof x)
    						return true;
    					}}}
    		//determines if there are 3 O's in a row
    		else 
    			{for (Piece donnie:neighbors)
    				if (donnie instanceof o)
    				{int dir=pieceLoc.getDirectionToward(donnie.getLocation());
    				if (gr.get(donnie.getLocation().getAdjacentLocation(dir)) instanceof o)
    				return true;
    				}
    				else
    				{if (donnie instanceof o)
    				{int dir=pieceLoc.getDirectionToward(donnie.getLocation());
    				if (gr.get(pieceLoc.getAdjacentLocation(dir-180)) instanceof o)
    					return true;
    				}}}
    		//if there are not 3 in a row, returns false. else true
    		}
    		return false;
    		
    		
    	}
       
    
    
    
    
    public boolean locationClicked(Location loc){
    	   //gets location of drop
       //Location dropLoc = game.Loca(loc.getCol(), loc.getRow());
    	   gr = getGrid();
       if (!MoveLocation(loc)) 
          setMessage("Illegal drop location, try again");
    
          //determines what to put in grid
       else
          if (currentColor==Color.BLACK){
        	 
        	 gr.put(loc,new x());
        	 
        	 
          }
          else {
        	 
        	  gr.put(loc,new o());
        	 
          }
          //checks to see if game is over
          if (gr.get(loc)!=null) 
        	  gameOver=win(gr.get(loc));
       
          
          if (gameOver==true)
          {
           if (currentColor==Color.BLACK)
        	   winsPerPlayer[0]++;
           else
        	   winsPerPlayer[1]++;
           setMessage("Game Over! X wins- "+winsPerPlayer[0]+"  O wins- "+winsPerPlayer[1]);
          }
          else  
            flipTurn();
       return true;
    }     
       //ADDED LAST MINUTE MAY NOT BE NECESSARY
       public boolean MoveLocation(Location loc)
       {
    	   gr = getGrid();
          if (gr.get(loc)!= null)
    	   if (gr.get(loc) instanceof x ||gr.get(loc) instanceof o)
           return false; //returns false if move not possible
         
        	  return true;   //returns true if move is possible
       }
    }
    
    
    import info.gridworld.actor.Actor;
    import info.gridworld.grid.Grid;
    import info.gridworld.grid.Location;
    
    public class Piece extends Actor
    {
      Location location=this.getLocation();
      int direction;
     
     public Piece()
      {
          direction = Location.NORTH;
          
      }
    
    
    
    }
    
    
    
    import info.gridworld.grid.Grid;
    import info.gridworld.grid.Location;
    
    public class TicTacToeGame
    {
       private Grid<Piece> theGrid;  //grid the game is in
      
       public Location Loca(int column, int row)
       {	Location nextLoc = new Location(row, column);
       	return nextLoc;
       }
    
    	   public TicTacToeGame(Grid<Piece> gr)
    	   {
    	      theGrid = gr;
    	   }
    	}
    
    
    public class o extends Piece{
     public o()
    		   {
    		      
    		   }
    
    	}
    
    
    
    public class x extends Piece
    {
    	Location loc;
    	public x()
    	{
    	
    	}
    	
    		
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: NullPointerExceptor Help

    Add some println's in there to see which variable is null, then backtrack from there. Your code is not an SSCCE and references methods that aren't listed, which doesn't help anyone try and reproduce the problem (or even quickly look over the code to see if all variables are being correctly instantiated)

  3. #3
    dsager is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Re: NullPointerExceptor Help

    The issue happens at the When we try to initialize the neighbors arraylist

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: NullPointerExceptor Help

    Quote Originally Posted by dsager View Post
    The issue happens at the When we try to initialize the neighbors arraylist
    Did you take my advice and print out the values of the variables? This will help you determine which variables are null, and you can go back through your code to determine when, if, and how they are instantiated

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,439
    Blog Entries
    7
    Rep Power
    20

    Default Re: NullPointerExceptor Help

    Quote Originally Posted by dsager View Post
    The issue happens at the When we try to initialize the neighbors arraylist
    All we can do is what you can do yourself: read the stack trace and figure out whiceh object is null while it shouldn't be; next add some System.out.println( ... ) statements to figure out why it is still null.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,439
    Blog Entries
    7
    Rep Power
    20

    Default Re: NullPointerExceptor Help

    This is not an advanced Java question; I moved the thread.

    kind regards,

    Jos

    edit: sorry for not leaving a trace in the other section.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    dsager is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Re: NullPointerExceptor Help

    Do you guys know the grid world for java? That's what the get neighbors method is from. But the problem is for some reason when we use the it, it's not returning anything for the array list neighbors. And we can't figure out why it won't...

Tags for this Thread

Posting Permissions

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