I still cant get my head around this: java.lang.NullPointerException

heres my disgusting amount of code and its error report. please help. its basically the board for a twixt game. if you dont know what that is its not important because the error has nothing to do with the rules of the game.

Exception in thread "main" java.lang.NullPointerException

at Board.pcomp(Board.java:81)

at Board.addremovebridge(Board.java:34)

at Twixt.main(Twixt.java:21)

Thanks again for reading this far.

publicclass Twixt{

publicstaticvoid main(String[] args){

Board game =new Board();

point p =new point();

p.x = 4;

p.y = 4;

p.p = 1;

point f =new point();

f.x = 5;

f.y = 6;

p.p = 1;

String s =new String();

s = game.placetower(p);

s = game.placetower(p);

System.out.println(s);

s = game.placetower(f);

System.out.println(s);

s = game.addremovebridge(p,f);

System.out.println(s);

s = game.addremovebridge(p,f);

System.out.println(s);

}

}

publicclass point{

int x = 0;

int y = 0;

int p = 0;

}

import java.lang.Math;

publicclass Board{

publicint[][] board1 =newint[26][26];

public point[][][] board2 =new point[26][26][6];

privateint board3d;

privateint board3e;

/*Board() {

for(int x = 0; x<=25; x++){

for(int i = 0; i<=25; i++){

for(int j = 0; j<=6;j++){

pmakezero(board2[x][i][j]);

}

}

}

}*/

public String placetower(point d){

if (board1[d.x][d.y] == 0){

board1[d.x][d.y] = d.p;

return ("Tower placed at "+d.x+", "+d.y+".");

}else{

return ("That space is already taken.");

}

}

public String addremovebridge(point d, point e){

if(checkplayer(d,e)){

boolean removebridge =false;

for(int i = 0; i<=5;i++){

if(pcomp(board2[d.x][d.y][i], e)){

pmakezero(board2[d.x][d.y][i]);

removebridge =true;

}

if(pcomp(board2[e.x][e.y][i], d)){

pmakezero(board2[e.x][e.y][i]);

removebridge =true;

}

}

if(distance(d,e)&&!intersect(d,e)&&!removebridge&&!full(d,e)){

board2[d.x][d.y][board3d] = e;

board2[e.x][e.y][board3e] = d;

return ("Bridge placed between "+d.x+", "+d.y+" and "+e.x+", "+e.y+".");

}else{

return ("That is not a valid bridge placement.");

}

}else{

return ("That tower does not belong to you.");

}

}

privateboolean distance(point d, point e){

double g;

int f;

int i;

f = d.x - e.x;

i = d.y - e.y;

f = f*f;

i = i*i;

f = f+i;

g = f;

if (g>0){

g = g * -1;

}

g = Math.sqrt(g);

if(g == Math.sqrt(5)){

returntrue;

}else{

returnfalse;

}

}

privateboolean pcomp(point d, point e){

if(d.x == e.x && d.y == e.y){

returntrue;

}else{

returnfalse;

}

}

privatevoid pmakezero(point d){

d.x = 0;

d.y = 0;

d.p = 0;

}

privateboolean checkplayer(point d, point e){

if(board1[d.x][d.y] == d.p && board1[e.x][e.y]==e.p){

returntrue;

}else{

returnfalse;

}

}

privateboolean full(point d, point e){

point x;

int y;

int z;

boolean full =true;

for(int i=0; i<=5; i++){

x = board2[d.x][d.y][i];

y = x.x;

z = x.y;

if(y == 0 && z == 0){

board3d = i;

i = 3;

full =false;

}

}

for(int i=0; i<=3; i++){

x = board2[e.x][e.y][i];

y = x.x;

z = x.y;

if(y == 0 && z == 0){

board3e = i;

i = 3;

full =false;

}

}

return full;

}

privateboolean intersect(point d, point e){

boolean inter =false;

int f;

int h;

f = d.x - e.x;

h = d.y - e.y;

if(f == 2 && h == 1){

if(board1[e.x + 1][e.y]!=0){

for(int w=0;w<=5;w++){

if(board2[e.x + 2][e.y + 2][w]==e){

inter =true;

}elseif(board2[e.x][e.y + 2][w]==e){

inter =true;

}

}

}elseif(board1[d.x - 1][d.y]!=0){

for(int w=0;w<=5;w++){

if(board2[d.x - 2][d.y - 2][w]==d){

inter =true;

}elseif(board2[d.x][d.y - 2][w]==d){

inter =true;

}

}

}elseif(board1[e.x + 2][e.y]!=0){

for(int w=0;w<=5;w++){

if(board2[e.x + 1][e.y + 2][w]==e){

inter =true;

}

}

}elseif(board1[d.x - 2][d.y]!=0){

for(int w=0;w<=5;w++){

if(board2[d.x - 1][d.y - 2][w]==d){

inter =true;

}

}

}

}elseif(f == -2 && h == -1){

if(board1[e.x - 1][e.y]!=0){

for(int w=0;w<=5;w++){

if(board2[e.x - 2][e.y - 2][w]==e){

inter =true;

}elseif(board2[e.x][e.y - 2][w]==e){

inter =true;

}

}

}elseif(board1[d.x + 1][d.y]!=0){

for(int w=0;w<=5;w++){

if(board2[d.x + 2][d.y + 2][w]==d){

inter =true;

}elseif(board2[d.x][d.y + 2][w]==d){

inter =true;

}

}

}elseif(board1[e.x - 2][e.y]!=0){

for(int w=0;w<=5;w++){

if(board2[e.x - 1][e.y - 2][w]==e){

inter =true;

}

}

}elseif(board1[d.x + 2][d.y]!=0){

for(int w=0;w<=5;w++){

if(board2[d.x + 1][d.y + 2][w]==d){

inter =true;

}

}

}

}elseif(f == 1 && h == 2){

if(board1[e.x][e.y + 1]!=0){

for(int w=0;w<=5;w++){

if(board2[e.x + 2][e.y + 2][w]==e){

inter =true;

}elseif(board2[e.x + 2][e.y][w]==e){

inter =true;

}

}

}elseif(board1[d.x][d.y - 1]!=0){

for(int w=0;w<=5;w++){

if(board2[d.x - 2][d.y - 2][w]==d){

inter =true;

}elseif(board2[d.x - 2][d.y][w]==d){

inter =true;

}

}

}elseif(board1[e.x][e.y + 2]!=0){

for(int w=0;w<=5;w++){

if(board2[e.x + 2][e.y + 1][w]==e){

inter =true;

}

}

}elseif(board1[d.x][d.y - 2]!=0){

for(int w=0;w<=5;w++){

if(board2[d.x - 2][d.y - 1][w]==d){

inter =true;

}

}

}

}elseif(f == -1 && h == -2){

if(board1[e.x][e.y - 1]!=0){

for(int w=0;w<=5;w++){

if(board2[e.x - 2][e.y - 2][w]==e){

inter =true;

}elseif(board2[e.x - 2][e.y][w]==e){

inter =true;

}

}

}elseif(board1[d.x][d.y + 1]!=0){

for(int w=0;w<=5;w++){

if(board2[d.x + 2][d.y + 2][w]==d){

inter =true;

}elseif(board2[d.x + 2][d.y][w]==d){

inter =true;

}

}

}elseif(board1[e.x][e.y - 2]!=0){

for(int w=0;w<=5;w++){

if(board2[e.x - 2][e.y - 1][w]==e){

inter =true;

}

}

}elseif(board1[d.x][d.y + 2]!=0){

for(int w=0;w<=5;w++){

if(board2[d.x + 2][d.y + 1][w]==d){

inter =true;

}

}

}

}

return inter;

}

}

Thank you.

[19683 byte] By [spoonlyorangea] at [2007-10-2 16:25:18]
# 1

> Exception in thread "main"

> java.lang.NullPointerException

> at Board.pcomp(Board.java:81)

> at Board.addremovebridge(Board.java:34)

> at Twixt.main(Twixt.java:21)

>

So the null pointer occurs on the 81st line in the Board.java source code file. I'm not sure, but I expect this is line 81.if(d.x == e.x && d.y == e.y){

Here's one way to troubleshoot null pointer problems. First find the line causing the error. Then, use System.out.println() statements to display the values of all the reference variables in that line, one variable at a time. So if I guessed correctly on line 81, you would put these statements just before line 81System.out.println("d " + d);

System.out.println("e " + e);

Because d and e are the only reference variables in that line. When you run the code after putting these lines in, one or both of these will display "null" and that will tell you which variable to trace. You will need to trace backward through your code a find out why d or e or both are null.

(hint: A statement like Object[] objects = new Object[10];" creates an Object array with 10 elements, each of which is null. You have to use "objects[1] = new Object();" to create an element in the array which is an Object.)

atmguya at 2007-7-13 17:24:05 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2
Ok, cool, thanks. But it seems that anytime i refernce anything to do with board2 it throws a null exception. Any ideas?
spoonlyorangea at 2007-7-13 17:24:05 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 3
Try thisDownload a winsock fix.WinSock XP Fix for XP http://www.majorgeeks.com/download4372.htmlWinSock Fix for 95 98 ME http://www.softpedia.com/get/Tweak/Network-Tweak/WinSockFix.shtml
Sheila4645a at 2007-7-13 17:24:05 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 4
> Ok, cool, thanks. But it seems that anytime i> refernce anything to do with board2 it throws a null> exception. Any ideas?Did you get the hint I gave you? You need to fill the board2 array with point objects, probably in a constructor for Board.
atmguya at 2007-7-13 17:24:05 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...