Results 1 to 3 of 3
- 05-12-2011, 11:51 AM #1
Member
- Join Date
- May 2011
- Location
- Belgium
- Posts
- 13
- Rep Power
- 0
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:
As far as I can tell there's nothing wrong with this loop aside from not creating the lblEmpty ...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; }
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 = domainControllerLast edited by Bloitz; 05-12-2011 at 11:55 AM.
- 05-12-2011, 12:49 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
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.
- 05-12-2011, 01:11 PM #3
Member
- Join Date
- May 2011
- Location
- Belgium
- Posts
- 13
- Rep Power
- 0
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 :) )
+ rep'ed even though it's obvious now I think of it. :)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] }
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.jpgLast edited by Bloitz; 05-12-2011 at 01:53 PM.
Similar Threads
-
double a * double b = weird output
By GPB in forum New To JavaReplies: 3Last Post: 03-26-2010, 10:40 AM -
Check if double is double
By marshalthrone in forum New To JavaReplies: 8Last Post: 09-30-2009, 02:51 PM -
non-static method add(double,double) cannot be referenced from a static context
By cravi85 in forum Java SoftwareReplies: 5Last Post: 03-21-2009, 09:32 PM -
Double.valueOf() vs Double.parseDouble()
By greenbean in forum New To JavaReplies: 10Last Post: 01-12-2009, 08:39 AM -
NullPointerException converting String to double
By infaddict in forum New To JavaReplies: 3Last Post: 07-19-2008, 06:01 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks