Results 1 to 9 of 9

Thread: Using images

  1. #1
    zjames is offline Member
    Join Date
    Nov 2010
    Posts
    20
    Rep Power
    0

    Default Using images

    Hi, I am trying to make a Java app which involves displaying images. I use the code
    Java Code:
    Image = Toolkit.getDefaultToolkit().getImage("img.png");
    but it returns an error, "NullPointerException". img.png is in the build folder with all the .class files. Does anyone know why I get this error?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default

    Is that the actual line you're using? Is that the actual line you get the NPE on? Is Image a variable name? If not, what is the variable name?

  3. #3
    zjames is offline Member
    Join Date
    Nov 2010
    Posts
    20
    Rep Power
    0

    Default

    yes, that it is the line I get an error and Image is a variable type

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default

    I'm surprised you're getting runtime errors, because that line shouldn't even compile. Compare this:
    Java Code:
    String = "test"; //won't compile
    to this:
    Java Code:
    String s = "test"; //will compile
    Notice the difference?

  5. #5
    zjames is offline Member
    Join Date
    Nov 2010
    Posts
    20
    Rep Power
    0

    Default

    I type the code wrong my mistake. It was actually
    Java Code:
     Image img = Toolkit.getDefaultToolkit().getImage("img.png");

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The method may not be looking for the file in the build folder but rather in the user directory. To find out where that is, run
    Java Code:
    System.out.println(System.getProperty("user.dir"));

  7. #7
    zjames is offline Member
    Join Date
    Nov 2010
    Posts
    20
    Rep Power
    0

    Default

    I have run that command and the output was a file location. I have put the images in there, but still get the same error.

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

    Default

    Quote Originally Posted by zjames View Post
    I type the code wrong my mistake. It was actually
    Java Code:
     Image img = Toolkit.getDefaultToolkit().getImage("img.png");

    I am rather surprised you get a null pointer exception at runtime on this line, whether or not the image exists. For a nonexistent file, running the following:

    Java Code:
    import java.awt.Image;
    import java.awt.Toolkit;
    
    public class Main {
        public static void main(String[] args) {
            Image img = Toolkit.getDefaultToolkit().getImage("abcd.efg");
            System.out.println(img);
        }
    }

    I get the following output:

    Java Code:
    sun.awt.image.ToolkitImage@14318bb

    Could you verify that the problem is as you described it (even perhaps running the code above)? Or describe what the problem is?

  9. #9
    zjames is offline Member
    Join Date
    Nov 2010
    Posts
    20
    Rep Power
    0

    Default

    Thanks, I tried running that command and receieved no error.
    Don't know what the error was, but I have now renamed the image to abcdef.png and it works fine.
    Last edited by zjames; 11-19-2010 at 04:55 PM.

Similar Threads

  1. Images
    By Witik in forum New To Java
    Replies: 7
    Last Post: 09-13-2010, 01:34 PM
  2. images
    By amith in forum AWT / Swing
    Replies: 3
    Last Post: 06-27-2008, 08:38 PM
  3. images
    By amith in forum AWT / Swing
    Replies: 1
    Last Post: 05-20-2008, 10:54 AM
  4. Help with images...
    By toby in forum Java Applets
    Replies: 1
    Last Post: 08-04-2007, 05:25 AM
  5. Images in JSP
    By Daniel in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-05-2007, 06:01 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
  •