I'm trying to load a bitmap into an Image object using the following code:

System.out.println("Point A");
		System.out.println("Point B");
		m_image = Toolkit.getDefaultToolkit().getImage(getClass().getResource(filename));
		System.out.println("Point C");
		MediaTracker mt = new MediaTracker(null);
		System.out.println("Point D");
		mt.addImage(m_image, 0);
		System.out.println("Point E");
		try
		{
			System.out.println("Point F");
			mt.waitForID(0);
			System.out.println("Point G");
		}
		catch (InterruptedException ie)
		{
			System.err.println(ie);
			System.exit(1);
		}
		System.out.println("Point H");

m_image is a private java.awt.Image object of the class I'm in and filename is a String argument of the method I'm in. The filename I pass to this method when I call it is the name of a bitmap in the same directory of the .java file. If it matters, I'm using eclipse to run this.

The code gets to point F, and gives me a NullPointerException (or so I think that's the issue, based on my knowledge of Java error output):

Exception in thread "main" java.lang.NullPointerException
	at java.awt.ImageMediaEntry.getStatus(MediaTracker.java:908)
	at java.awt.MediaTracker.statusID(MediaTracker.java:705)
	at java.awt.MediaTracker.waitForID(MediaTracker.java:653)
	at java.awt.MediaTracker.waitForID(MediaTracker.java:622)
	at Sprite.SetImage(Sprite.java:26)
	at Main.main(Main.java:45)

Any help getting the above code to work (or suggesting a alternate method of loading images) is appreciated.

which line is line 45? there are only 20 here.
what type of image are you trying to load and store to?
what point is it printing up to? (point a,b,c,d,e,f,g,h)

as an alternative why not use BufferedImage img=ImageIO.read(new File("myFile.jpg")); ?
of course i don't know if this works with resources

Sorry about the lines, here's the original file:

import java.awt.*;

public abstract class Sprite
{
	private Image m_image;
	private Point m_position;
	
	public Sprite(int x, int y)
	{
		m_position = new Point(x, y);
	}
	
	public void SetImage(String filename)
	{
		System.out.println("Point A");
		System.out.println("Point B");
		m_image = Toolkit.getDefaultToolkit().getImage(getClass().getResource(filename));
		System.out.println("Point C");
		MediaTracker mt = new MediaTracker(null);
		System.out.println("Point D");
		mt.addImage(m_image, 0);
		System.out.println("Point E");
		try
		{
			System.out.println("Point F");
			mt.waitForID(0);
			System.out.println("Point G");
		}
		catch (InterruptedException ie)
		{
			System.err.println(ie);
			System.exit(1);
		}
		System.out.println("Point H");
	}
	
	
	public void GetInput(KeyboardListener kb)
	{
	}
	
	public void Update()
	{
	}
	
	public void Draw(Graphics2D g)
	{
		g.drawImage(m_image, m_position.x, m_position.y, null);
	}
	
	public Point GetPosition()
	{
		return m_position;
	}
	
	public int GetX()
	{
		return m_position.x;
	}
	
	public int GetY()
	{
		return m_position.y;
	}
	
	public void SetPosition(Point p)
	{
		m_position = p;
	}
	
	public void SetX(int x)
	{
		m_position.x = x;
	}
	
	public void SetY(int y)
	{
		m_position.y = y;
	}
}

line 45 is just the call to this method from another file.

The code gets to point F

I'm loading a bitmap.

I tried the method you suggested and I get this exception:
javax.imageio.IIOException: Can't read input file!

Here's the code:

try
		{
			System.out.println("Point A");
			m_image = ImageIO.read(new File(filename));
			System.out.println("Point B");
		}
		catch(IOException e)
		{
			System.out.println("Point C");
			System.err.println(e);
			System.out.println("Point D");
			System.exit(1);
		}

I also changed m_image from an object of Image to BufferedImage

Erm... silly question, but are you sure the file exists...?
if you test (new File(filename)).exists(), what does it print?

Hmm... apparently Java isn't recognizing the file. But the file is in the same directory as the .java file.

$ ls ~/workspace/Pong/src/
Ball.java  KeyboardListener.java  Main.java  Paddle.java  Sprite.java  paddle.bmp

and the message printed by

if(!(new File(filename)).exists())
		{
			System.out.println(filename+" doesn't exist");
		}

was "paddle.bmp doesn't exist"

Is putting the file in the .java file's directory not sufficient? Does it have to be in a PATH directory?

The location of the source file has nothing to do with it. Either make sure the file is in the working directory from which you run the program (if you're running from the command line, that's the directory you're in when you launch the program), or specify a complete path.

weird, off topic, question are you making Pong or Break Out?

alright, so apparently eclipse uses the project's root folder as the working directory by default, so I made the path local to that and it worked. Many thanks.

Oh, and I'm making Pong.

edit:
hmm, well actually my original method still doesn't work.... I'll used the other method for now, but any other ideas why the first one doesn't work?

I have a feeling it's because you're passing in null to the constructor of MediaTracker.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.