This is the main class:-
import javax.swing.JFrame;
public class RR {
public static void main(String[] args) {
RegionalResource object = new RegionalResource();
object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
object.setSize(500,500);
object.setVisible(true);
}
}
Here's the other class:-
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RegionalResource extends JFrame {
private JComboBox box;
private JLabel picture;
private static String imagenames[] = {"1.png", "2.png"};
private Icon images[] = {new ImageIcon(getClass().getResource(imagenames[0])), new ImageIcon(getClass().getResource(imagenames[1])) };
public RegionalResource() {
super("Tourism Information Provider");
setLayout(new FlowLayout());
box = new JComboBox(imagenames);
box.addItemListener(
new ItemListener() {
public void itemStateChanged(ItemEvent event) {
if(event.getStateChange() == ItemEvent.SELECTED)
{
picture.setIcon(images[box.getSelectedIndex()]);
}
}
}
);
add(box);
picture = new JLabel(images[0]);
add(picture);
}
}
When I run the program (in eclipse oxygen 2), it results in the following error
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at RegionalResource.<init>(RegionalResource.java:11)
at RR.main(RR.java:6)