Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By eRaaaa

Thread: Setting background for JPanel causes NullPointerException

  1. #1
    JG4m3r is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Setting background for JPanel causes NullPointerException

    Hi guys!
    So I'm, having this problem in my program. I've gotten a JPanel array, and i want to give the JPanels in this array a background color.
    I am trying to do this by using the following code, but I get a NullPointerException:

    Java Code:
    JPanel[][] mapPanels;
    mapPanels = new JPanel[mapHeight][mapWidth]; //I get the two variables mapHeight and mapWidth earlier in the program
    
    for(int i= 0; i<mapHeight-1; i++){
    		for(int u = 0; u<mapWidth-1; u++){
    			mapPanels[i][u].setBackground(Color.BLACK);
    		}
    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: Setting background for JPanel causes NullPointerException

    You never created a JPanel instance!
    Add the line
    mapPanels[i][u] = new JPanel();
    in your loop otherwise all array entries are null!
    Fubarable likes this.

  3. #3
    JG4m3r is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Re: Setting background for JPanel causes NullPointerException

    Thank you very much

Similar Threads

  1. Setting JFrames Background To An Image?
    By Alerhau in forum AWT / Swing
    Replies: 2
    Last Post: 08-13-2011, 10:38 PM
  2. background color with jpanel
    By hannerz06 in forum New To Java
    Replies: 6
    Last Post: 03-31-2010, 03:25 AM
  3. setting background color of JFrame form with NetBeans 6.1
    By onefootswill in forum New To Java
    Replies: 4
    Last Post: 08-12-2008, 07:02 AM
  4. Replies: 2
    Last Post: 03-24-2008, 05:29 AM
  5. Problem Setting offscreen background
    By D34N0 in forum Java Applets
    Replies: 1
    Last Post: 07-14-2007, 11:46 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
  •