Results 1 to 14 of 14
  1. #1
    TheBreadCat is offline Member
    Join Date
    May 2010
    Posts
    17
    Rep Power
    0

    Default cant find the error , please help.

    im getting this error:
    Java Code:
    java.lang.ExceptionInInitializerError
    Caused by: java.lang.NullPointerException
    	at Map.<init>(Map.java:21)
    and i cant find the errors origen.
    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);
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    See my comments in your code:

    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());
    	}
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    TheBreadCat is offline Member
    Join Date
    May 2010
    Posts
    17
    Rep Power
    0

    Default

    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;
    			}
    		}
    	}
    
    }

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    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.

  6. #6
    TheBreadCat is offline Member
    Join Date
    May 2010
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    See my comments in your code:

    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());
    	}
    kind regards,

    Jos
    then how do i initialize it?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by TheBreadCat View Post
    then how do i initialize it?
    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.

  8. #8
    TheBreadCat is offline Member
    Join Date
    May 2010
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    See my comments in your code:

    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());
    	}
    kind regards,

    Jos
    Quote Originally Posted by j2me64 View Post
    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.
    renamed Vector to Vector2d and Map to Wolrd.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by TheBreadCat View Post
    then how do i initialize it?
    Pick your choice:

    Java Code:
    List<Entity> ents= new ArrayList<Entity>();
    or:

    Java Code:
    List<Entity> ents= new LinkedList<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?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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;
                }
             }
          }
       }
    }

  11. #11
    TheBreadCat is offline Member
    Join Date
    May 2010
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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;
                }
             }
          }
       }
    }
    YAY the problem is solved!
    or..
    Java 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"
    O.o

  12. #12
    TheBreadCat is offline Member
    Join Date
    May 2010
    Posts
    17
    Rep Power
    0

    Default

    so what to do?

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by TheBreadCat View Post
    so what to do?
    Compile your Mother class and make the classpath point to the correct directory. (even better for now: recompile all your Java files)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    TheBreadCat is offline Member
    Join Date
    May 2010
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    make the classpath point to the correct directory.
    how?(im using eclipse)

Similar Threads

  1. Find the error in this and why this can't be ?
    By ron2794 in forum New To Java
    Replies: 11
    Last Post: 12-14-2010, 03:23 PM
  2. I need help with an error! Cannot find symbol error!
    By ambria1975 in forum New To Java
    Replies: 2
    Last Post: 07-07-2010, 01:37 AM
  3. Cannot find symbol error
    By rajivjoshi in forum New To Java
    Replies: 3
    Last Post: 05-31-2010, 10:13 AM
  4. error cannot find symbol
    By jcoon3 in forum New To Java
    Replies: 3
    Last Post: 09-27-2009, 10:56 PM
  5. Error: cannot find symbol
    By silvia in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •