Java ImageIO Error

Started by
4 comments, last by spacekid434 12 years, 6 months ago
I have an Art class with a static overloaded (don't know if this is the name for it in Java) function that returns an Image. One version of the function returns just an Image from a file, the other returns a sub-image based off of an index. However, when I use the the first instance of the function (returns a whole image), the program goes into an infinite loop and keeps paint() ing but doesn't display the actual image. When I try to run the second instance (returns a sub-image) the program throws a java.lang.IllegalArgumentException: input == null!

Here is the full error
java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at me.spacekid434.Exordium.Art.getTexture(Art.java:33)
at me.spacekid434.Exordium.Game.paint(Game.java:45)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$700(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)[/quote]


And here are the two functions
public static Image getTexture(String filename){
Image img = null;
img = Toolkit.getDefaultToolkit().createImage(filename);
System.out.println("Image!");
return img;
}

public static Image getTexture(String filename, int indexX, int indexY){
BufferedImage tempImg = null;
Image img = null;

if(filename == null)
return null;
try{
tempImg = ImageIO.read(Art.class.getResource(filename));
img = tempImg.getSubimage((indexX * TEX_WIDTH), (indexY * TEX_HEIGHT), TEX_WIDTH, TEX_HEIGHT);

}catch(Exception e){
e.printStackTrace();
}
return img;
}



and the paint method:
public void paint(Graphics g){
super.paint(g);
if(gamestate == Gamestate.MainMenu){
g.drawImage(MainMenu.background, 0, 0, this);
g.drawImage((Art.getTexture("GUI/tile.png", 1, 1)), 0, 20, this);

MainMenu.play.setVisible(true);
MainMenu.options.setVisible(true);
MainMenu.load.setVisible(true);
}
else if(gamestate == Gamestate.Credits){
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);
}
else if(gamestate == Gamestate.Settings){
g.drawImage(SettingsMenu.background, 0, 20, this);
}

if(OverlayMenu){
g.setColor(new Color(0, 0, 0, 126));
g.fillRect(0, 0, WIDTH, HEIGHT);
}
}
Advertisement
Can someone please help me!

Can someone please help me!


Where do you use the first function? Unless I'm missing something, it's never called in any of the code you posted. Or were you swapping them out in the paint() method, for testing?

Are you sure you're actually getting a valid image back? Looking at the error messages makes it seem like you're not..

Just out of curiosity, why are you using two different methods to get the image? In your first getTexture() method, you use...
img = Toolkit.getDefaultToolkit().createImage(filename);
and in your second getTexture() method, you use...
tempImg = ImageIO.read(Art.class.getResource(filename));
[color="#1C2837"]
Where do you use the first function? Unless I'm missing something, it's never called in any of the code you posted. Or were you swapping them out in the paint() method, for testing?[/quote]
Im just swapping them out to test.

Are you sure you're actually getting a valid image back? Looking at the error messages makes it seem like you're not..[/quote]
Im not sure I am, can you tell me a way to check please

[color="#1C2837"]Just out of curiosity, why are you using two different methods to get the image? In your first getTexture() method,[color="#1c2837"][/quote]

[color="#1c2837"]That was some old code, I was testing to see if different ways of obtaining the resource would fix it. Here are my new, much cleaner methods (that still dont work):

[color="#1c2837"]

package me.spacekid434.Exordium;

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

public class Art {

public static final int TEX_WIDTH = 16, TEX_HEIGHT = 16;

public Art(){

}

public static BufferedImage getTexture(String filename){
try {
BufferedImage img = ImageIO.read(Art.class.getResource(filename));
return img;
}catch (Exception e){
e.printStackTrace();
}
return null;
}

public static BufferedImage getTexture(String filename, int indexX, int indexY){
try{
BufferedImage tempImg;
BufferedImage img;

tempImg = ImageIO.read(Art.class.getResource(filename));
img = tempImg.getSubimage((indexX * TEX_WIDTH), (indexY * TEX_HEIGHT), TEX_WIDTH, TEX_HEIGHT);

return img;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}

[color="#1C2837"]
Are you sure you're actually getting a valid image back? Looking at the error messages makes it seem like you're not..

Im not sure I am, can you tell me a way to check please[/quote]

Can't you just do the standard...
if (img == null)
{
//do something to inform you that the image wasn't loaded
}


Also...
g.drawImage((Art.getTexture("GUI/tile.png", 1, 1)), 0, 20, this);
I believe, when you're trying to get things from subfolders, there should be a leading slash. Example: "/GUI/tile.png". Maybe try placing the image in the main folder and try to access it with just the file name.
Thanks, looks like it was a mixture of not having a '/' and some other code that i had in the getTexture() functions.

This topic is closed to new replies.

Advertisement