0

I want to add image in java swing, I wrote a simple code with out any error in the code(l also tried ("rr.png") instead ("c:\rr.png") but in both situation I got this when l want run it:

Exception in thread "main" java.lang.NullPointerException
   at javax.swing.ImageIcon.<init>(Unknown Source)
   at quize1.uml.<init>(uml.java:14)
   at quize1.uml.main(uml.java:20)

Here is my code:

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

  public class uml extends JFrame {

  private ImageIcon image2;
  private JLabel Label2;
   public uml() {
    setLayout(new FlowLayout());
    image2 = new ImageIcon(getClass().getResource("c:\\rr.png"));
    Label2 = new JLabel( image2);
    add (Label2);
    }

   public static void main(String arg[]) {
    uml gui = new uml();
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.setVisible(true);
    gui.setTitle("uml ");

   }}
seenukarthi
  • 8,241
  • 10
  • 47
  • 68
Rezan
  • 35
  • 4
  • Put your image files in the resource folder, not in your C drive. Then you can access it by just calling `getClass().getResource("rr.png")`. – lkq Jun 29 '16 at 18:29
  • @KeqiangLi please can you tell me what exactly mean by resource folder, thank you. – Rezan Jun 29 '16 at 18:34
  • say you are using Eclipse, check this [post](http://stackoverflow.com/questions/27934796/how-do-i-add-a-resources-folder-to-my-java-project-in-eclipse): – lkq Jun 29 '16 at 18:37
  • @KeqiangLi appreciate your help, please can you write answer in order to tick it as a right answer, thanks a lot. – Rezan Jun 29 '16 at 19:03
  • No need to, I'll just mark this question as a duplicate one. :) – lkq Jun 29 '16 at 19:06

0 Answers0