Results 1 to 14 of 14
- 02-12-2011, 04:29 PM #1
Member
- Join Date
- May 2010
- Posts
- 17
- Rep Power
- 0
cant find the error , please help.
im getting this error:
and i cant find the errors origen.Java Code:java.lang.ExceptionInInitializerError Caused by: java.lang.NullPointerException at Map.<init>(Map.java:21)
heres the code:
Java Code:import java.util.List; public class Map { public Entity GLOBAL_ENTITY = new Entity(); List<Entity> ents; char[][] map = new char[10][10]; private int x,y,i; public Map() { for(x = 0; x < 10; x++) { for(y = 0; y < 10; y++) { map[x][y] = '*'; } } ents.add(GLOBAL_ENTITY.add()); } public void DrawMap(List<Entity> el) { for(i = 0; i < el.size();i++) { for(x = 0; x < 10; x++) { for(y = 0; y < 10; y++) { map[x][y] = el.get(i).draw_char; } } } } }Java Code://import java.util.*; public class Entity { public Vector vector = new Vector(); static int count; int id; final char draw_char = 'e'; public void Entitty() { count++; id = count; } public void MoveDir(int ar) { switch(ar) { case 0: vector.x -=1; break; case 1: vector.y -=1; break; case 2: vector.x +=1; break; case 3: vector.y +=1; break; default: System.out.printf("Illigal argument. use 0 , 1, 2 , 3"); break; } } public Entity add() { Entity re = new Entity(); return(re); } }
-
Which line is throwing the error? (line 21 in your Map class) That's the first place to start. And where's the code where you create a Map object and call its methods such as DrawMap(...)?
edit other recommendations: Please learn the Java naming conventions. To keep things consistent and thus easier to understand, you'll want to start method names with a lower case letter and classes with an upper case letter. Also, you may wish to rename your Map class to another name, something that isn't already part of the standard Java library.
Luck!Last edited by Fubarable; 02-12-2011 at 05:08 PM.
- 02-12-2011, 05:06 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
See my comments in your code:
kind regards,Java Code:import java.util.List; public class Map { public Entity GLOBAL_ENTITY = new Entity(); List<Entity> ents; // <--- not initialized so its null char[][] map = new char[10][10]; private int x,y,i; public Map() { for(x = 0; x < 10; x++) { for(y = 0; y < 10; y++) { map[x][y] = '*'; } } // you can't add anything to ents (it's null) ents.add(GLOBAL_ENTITY.add()); }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-12-2011, 05:07 PM #4
Member
- Join Date
- May 2010
- Posts
- 17
- Rep Power
- 0
Java Code:public class Mother { public static int[][] draw_map = new int[10][10]; static int i , j; static final int LEFT = 0; static final int UP = 1; static final int RIGHT = 2; static final int DOWN = 3; static Entity ent1 = new Entity(); static Map map1 = new Map(); public static void main(String[] args) { InitMap(); map1.DrawMap(map1.ents); } public static void InitMap() { for(i = 0; i <10; i++) { for(j = 0; j <10; j++) { draw_map[i][j] = 0; } } } }
- 02-12-2011, 05:09 PM #5
please note that Map is a name for an Interface in Java and it's not a good idea to name your class Map. inside the class Entity you are instantiating a class with new Vector(). even if you make an import with java.util.Vector the code produces errors because the variables x and y used in the code are not fields of the class Vector.
- 02-12-2011, 05:10 PM #6
Member
- Join Date
- May 2010
- Posts
- 17
- Rep Power
- 0
-
assign it an object using new XXXXX(). In this case since List is an interface not a class, you can't do = new List<Entity>();, but you can use new ArrayList<Entity>() or LinkedList whichever you feel is best. I do find it strange that you are using a second class and directly accessing your Map's fields to pass into a Map method. If Map is going to be calling code on its own field, why the need for an outside class to pass the field back in? Again it seems strange.
- 02-12-2011, 05:14 PM #8
Member
- Join Date
- May 2010
- Posts
- 17
- Rep Power
- 0
- 02-12-2011, 05:14 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Pick your choice:
or:Java Code:List<Entity> ents= new ArrayList<Entity>();
b.t.w. you posted another piece of code without any comments; what do these two pieces of code have to do with eachother?Java Code:List<Entity> ents= new LinkedList<Entity>();
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
-
For instance you have the code below (please see comments):
Java Code:public class Mother { //.... static Map map1 = new Map(); // create a new Map object //.... public static void main(String[] args) { InitMap(); map1.DrawMap(map1.ents); // you call Map's DrawMap method passing it's own field, ents back into it. } //.... }
Java Code:class Map { //... List<Entity> ents; //... //... public void DrawMap(List<Entity> el) { for (i = 0; i < el.size(); i++) { for (x = 0; x < 10; x++) { for (y = 0; y < 10; y++) { map[x][y] = el.get(i).draw_char; } } } } }
Rather, wouldn't it make more sense to just have Map make its method call with its own field:
Java Code:public class Mother { //.... static Map map1 = new Map(); //.... public static void main(String[] args) { InitMap(); map1.DrawMap(); // call DrawMap without parameter } //.... }
Java Code:class Map { //... List<Entity> ents; //... //... public void DrawMap() { for (i = 0; i < ents.size(); i++) { // *** call code here with the internal field itself for (x = 0; x < 10; x++) { for (y = 0; y < 10; y++) { map[x][y] = ents.get(i).draw_char; } } } } }
- 02-12-2011, 05:30 PM #11
Member
- Join Date
- May 2010
- Posts
- 17
- Rep Power
- 0
YAY the problem is solved!
or..O.oJava Code:java.lang.NoClassDefFoundError: Mother Caused by: java.lang.ClassNotFoundException: Mother at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) Exception in thread "main"
- 02-12-2011, 07:01 PM #12
Member
- Join Date
- May 2010
- Posts
- 17
- Rep Power
- 0
so what to do?
- 02-12-2011, 07:04 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
- 02-12-2011, 07:17 PM #14
Member
- Join Date
- May 2010
- Posts
- 17
- Rep Power
- 0
Similar Threads
-
Find the error in this and why this can't be ?
By ron2794 in forum New To JavaReplies: 11Last Post: 12-14-2010, 03:23 PM -
I need help with an error! Cannot find symbol error!
By ambria1975 in forum New To JavaReplies: 2Last Post: 07-07-2010, 01:37 AM -
Cannot find symbol error
By rajivjoshi in forum New To JavaReplies: 3Last Post: 05-31-2010, 10:13 AM -
error cannot find symbol
By jcoon3 in forum New To JavaReplies: 3Last Post: 09-27-2009, 10:56 PM -
Error: cannot find symbol
By silvia in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:39 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks