Results 1 to 7 of 7
Thread: NullPointerExceptor Help
- 05-25-2012, 06:04 PM #1
Member
- Join Date
- May 2012
- Posts
- 3
- Rep Power
- 0
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() { } }
- 05-25-2012, 06:13 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
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)
- 05-25-2012, 06:40 PM #3
Member
- Join Date
- May 2012
- Posts
- 3
- Rep Power
- 0
Re: NullPointerExceptor Help
The issue happens at the When we try to initialize the neighbors arraylist
- 05-25-2012, 07:24 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
- 05-25-2012, 07:24 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,403
- Blog Entries
- 7
- Rep Power
- 17
Re: NullPointerExceptor Help
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-25-2012, 07:26 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,403
- Blog Entries
- 7
- Rep Power
- 17
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.When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-25-2012, 08:36 PM #7
Member
- Join Date
- May 2012
- Posts
- 3
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks