I keep repeatedly getting nullpointerexception errors in a program I am
writing for pleasure. Unfortunatly, depsite my continual attempts to
fix them, I cannot. Also, any help with the program itself would be
nice...:
//Map generating test program
import java.util.*;
public class Main {
static Tile[][] map = new Tile[15][10];
public static void main(String[] args) {
int x, y, i;
Random r = new Random();
for(x = 0; x < 10; x++)
for(y = 0; y < 10; y++)
map[x][y] = new Tile(x,y,14,9);
for(x = 0; x < 15; x++)
for(y = 0; y < 10; y++){
{//makes the map
i = r.nextInt(2);
if(i>0){
map[x][y].dir(r.nextInt(4)); //error
}//for
Disp();
}
public static void Disp(){
String s;
for(int x = 0; x < 15; x++){
for(int y = 0; y < 10; y++){
if(map[x][y].north())
s = "1";
else
s = "0";
if(map[x][y].east())
s = s+"1";
else
s = s+"0";
if(map[x][y].south())
s = s+"1";
else
s = s+"0";
if(map[x][y].west())
s = s+"1";
else
s = s+"0";
if(s.equals("1000"))
System.out.print("^");
if(s.equals("0100"))
System.out.print(">");
if(s.equals("0010"))
System.out.print("u");
if(s.equals("0001"))
System.out.print("<");
if(s.equals("1010"))
System.out.print("|");
if(s.equals("1100"))
System.out.print("E");
if(s.equals("1001"))
System.out.print("W");
if(s.equals("0101"))
System.out.print("-");
if(s.equals("0110"))
System.out.print("e");
if(s.equals("0011"))
System.out.print("w");
if(s.equals("1101"))
System.out.print("n");
if(s.equals("0111"))
System.out.print("s");
if(s.equals("1110"))
System.out.print("}");
if(s.equals("1011"))
System.out.print("{");
if(s.equals("1111"))
System.out.print("+");
if(s.equals("0000"))
System.out.print("?");
}
System.out.println();
}
}
}
import java.util.*;
public class Tile {
public boolean n,e,s,w,temp, nt, et, st, wt;
public int exits, x, y, i, xmax, ymax;
char c;
Random r = new Random();
public Tile(int xx, int xy, int xxm, int xym){
n = false;
e = false;
s = false;
w = false;
nt= false;
et= false;
st= false;
wt= false;
temp = false;
exits = 0;
x = xx;
y = xy;
xmax = xxm;
ymax = xym;
}
public void dir(int i){
switch(i){
case 0: if(!n && y != ymax){
n = true;
Main.map[x][y+1].dir(2); //error
break;}
case 1: if(!e && x != xmax){
e = true;
Main.map[x+1][y].dir(3); //error
break;
case 2: if(!s && y != 0)
{s = true;
Main.map[x][y-1].dir(0); //error
break;}
case 3: if(!w && x != 0){
w = true;
Main.map[x-1][y].dir(1);
break;}
}}
public int getExits(){
return exits;
}
public boolean north(){
return n;
}
public boolean south(){
return s;
}
public boolean west(){
return w;
}
public boolean east(){
return e;
}
}
Honestly, I cannot figure out what the issue is... The errors are:
Exception in thread "main" java.lang.NullPointerException
at Tile.dir(Tile.java:40)
at Tile.dir(Tile.java:52)
at Tile.dir(Tile.java:30)
at Main.main(Main.java:27)
they are all noted with comments. Any help would be greatly
appreciated. I apologize profusly if I broke any standard ettiquette.
Rhino - 02 Mar 2006 04:55 GMT
>I keep repeatedly getting nullpointerexception errors in a program I am
> writing for pleasure. Unfortunatly, depsite my continual attempts to
[quoted text clipped - 160 lines]
> they are all noted with comments. Any help would be greatly
> appreciated. I apologize profusly if I broke any standard ettiquette.
Telling us which lines of the source are line 40, line 52, line 30, and line
27 would be a big help here. There are no line numbers in the source you
pasted into your post.
Also, NullPointerExceptions are rarely difficult to find if you fire up your
debugger and step through the program to the line that fails: just set a
breakpoint on the desired line and run to the breakpoint, then check which
variables on that line are unexpectedly null. Doing it that way also
eliminates the need for you to wait for replies to your post, which may take
many hours.
I use Eclipse and it comes with a decent debugger; Eclipse is free so the
price is right. I also find that I am more productive using an IDE than the
command line, partly because the debugger is a faster way to find problems
than writing System.out.println() statements.
--
Rhino
Vincent YSMAL - 02 Mar 2006 06:33 GMT
MoogleGunner a écrit :
> I keep repeatedly getting nullpointerexception errors in a program I am
> writing for pleasure. Unfortunatly, depsite my continual attempts to
[quoted text clipped - 160 lines]
> they are all noted with comments. Any help would be greatly
> appreciated. I apologize profusly if I broke any standard ettiquette.
Hello,
it seems that, in main method, you make a loop to create Tile on x from
0 to 9 (<10), and then, you make loop on x from 0 to 14, for :
{//makes the map
, but when x =10 .... you're trying to access a Tile is not existing ....