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 2 of 2

Thread: Java JComboBox and Picture Problem

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Java JComboBox and Picture Problem

    Hi, I'm learning Java, but I'm getting an Error in my "program" - Please help me fix it.

    MY MAIN CLASS:
    import javax.swing.JFrame;
     
    public class Main {
     
    	public static void main(String[] args) {
     
    		Gui go = new Gui();
    		go.setSize(300, 200);
    		go.setVisible(true);
    		go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    	}
     
    }



    MY GUI CLASS:
    import java.awt.*;
    import java.awt.event.*;
     
    import javax.swing.*;
     
    public class Gui extends JFrame{
     
    	private JComboBox box;
    	private JLabel picture;
     
    	//The Pictures
    	private static String[] filename = {"b.png", "x.png"};
    	private Icon[] pics = {new ImageIcon(getClass().getResource(filename[0])), new ImageIcon(getClass().getResource(filename[1]))};
     
    	public Gui(){
    		super("JComboBox");
    		setLayout(new FlowLayout());
     
    		box = new JComboBox(filename);
     
    		box.addItemListener(
    				new ItemListener(){
    					public void itemStateChanged(ItemEvent event){
    						if(event.getStateChange()==ItemEvent.SELECTED)
    							picture.setIcon(pics[box.getSelectedIndex()]);
    					}
    				}
    			);
    		add(box);
    		//Sets default picture.
    		picture=new JLabel(pics[0]);
    		add(picture);
     
    	}
     
    }


    NOTE: I have two pictures in my src folder called : "x.png" and "b.png"


    IM GETTING THIS ERROR:
    Exception in thread "main" java.lang.NullPointerException
    	at javax.swing.ImageIcon.<init>(Unknown Source)
    	at GUI_JComboBox.Gui.<init>(Gui.java:15)
    	at GUI_JComboBox.Main.main(Main.java:9)

    Thanks for your time


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,090
    My Mood
    Mellow
    Thanks
    244
    Thanked 752 Times in 738 Posts

    Default Re: Java JComboBox and Picture Problem

    Use print statements to find the null objects. I'm guessing the files aren't being found so pics[] contains nulls. Start there.

Similar Threads

  1. Problem with reset on JComboBox
    By WorkingMan in forum AWT / Java Swing
    Replies: 4
    Last Post: April 25th, 2013, 01:19 PM
  2. JcomboBox duplicates problem
    By ishiro in forum AWT / Java Swing
    Replies: 0
    Last Post: May 28th, 2012, 02:16 PM
  3. HELP - placing picture on top of another picture in Java - JLayerPane
    By smasm in forum What's Wrong With My Code?
    Replies: 39
    Last Post: April 27th, 2012, 08:16 PM
  4. Converting a picture made up of 0s and 1s into a colored picture??
    By Kranti1992 in forum Java Theory & Questions
    Replies: 10
    Last Post: November 21st, 2011, 06:25 PM
  5. [SOLVED] Having Problem with my JComboBox
    By dazzl3r in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 9th, 2010, 12:03 AM

Tags for this Thread