Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: NullPointerException on JButton.setIcon.

  1. #1
    Member
    Join Date
    May 2013
    Posts
    106
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default NullPointerException on JButton.setIcon.

    Wasn't sure which subforum to put this in. It feels like it crosses a couple different aspects of Java.

    So I have these two classes that I wrote a few months back. The Card class represents a single playing card and the Deck class is just an array of Card objects. I'm trying to build 4 JPanels, each with 13 cards. I have a function that builds an individual panel. It in turn calls a function to deal a single card. Each panel has 13 JButtons with a graphical representation of the card on it.

    Here is the function that builds the panel.

    private JPanel buildCardPanel()
    	{
    		//Panel to add elements to.
    		JPanel returnPanel = new JPanel();
     
    		//JButton on which an individual card will be shown.
    		JButton[] handButton = new JButton[13];
     
    		//A hand of 13 cards.
    		Card[] hand = new Card[13];
     
    		returnPanel.setLayout(new GridLayout(1,13));
    		for(int i = 0; i < 13; i++)
    		{
    			//Deal a card.
    			hand[i] = dealCard();
    			//Set that card's image icon on the button.
    			System.out.println(hand[i].getImageIcon().toString());
    			handButton[i].setIcon(hand[i].getImageIcon());
    			returnPanel.add(handButton[i]);
    		}
    		return returnPanel;
    	}

    Here is the function that deals cards. It accesses a Deck object named starterDeck (I think baseball cards were on my mind when I came up with that name... it's kind of a stupid variable name) defined earlier. I realize that it doesn't prevent the same card from being drawn multiple times. I'll implement that later.

    	private Card dealCard()
    	{
    		Random rand = new Random();
     
    		//Draw a card
    		return starterDeck.cards[rand.nextInt(DECKSIZE)];		
    	}

    The Card.getImageIcon() method is a simple getter method:

    public ImageIcon getImageIcon()
    	{
    		return this.cardIcon;
    	}

    But I keep getting a NullPointerException on the line that says handButton[i].setIcon(hand[i].getImageIcon()); which I don't understand.

    If you notice the println statement before that, you probably realize that's just temporary for debugging purposes. It's returning a value. I get something like "javax.swing.ImageIcon@4ba2fc31", which matches what I get if I put a System.out.println(this.cardIcon); inside the Card.getImageIcon function.

    So I'm really confused. hand[i].getImageIcon() seems to be returning an ImageIcon object. That object is being passed to handButton[i].setIcon(); So where the heck is the Null Pointer Exception coming from?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: NullPointerException on JButton.setIcon.

    What is the value of handButton[i]?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: NullPointerException on JButton.setIcon.

    But I keep getting a NullPointerException on the line that says handButton[i].setIcon(hand[i].getImageIcon()); which I don't understand
    Do you ever instantiate the objects in the handButton array? Creating an array is one thing, but if its an array of objects the default value will be null until instantiated.

  4. #4
    Member
    Join Date
    May 2013
    Posts
    106
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: NullPointerException on JButton.setIcon.

    Quote Originally Posted by copeg View Post
    Do you ever instantiate the objects in the handButton array? Creating an array is one thing, but if its an array of objects the default value will be null until instantiated.
    I thought the statement JButton[] handButton = new JButton[13]; instantiated them. I guess I was wrong. I always screw up with object array. Blech. So I oughtta throw something like handButton[i] = new JButton(); at the start of that for loop?

  5. #5
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: NullPointerException on JButton.setIcon.

    Yes, you also might want to set the icon for the buttons too. They won't have an ImageIcon by default unless you either set it in the constructor or call a setter method.

  6. #6
    Member
    Join Date
    May 2013
    Posts
    106
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: NullPointerException on JButton.setIcon.

    Ok got it working. Thanks Norm and Copeg.

Similar Threads

  1. Problem with setIcon or something(JButton)
    By leonne in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 6th, 2013, 08:41 PM
  2. JButton
    By usherlad in forum AWT / Java Swing
    Replies: 1
    Last Post: August 10th, 2012, 12:02 PM
  3. JButton
    By ranjithfs1 in forum AWT / Java Swing
    Replies: 4
    Last Post: March 17th, 2012, 03:12 AM
  4. Help with jButton
    By ls7897 in forum AWT / Java Swing
    Replies: 6
    Last Post: March 13th, 2011, 03:22 PM
  5. JButton...
    By chronoz13 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 27th, 2009, 11:39 AM