Sign in to follow this  
spacekid434

Java ImageIO Error

Recommended Posts

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
[quote]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
[code] 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;
}[/code]


and the paint method:
[code]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);
}
}[/code]

Share this post


Link to post
Share on other sites
[quote name='spacekid434' timestamp='1318391513' post='4871718']
Can someone please help me!
[/quote]

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...
[code]img = Toolkit.getDefaultToolkit().createImage(filename);[/code]
and in your second getTexture() method, you use...
[code]tempImg = ImageIO.read(Art.class.getResource(filename));[/code]

Share this post


Link to post
Share on other sites
[color="#1C2837"][size="2"][quote]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.

[quote]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

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

[size="2"][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][/size]
[size="2"][color="#1c2837"]
[code]
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;
}
}[/code]
[/color][/size]

Share this post


Link to post
Share on other sites
[quote name='spacekid434' timestamp='1318396587' post='4871735'][color="#1C2837"][size="2"][quote]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[/size][/color][/quote]

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

Also...
[code]g.drawImage((Art.getTexture("GUI/tile.png", 1, 1)), 0, 20, this);[/code]
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this