0

My program successfully compiles but when I run it, it gives such error. I think my exception occurs due to image. I have searched on internet for this error but nothing solves my problem. Here is my code

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.Icon;
import javax.swing.ImageIcon;
 class Labelframe extends JFrame
{
    private JLabel l1;
    private JLabel l2;
    private JLabel l3;

    public Labelframe()
    {
        super("Testing JLabel");
        setLayout(new FlowLayout());
        l1=new JLabel("label with text");
        l1.setToolTipText("this is label 1");
        add(l1);

        Icon bug=new ImageIcon(getClass().getResource("bug1.png"));
        l2=new JLabel("label with text and icon",bug,SwingConstants.LEFT);
        l2.setToolTipText("this is label 2");
        add(l2);

        l3=new JLabel();
        l3.setText("label with text and icon at bottom");
        l3.setIcon(bug);
        l3.setHorizontalTextPosition(SwingConstants.CENTER);
        l3.setVerticalTextPosition(SwingConstants.BOTTOM);
        l3.setToolTipText("this is l3");
        add(l3);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(260,180);
        setVisible(true);
    }
}

public class Labeltest 
{
    public static void main(String [] args)
    {
        Labelframe lf=new Labelframe();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Fathr
  • 31
  • 6
  • `I think my exception occurs due to image.` - well that is easy to confirm. The stack trace will tell you the statement causing the error. So then you determine which variable on that line is null and fix the problem. Is your image file found on your class path? Also individual words in a class name should start with an upper case character. – camickr Dec 11 '16 at 02:33
  • Once again when people see the words "NullPointerException" they blindly close the question as a duplicate of: http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it. Once again that answer will not help solve this problem, so I have reopened the question. – camickr Dec 11 '16 at 03:42
  • See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Dec 11 '16 at 05:22
  • Is `bug1.png` in the same path of the Jar (and directory of the file system) as the `Labelframe` class? It works fine with a synthetic image, so I postulate that the image is not on the class-path where you think it is.. – Andrew Thompson Dec 11 '16 at 05:23
  • Thank u for solving my problem. actually my image is not found class path therefore error occurs. – Fathr Dec 12 '16 at 05:33
  • 1
    *"Thank u.."* Thank who? Tip: Add @camickr (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Dec 12 '16 at 12:56
  • Thanks @Andrew Thompson and camickr – Fathr Dec 12 '16 at 16:52

0 Answers0