Results 1 to 3 of 3
  1. #1
    iTyGuy32 is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default Can't load images

    I've looked throughout the internet on ways to modify images, but my biggest problem is that I can't load images unless I specify the full path! I have the image in the same directory as the class & java files. I use eclipse, and it gives a qualified name, or I use just the base name like tutorials do, but I always get a null pointer exception.

    Java Code:
    Image img;
    		Toolkit tool = Toolkit.getDefaultToolkit();
    		img = tool.getImage("test.png");
    Exception:
    Java Code:
    Exception in thread "Thread-4" java.lang.NullPointerException
    	at com.ityguy32.AppletGame.ImageReader.<init>(ImageReader.java:18)
    	at com.ityguy32.AppletGame.Start.run(Start.java:22)
    	at java.lang.Thread.run(Unknown Source)

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

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

    Default

    You need to know where Java is looking for the file, and likely it's the user's working directory. To find out what this is, place this somewhere in a program in the same package as your current program:

    Java Code:
    System.getProperty("user.dir");
    The other option is not to use files at all but instead use resources. If you do that, you'll likely find your images easily since resources look in the location of where your class files are located.

Similar Threads

  1. How to Load images one by one...
    By abhay_mishra in forum AWT / Swing
    Replies: 0
    Last Post: 05-09-2011, 05:23 PM
  2. Load and Save images
    By shakeel in forum Java 2D
    Replies: 13
    Last Post: 12-29-2010, 06:38 PM
  3. Replies: 4
    Last Post: 12-16-2010, 04:32 PM
  4. Replies: 0
    Last Post: 08-13-2010, 10:52 AM
  5. Cannot load images at the SliderDemo
    By HelloRick in forum New To Java
    Replies: 0
    Last Post: 10-07-2009, 09:58 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
  •