Results 1 to 3 of 3
  1. #1
    Bloitz is offline Member
    Join Date
    May 2011
    Location
    Belgium
    Posts
    13
    Rep Power
    0

    Default NullPointerException in a double loop

    Hello fellow javanians,

    Breaking my head over this method for the last 3 hours. I'm getting a NullPointerException in this double loop. I know it's coming from lblEmpty[row][col] in pnlBoard.add(lblEmpty[row][col]); because I changed it with a testlabel (one that wasn't created in the loop) and it worked...

    Scenario: I have to get a predesigned gameboard from a DB (works because I first did it with a JTextPane and copied the displayBoard method from our working ConsoleApplication) and display it in my GUI. So I made a JPanel to house the gameboard and made a double loop to add JLabels according to the size of the gameboard in the DB.

    Error comes from pnlBoard.add(lblEmpty[row][col]); where lblEmpty[row][col] doesn't seem to exist according to Java (and Java tends to be right a lot).
    Here's my loop:
    Java Code:
    private JLabel lblEmpty[][];
    private JPanel pnlBoard;
    
    
    //loads of other, unrelated code
    
    initGUI(){
    // loads of other, unrelated code
    getContentPane().add(displayBoard());
    }
    
    private JPanel displayBoard()
    	{
    		String[][] spelbord=domeinController.startSpel();
    		
    		GridLayout grid = new GridLayout(spelbord.length, spelbord[0].length, 2, 2);
    		pnlBoard = new JPanel();
    		pnlBoard.setLayout(grid);
    		pnlBoard.setBounds(20,20,500,420);
    		
    		lblEmpty = new JLabel[spelbord.length][spelbord[0].length];
    		for(int row=0;row<spelbord.length;row++)
    		{
    			for(int col=0;col<spelbord[row].length;col++)
    			{
    				if(row == spelbord.length -2 && col == spelbord.length -2){
    					lblFinish = new JLabel("FINISH");
    					pnlBoard.add(lblFinish);
    				}
    				else
    					lblEmpty[row][col] = new JLabel("X");
    					pnlBoard.add([COLOR="red"]lblEmpty[row][col][/COLOR]);
    			}
    		}
    		return pnlBoard;
    	}
    As far as I can tell there's nothing wrong with this loop aside from not creating the lblEmpty ...

    If you'd like more info, please feel free to ask.

    Kind regards, Bloitz

    PS: Dutch ==> English translations if its any help:
    spelbord = gameboard
    domeinController = domainController
    Last edited by Bloitz; 05-12-2011 at 12:55 PM.

  2. #2
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    You've declared and created a 2D array that can hold a table of JLabels, but you haven't put any JLabels into it, so all the entries will be null references.

  3. #3
    Bloitz is offline Member
    Join Date
    May 2011
    Location
    Belgium
    Posts
    13
    Rep Power
    0

    Default

    Thank you for your reply

    I created a second double loop filling up the 2D array and it works. it just seems a lot of code for something small ... :)

    Solution 1: Filling up the 2D array first:
    Java Code:
    private class BoardPanel extends JPanel{
    		
    		private JLabel lblEmpty[][], lblFinish;
    		
    		public BoardPanel(){
    			super();
    			displayBoard();
    		}
    		private void displayBoard()
    		{
    			String[][] spelbord=domeinController.startSpel();
    			
    			GridLayout grid = new GridLayout(spelbord.length, spelbord[0].length, 2, 2);
    			setLayout(grid);
    			setSize(500,420);
    			
    			[COLOR="RoyalBlue"]{
    				lblEmpty = new JLabel[spelbord.length][spelbord[0].length];
    				for(int row=0;row<spelbord.length;row++)
    				{
    					for(int col=0;col<spelbord[row].length;col++)
    					{
    						lblEmpty[row][col] = new JLabel("X");
    					}
    				}
    			}[/COLOR]
    			for(int row=0;row<spelbord.length;row++)
    			{
    				for(int col=0;col<spelbord[row].length;col++)
    				{
    					if(row == spelbord.length -2 && col == spelbord.length -2){
    						lblFinish = new JLabel("FINISH");
    						this.add(lblFinish);
    					}
    					else
    						this.add(lblEmpty[row][col]);
    				}
    			}
    		}
    	}

    Solution2: Creating an Innerclass within an innerclass of the JFrameclass (no, this isn't a sneaky Inception refrence although the movie is pretty good :) )

    Java Code:
    private class BoardPanel extends JPanel{
    		
    		private JLabel lblFinish, lblObst, lblPlayer;
    		[COLOR="RoyalBlue"]private LblEmpty lblEmpty;[/COLOR]
    		
    		public BoardPanel(){
    			super();
    			displayBoard();
    		}
    		private void displayBoard()
    		{
    			String[][] spelbord=domeinController.startSpel();
    			
    			GridLayout grid = new GridLayout(spelbord.length, spelbord[0].length, 2, 2);
    			setLayout(grid);
    			setSize(500,420);
    			
    			for(int row=0;row<spelbord.length;row++)
    			{
    				for(int col=0;col<spelbord[row].length;col++)
    				{
    					if(row == spelbord.length -2 && col == spelbord.length -2){
    						lblFinish = new JLabel("FINISH");
    						this.add(lblFinish);
    					}
    					else
    						[COLOR="RoyalBlue"]lblEmpty = new LblEmpty();[/COLOR]
    						this.add(lblEmpty);
    				}
    			}
    		}
    		
    		[COLOR="RoyalBlue"]private class LblEmpty extends JLabel{
    			public LblEmpty(){
    				super("");
    				this.setBackground(getContentPane().getBackground());
    				this.setBorder(BorderFactory.createLineBorder(Color.white));
    			}
    		}[/COLOR]
    	}
    + rep'ed even though it's obvious now I think of it. :)

    PS: I always get this error when clicking 'Mark as solved' in the thread tools. It does however mark it as solved when I check back. I use Google Chrome version 11.0.696.65: http://i.imgur.com/L0kJs.jpg
    Last edited by Bloitz; 05-12-2011 at 02:53 PM.

Similar Threads

  1. double a * double b = weird output
    By GPB in forum New To Java
    Replies: 3
    Last Post: 03-26-2010, 11:40 AM
  2. Check if double is double
    By marshalthrone in forum New To Java
    Replies: 8
    Last Post: 09-30-2009, 03:51 PM
  3. Replies: 5
    Last Post: 03-21-2009, 10:32 PM
  4. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 09:39 AM
  5. NullPointerException converting String to double
    By infaddict in forum New To Java
    Replies: 3
    Last Post: 07-19-2008, 07:01 PM

Posting Permissions

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