Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2010
    Rep Power

    Default Problem loading an Image

    Firstly I am sorry if this is the incorrect place for this but it seems right to me. I am trying to load an image into my program but can't seem to be able to do it. I have searched the Internet and this forum and keep coming up with the same few approaches and have tried them all. I am using Eclipse and have a file structure as follows:

    | -
    | - blocks1.bmp

    So I have a package inside the src dir and a res dir where I am currently storing the image. I have tried the following code:

    Java Code:
    package editor;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
    public class EditorPanel extends JPanel
        private static final int DEFAULT_WIDTH = 800;
        private static final int DEFAULT_HEIGHT = 600;
        private ImageIcon icon;
        private Image image;
        public EditorPanel()
    	setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
    	 URL imgURL = EditorPanel.class.getResource("../../res/block1.bmp");
    	 image = Toolkit.getDefaultToolkit().getImage(imgURL);
        public void paintComponent(Graphics g)
    	// g.drawImage(icon.getImage(), 20, 20, null);
    	g.drawImage(image, 20, 20, null);
    	g.drawString("Can You See Me", 10, 50);
    er different methods of loading the image but each time I get a NullPointerException at the point of drawing the image in paintComponent. I have also tried different variations of the path string but to no avail. Does any here know what I am doing wrong?
    }// end EditorPanel
    I have tried a few oth

  2. #2
    Join Date
    Oct 2010
    Rep Power


    I have fixed this now and the was problem that my res folder was outside of the src folder. I have moved it and all is ok now.

Similar Threads

  1. Image Loading
    By hobbles in forum New To Java
    Replies: 10
    Last Post: 09-06-2010, 08:30 PM
  2. Problem with loading image
    By m00nchile in forum New To Java
    Replies: 5
    Last Post: 05-25-2010, 01:34 PM
  3. Problem in loading Image
    By rahulm87 in forum Java 2D
    Replies: 1
    Last Post: 09-08-2009, 04:45 PM
  4. Rambling about image loading...
    By Zamppa in forum Java Applets
    Replies: 0
    Last Post: 04-14-2009, 03:55 PM
  5. Loading An Image Help Please!
    By shaungoater in forum Java 2D
    Replies: 2
    Last Post: 01-09-2008, 09:14 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