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