Im tying to run my java program using netbeans and im getting this error, any suggestion?
Exception in thread \"AWT-EventQueue-0\" java.lang.NullPointerException
It looks like one (or more) of the arguments you are passing into your ImageIcon constructor are null.
EDIT
The exception is occurring at one of these two lines:
ImageIcon image = new ImageIcon(MainForm.class.getResource("SHA_logo.gif"));
or
image = new ImageIcon(MainForm.class.getResource("maryland_flag_round.gif"));
In either case, the problem is the same: the resource is not being found, and so null
is being returned by getResource(). Why aren't you just using new ImageIcon(String filename)
? I'm not even 100% sure what getResource() is doing.
A few other quick comments (suggestions) on your code:
pane.setLayout(null);
is not a good idea, I'm not even sure you can just get rid of the LayoutManager like that.
Reusing the same reference (image
and label
) multiple times for different objects is bad coding style. You're not saving any memory by doing that, and the code makes less sense.
Use more descriptive names! button1
should probably be called 'interchangeButton' or something along those lines. For such a small segment of code, no one will get lost, but if you ever work on a larger project, a good naming scheme is vital.