Results 1 to 4 of 4
  1. #1
    knightwriter is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Cannot get image to load onto JFrame...

    I'm trying to load a picture(image1), on a JLabel within a JFrame. I first tried putting the image in the same package as the class it is in. That didn't work. Then I tried putting the image in a separate package within the source packages. Below is the code I'm using to try to get the image to load. FYI Intro is the name of the JFrame.
    Java Code:
    ImageIcon image1 = new ImageIcon(getClass().getResource("pRPG1.png"));
            JLabel label1 = new JLabel(image1);
            Intro.add(label1);
    I've tried reading the swing tutorials and I'm not getting anywhere with this. I feel like this should be a simple process but I can't seem to get it to work.

    Thank in advance for any help.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Cannot get image to load onto JFrame...

    Does the image load? Put this line just before you create your JLabel and see if 'image1' has a reasonable (non null) value:

    Java Code:
    System.out.println(image1);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    knightwriter is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Cannot get image to load onto JFrame...

    Jos,
    I did what you told me to do and ran the app. The JFrame worked but did not show with the image. I did get an exception at runtime here it is:
    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
    at PaperRPG.Introscreen.Intro(Introscreen.java:37)
    at PaperRPG.MainScreen.main(MainScreen.java:11)

    The Introscreen.Intro is where the JFrame is located and it's also where I'm trying to put the image within.
    This is the code on Introscreen.java:37 ImageIcon image1 = new ImageIcon(getClass().getResource("pRPG1.png"));
    The MainScreen.main is my main method that is calling the Introscreen and creating the JFrame.
    This is the code on MainScreen.java:11 Introduction.Intro();

    I hope this helps. I'm not sure what's going on. (and thanks for the help so far!!!)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Cannot get image to load onto JFrame...

    What that stack trace is trying to tell you that in the constructor of the ImageIcon class (that's the <init> part) something failed; most likely it wasn't supplied a URL, so the getResource( ... ) call returned null indicating that no resource could be found. For now try to specify the absolute path of the image file.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Load image from jar
    By sublixt in forum Java 2D
    Replies: 2
    Last Post: 10-17-2011, 01:15 AM
  2. Load image to jPanel
    By DJIT in forum AWT / Swing
    Replies: 7
    Last Post: 12-27-2010, 01:48 PM
  3. load image in the jsp page
    By maneuk in forum Eclipse
    Replies: 0
    Last Post: 04-09-2010, 10:23 PM
  4. Can not load image in SWT/Eclipse
    By janice in forum SWT / JFace
    Replies: 4
    Last Post: 10-03-2008, 05:53 PM
  5. Help with load image
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 08:16 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
  •