Hello folks, I recently started learning Java and bought a book to look at some examples. When I copied this example program out of the book I get a null pointer exception. Most of the other programs worked perfectly this is the only one iv had a problem with so far. At first i thought it was because I didn't have an image called bug1.gif in the same folder as my java files however that was not the case.

The error I get is
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.(init)(ImageIcon.java:167)
at LabelFrame.(init)(LabelFrame.java:23)
at LabelTest.main(LabelTest.java:7)

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.Icon;
import javax.swing.ImageIcon;

public class LabelFrame extends JFrame
{
	private JLabel label1;
	private JLabel label2; // ImageIcon
	private JLabel label3;

	public LabelFrame()
	{
		super("Testing JLabel");
		setLayout(new FlowLayout());

		label1 = new JLabel("Label with Text");
		label1.setToolTipText("This is label1");
		add(label1);

		Icon bug = new ImageIcon(getClass().getResource("bug.gif"));
		label2 = new JLabel("Label with text and icon",bug,SwingConstants.LEFT);
		label2.setToolTipText("This is label2");
		add(label2);

		label3 = new JLabel();
		label3.setText("Label with icon and text at bottom");
		label3.setIcon(bug);
		label3.setHorizontalTextPosition(SwingConstants.CENTER);
		label3.setVerticalTextPosition(SwingConstants.BOTTOM);
		label3.setToolTipText("This is label3");
		add(label3);
	}
}
import javax.swing.JFrame;

public class LabelTest
{
	public static void main(String args[])
	{
		LabelFrame labelFrame = new LabelFrame();
		labelFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		labelFrame.setSize(275,180);
		labelFrame.setVisible(true);
	}
}

Hi.
At first glance it seem that the error is caused by this instruction:

ImageIcon(getClass().getResource("bug.gif"));

Can you please tell where you put the "bug.gif" file?

I have all the files stored together in the same folder (LabelTest, LabelFrame, bug1.gif)

If it is so it should work; please check spelling.

The gif file is named "bug.gif" or "bug1.gif"?

If it is so it should work; please check spelling.

The gif file is named "bug.gif" or "bug1.gif"?

ah that was it, changed it to bug.gif and it works, thank you so much for the help