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

    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");
    Java Code:
    Exception in thread "Thread-4" java.lang.NullPointerException
    	at com.ityguy32.AppletGame.ImageReader.<init>(
    	at Source)

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Rep Power

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    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:
    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, 06:23 PM
  2. Load and Save images
    By shakeel in forum Java 2D
    Replies: 13
    Last Post: 12-29-2010, 07:38 PM
  3. Replies: 4
    Last Post: 12-16-2010, 05:32 PM
  4. Replies: 0
    Last Post: 08-13-2010, 11:52 AM
  5. Cannot load images at the SliderDemo
    By HelloRick in forum New To Java
    Replies: 0
    Last Post: 10-07-2009, 10: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