Results 1 to 4 of 4
  1. #1
    SwordMaster is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Default Exception in thread "main" java.lang.NullPointerException

    Hey Guys

    I am trying to get NetBeans to open a Window with a Picture I want in it.
    Unfortunately it doesn't work, here's the Code + Error Message

    I added the Ressource Folder with the Images into the Project Properties, so not finding the Images shouldn't be the problem.

    Java Code:
    package image;
    
    import java.awt.*;
    import javax.swing.*;
    
    public class Image extends JFrame
    {
        private JLabel label1;
        private JLabel label2;
        private JTextField textfield;
        private JButton button;
        private ImageIcon image1;
        private ImageIcon image2;
        
        Image()
        {
            setLayout(new FlowLayout());
            
            image1 = new ImageIcon(getClass().getResource("Text.jpg"));
            label1 = new JLabel(image1);
            add(label1);
                   
        }
        public static void main(String args[])
        {
            Image gui = new Image();
            gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            gui.setVisible(true);
            gui.pack();
            gui.setTitle("Title");
            
        }
    }
    Error Message:

    run:
    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
    at image.Image.<init>(Image.java:23)
    at image.Image.main(Image.java:30)
    Java Result: 1

    BUILD SUCCESSFUL (total time: 1 second)

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Exception in thread "main" java.lang.NullPointerException

    finding the Images shouldn't be the problem
    But what you're really interested in is whether it isn't the problem. Write some code and find out:

    Java Code:
    Image()
    {
        setLayout(new FlowLayout());
    
        System.out.println(
            "About to load image from " + getClass().getResource("Text.jpg"));         
        image1 = new ImageIcon(getClass().getResource("Text.jpg"));
        label1 = new JLabel(image1);
        add(label1);
                    
    }
    Then check that the image really does exist at the location specified.

  3. #3
    SwordMaster is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Seems like you are right...

    Now I get this message:

    run:
    About to load image from null
    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
    at image.Image.<init>(Image.java:23)
    at image.Image.main(Image.java:32)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)

    I tried several things with the pictures, so in the end I thought it was sth wrong with the code...
    How comes NetBeans can't find the picture, even if I save the pictures in its package folder / write the path to the picture?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Exception in thread "main" java.lang.NullPointerException

    First, sorry I thought that the code I posted would print the URL even if the resource couldn't be found. I guess I should read the API docs!

    This code *does* tell you where the runtime is looking:

    Java Code:
    Image()
    {
        setLayout(new FlowLayout());
     
        System.out.println("Using directory " + getClass().getResource(""));         
        System.out.println("Image url is " + getClass().getResource("Text.jpg"));         
        image1 = new ImageIcon(getClass().getResource("Text.jpg"));
        label1 = new JLabel(image1);
        add(label1);
                     
    }
    When I run that I see:

    Java Code:
    run:
    Using directory file:/C:/Users/Educator/Documents/NetBeansProjects/ntj/build/classes/image/
    Image url is file:/C:/Users/Educator/Documents/NetBeansProjects/ntj/build/classes/image/Text.jpg
    BUILD SUCCESSFUL (total time: 1 second)
    (And the window appears.)

    As you can see the image file ends up in the NB generated build/classes/image folder. I created the image in the src/image folder ie I put Text.jpg right alongside Image.java and NB copied it into the build tree. In the "Projects" pane of NB you should see the image file alongside the Java source file.

    Check for typos in the name. (And be aware that once you have this sorted out be aware that cAsE mAtTeRs when you package everything up into a jar archive.)

Similar Threads

  1. Replies: 5
    Last Post: 12-21-2011, 06:44 PM
  2. Replies: 5
    Last Post: 10-11-2011, 12:16 AM
  3. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  4. Replies: 1
    Last Post: 07-16-2009, 10:35 AM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •