Results 1 to 8 of 8
  1. #1
    hadez is offline Member
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default node[][] cannot be read outside for-loop

    Hi,

    I have to solve for school a pursuer-evader problem using a Self Organising Map, but for some reason I cannot acces the nodes created in a for-loop.
    I have created a class som to create the SOM.
    Java Code:
    	public som(int AantalXnodes, int aantalYnodes, int numRounds, double constStartLearningRate, 
    				Node bmu, AgentListener pursuerListener, AgentListener evaderListener) {
    		AantalXnodesSOM = AantalXnodes;
    		AantalYnodesSOM = aantalYnodes;
    		node = new Node[AantalXnodesSOM][AantalYnodesSOM];
    		this.numRounds = numRounds;
    		this.constStartLearningRate = constStartLearningRate;
    		bmuSOM = bmu;
    		this.pursuerListener = pursuerListener;
    		this.evaderListener = evaderListener;
    		
    		for(int a=0; a < AantalXnodesSOM; a++){
    			for(int b=0; b < AantalYnodesSOM; b++){
    				double x = Math.random() * 848;
    				double y = Math.random() * 360;
    				double z = Math.random() * Geometry.radToDegrees(AgentProperties.EVADER_MAX_BOUND_TURN_RATE);
    				node[a][b] = new Node(x,y,z);
    	//			System.out.println("Node["+a+"]["+b+"] aangemaakt");
    	//			
    				}
    		}
    	}
    where the node constructor looks as follows:
    Java Code:
    import nl.uu.cs.ias.pursuerevader.math.Geometry;
    
    
    public class Node {
    
    	double interval;
    	double radiaal;
    	double actiehoek;
    	double interradSOM; // uniek nummer voor de Node
    	
    	
    	public Node(double x, double y, double z){
    		interval = x;
    		radiaal = Geometry.degreesToRad(y);
    		actiehoek = z;
    		interradSOM = (2 * Math.PI / 848 * interval + radiaal); //  
    	}
    	
    	public void adjustweights(double m_dInfluence, double m_dLearningRate, double BMUinterval, double BMUradiaal){
    		interval += m_dLearningRate * m_dInfluence * (BMUinterval - interval);
    		radiaal += m_dLearningRate * m_dInfluence * (BMUradiaal - radiaal); 
    		interradSOM = (2 * Math.PI / 848 * interval + radiaal);
    	}
    	
    	
    	public String print() {
    		return "interval:\t"+interval+"\n radiaal:\t" + radiaal + "\n interradsom: \t" + interradSOM;
    	}
    }
    Why can't I read the node[x][y] outside the for-loop?

    Thx for your help :-)

    Hadez

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    When are you declaring node? Where do you get the error?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Additionally, what is the error message you're getting?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  4. #4
    hadez is offline Member
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default

    Java Code:
    	public som(int AantalXnodes, int aantalYnodes, int numRounds, double constStartLearningRate, 
    				Node bmu, AgentListener pursuerListener, AgentListener evaderListener) {
    		AantalXnodesSOM = AantalXnodes;
    		AantalYnodesSOM = aantalYnodes;
    		node = new Node[AantalXnodesSOM][AantalYnodesSOM];
    		this.numRounds = numRounds;
    		this.constStartLearningRate = constStartLearningRate;
    		bmuSOM = bmu;
    		this.pursuerListener = pursuerListener;
    		this.evaderListener = evaderListener;
    		
    		for(int a=0; a < AantalXnodesSOM; a++){
    			for(int b=0; b < AantalYnodesSOM; b++){
    				double x = Math.random() * 848;
    				double y = Math.random() * 360;
    				double z = Math.random() * Geometry.radToDegrees(AgentProperties.EVADER_MAX_BOUND_TURN_RATE);
    				node[a][b] = new Node(x,y,z);
    	//			System.out.println("Node["+a+"]["+b+"] aangemaakt");
    	//			
    				}
    			System.out.println(node[5][5].actiehoek);
    		}	
    	}
    Just right outside the for-loop already.

    Error:
    Java Code:
    java.lang.NullPointerException
    
    	at som.<init>(som.java:36)
    	at ExampleAI.start(ExampleAI.java:43)
    	at sun.applet.AppletPanel.run(AppletPanel.java:476)
    	at java.lang.Thread.run(Thread.java:679)
    Exception in thread "Thread-2" java.lang.NullPointerException
    	at ExampleAI.update(ExampleAI.java:62)
    	at nl.uu.cs.ias.pursuerevader.game.Game.send(Game.java:214)
    	at nl.uu.cs.ias.pursuerevader.game.Game.playRound(Game.java:103)
    	at nl.uu.cs.ias.pursuerevader.game.Game.run(Game.java:84)
    	at java.lang.Thread.run(Thread.java:679)
    I am declaring the node here:
    Java Code:
    public class som {
    	private static final long serialVersionUID = 1L;
    	int AantalXnodesSOM, AantalYnodesSOM;
    	Node[][] node;
    	int bmuX, bmuY;	
    	int numRounds;
    	double constStartLearningRate;
    	Node bmuSOM;
    	AgentListener pursuerListener, evaderListener;

  5. #5
    hadez is offline Member
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default

    Solved the problem... Somehow. Had to do the System.out.println(node[5][5].actiehoek); outside the forloop :O

  6. #6
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Think about it, where were you declaring your array? Was [5][5] null when you tried printing it in the loop? Printing it outside of the loop will wait for it to set [5][5] before calling it.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  7. #7
    hadez is offline Member
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default

    Yeah, I tried some troubleshooting as somewhere else in my code had a nullpointerexception. But I have probably changed someting somewhere, and now it's working fine! :-)

  8. #8
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    For future reference, if you ever get a nullpointerexception you should look at the what its telling you. If you are using an IDE it can point you directly to the offending code, and then you can analyze it from there.

    Your normal cause is that you haven't created the variable yet, or given it a value that can be read by the method you are calling.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Similar Threads

  1. Replies: 10
    Last Post: 03-29-2011, 11:09 AM
  2. Vetex vs Node
    By BeijingDuck in forum New To Java
    Replies: 19
    Last Post: 01-19-2011, 07:29 PM
  3. How to get a node value of an XML element?
    By rsenth99 in forum Java Servlet
    Replies: 9
    Last Post: 02-15-2010, 11:35 AM
  4. Replies: 5
    Last Post: 10-17-2008, 02:13 PM
  5. How to disabled a node.
    By smartsubroto in forum New To Java
    Replies: 32
    Last Post: 07-01-2008, 07:30 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
  •