java - How to dance around a NullPointerException? -
java - How to dance around a NullPointerException? -
i'm trying check whether or not move legal in game othello, using eclipse , gridworld. first thing location check if valid, in order check validity location, needs not null. problem is, 1 of requirements of beingness legal move is null/empty/unoccupied. how avoid this? have pointed out error supposedly at. (sorry if confused anyone.)
public boolean islegal(location loc1) { boolean islegal = false; string currentcolor = currentplayer.getcolor(); int row = loc1.getrow(); int col = loc1.getcol(); if(board.isvalid(loc1)) { if(board.get(loc1) == null) { for(location temploc : board.getvalidadjacentlocations(loc1)) { **if(!board.get(temploc).equals(currentcolor))** { if((row != temploc.getrow()) && (col == temploc.getcol())) { //count column if(temploc.getrow() < row) { for(int = row; > 1;) { location temploc2 = new location(i-2, col); if(!board.get(temploc2).equals(currentcolor)) { i--; } else { i=-1; islegal = true; } } } //count downwards column else { for(int = row; < 6;) { location temploc2 = new location(i+2, col); if(!board.get(temploc2).equals(currentcolor)) { i++; } else { i=9; islegal = true; } } } } else if(col != temploc.getcol() && row == temploc.getrow()) { //count right row if(col > temploc.getcol()) { for(int = col; > 1;) { location temploc2 = new location(row, i-2); if(!board.get(temploc2).equals(currentcolor)) { i--; } else { i=-1; islegal = true; } } } //count left row else { for(int = col; < 6;) { location temploc2 = new location(row, i+2); if(!board.get(temploc2).equals(currentcolor)) { i++; } else { i=9; islegal = true; } } } } else { //count up/right diag if(row-1 == temploc.getrow() && col+1 == temploc.getcol()) { int j = col; for(int = row; > 1;) { location temploc2 = new location(i-1, j+1); if(!board.get(temploc2).equals(currentcolor)) { i--; j++; } else { i=-1; islegal = true; } } } //count down/left diag else if(row+1 == temploc.getrow() && col-1 == temploc.getcol()) { int = row; for(int j = col; j > 1;) { location temploc2 = new location(i+1, j-1); if(!board.get(temploc2).equals(currentcolor)) { i++; j--; } else { i=9; islegal = true; } } } //count up/left diag else if(row-1 == temploc.getrow() && col-1 == temploc.getcol()) { int j = col; for(int = row; > 1;) { location temploc2 = new location(i-1, j-1); if(!board.get(temploc2).equals(currentcolor)) { i--; j--; } else { i=-1; islegal = true; } } } //count down/right diag else { int j = col; for(int = row; > 6;) { location temploc2 = new location(i+1, j+1); if(!board.get(temploc2).equals(currentcolor)) { i++; j++; } else { i=-1; islegal = true; } } } } } } } } homecoming islegal; }
one solution alter design no location ever null
.
you seem have equated null
"unoccupied" or "empty". instead create positions first (there aren't many of them on othello board) , initialize them boolean occupied = false
or equivalent fellow member variable. you'd have:
if ( !board.get(loc1).isoccupied() ) { /*stuff*/ }
instead of null check.
this improve object oriented design because empty location still location, , should manipulable.
java nullpointerexception gridworld
Comments
Post a Comment