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.
jcsnippets.atspace.com - 02 Mar 2006 08:19 GMT
> 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...:
<snipped source code
> Honestly, I cannot figure out what the issue is... The errors are:
> Exception in thread "main" java.lang.NullPointerException
[quoted text clipped - 4 lines]
> they are all noted with comments. Any help would be greatly
> appreciated. I apologize profusly if I broke any standard ettiquette.
I've pasted your code in notepad and tried to compile it, but it does not
compile.
At first sight, I'd say there's something wrong in the following:
--- code ---
for(x = 0; x < 10; x++)
for(y = 0; y < 10; y++)
--- /code ---
You have declared map as Tile[15][10], yet you loop x AND y over 0 to 10.
I'm inclined to believe that the null Tiles left are causing you
problems.
If I may, I'd like to make a suggestion. You know on which line(s) the
error occurs (by looking at the stacktrace). Insert some
System.out.println() statements before that line, and write out all
suspicious data. It's a crude way of debugging, but in most cases very
effective in finding errors like these.
Best regards and good luck,
JC

Signature
http://jcsnippets.atspace.com
a collection of source code, tips and tricks
zero - 03 Mar 2006 03:01 GMT
> I keep repeatedly getting nullpointerexception errors in a program I
> am
[quoted text clipped - 5 lines]
> import java.util.*;
> public class Main {
Please try to be a little more creative when picking a name for a class.
Anything is better than "Main". May I suggest "MainClass"
> static Tile[][] map = new Tile[15][10];
>
[quoted text clipped - 5 lines]
> for(y = 0; y < 10; y++)
> map[x][y] = new Tile(x,y,14,9);
your map (also not a very good name, Map is a well-known interface) is
15 columns by 10 rows (or vice versa), but you're only filling the first
10 columns. So, the last 5 are filled with null.
> for(x = 0; x < 15; x++)
> for(y = 0; y < 10; y++){
[quoted text clipped - 4 lines]
>
> map[x][y].dir(r.nextInt(4)); //error
here however, you are going over all 15 columns of the map. So, once x
is 10 or higher, you're essentially saying null.dir(...), which gives
you the NullPointerException.
Note that you can avoid all these problems by finding all occurences of
15 in your code and replacing them with map.length. To get the number
of rows, you can use map[0].length
> }//for
> Disp();
> }
>
> public static void Disp(){
please don't start method names with a capital letter
> String s;
> for(int x = 0; x < 15; x++){
[quoted text clipped - 16 lines]
> else
> s = s+"0";
for performance, you may want to look into StringBuffer:append
<rest of code snipped>
Once you have the NullPointerExceptions fixed, you should try to think
of ways to make your code shorter - and easier to read. Especially the
Tile class could do with some more meaningful variable names. Yes, it's
more typing, but it will help you avoid mistakes - and it will help us
help you if you have more questions.
To make the code shorter, think about some of these points:
* can a Tile have more than one direction? If it can only be north,
east, south or west, but never a combination of those, do you really
need separate booleans for each direction? Wouldn't one variable,
keeping track of the direction, be enough?
* look into the "switch" statement
* try to use a better indentation style (if it got mangled by posting it
here just ignore me). Are there any braces that don't do anything?
Roedy Green - 03 Mar 2006 15:24 GMT
>Please try to be a little more creative when picking a name for a class.
>Anything is better than "Main"
some worse :
Class Interface Java Swing AWT mAIN main
When challenged to come up with a bit of wit on "horticulture", she
quickly penned:
You can lead a horticulture but you can't make her think.
~ Dorothy Canfield Parker

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.