Reputation: 13
I'm participating in this Mini Ludum Dare #49
I've got my code done but I've got one problem (well a few) when running and compiling.
I am getting this output in the console "
java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at com.cmnatic.mld.GameState.IntroState.init(IntroState.java:31)
at com.cmnatic.mld.Manager.GameStateManager.setState(GameStateManager.java:46)
at com.cmnatic.mld.Manager.GameStateManager.<init>(GameStateManager.java:36)
at com.cmnatic.mld.Main.GamePanel.init(GamePanel.java:95)
at com.cmnatic.mld.Main.GamePanel.run(GamePanel.java:59)
at java.lang.Thread.run(Unknown Source)
" Now, I can give a long list of all the class files' code.
(com.cmnatic.mld.GameState.IntroState) at com.cmnatic.mld.GameState.IntroState.init(IntroState.java:31))
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import com.cmnatic.mld.Main.GamePanel;
import com.cmnatic.mld.Manager.GameStateManager;
import com.cmnatic.mld.Manager.Keys;
public class IntroState extends GameState {
private BufferedImage logo;
private int alpha;
private int ticks;
private final int FADE_IN = 60;
private final int LENGTH = 60;
private final int FADE_OUT = 60;
public IntroState(GameStateManager gsm) {
super(gsm);
}
public void init() {
ticks = 0;
try {
logo = ImageIO.read(getClass().getResourceAsStream("res/Logo/logo.gif"));
}
catch(Exception e) {
e.printStackTrace();
}
}
public void update() {
handleInput();
ticks++;
if(ticks < FADE_IN) {
alpha = (int) (255 - 255 * (1.0 * ticks / FADE_IN));
if(alpha < 0) alpha = 0;
}
if(ticks > FADE_IN + LENGTH) {
alpha = (int) (255 * (1.0 * ticks - FADE_IN - LENGTH) / FADE_OUT);
if(alpha > 255) alpha = 255;
}
if(ticks > FADE_IN + LENGTH + FADE_OUT) {
gsm.setState(GameStateManager.MENU);
}
}
public void draw(Graphics2D g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, GamePanel.WIDTH, GamePanel.HEIGHT2);
g.drawImage(logo, 0, 0, GamePanel.WIDTH, GamePanel.HEIGHT2, null);
g.setColor(new Color(0, 0, 0, alpha));
g.fillRect(0, 0, GamePanel.WIDTH, GamePanel.HEIGHT2);
}
public void handleInput() {
if(Keys.isPressed(Keys.ENTER)) {
gsm.setState(GameStateManager.MENU);
}
}
}
(at com.cmnatic.mld.Manager.GameStateManager.setState(GameStateManager.java:46) at com.cmnatic.mld.Manager.GameStateManager.(GameStateManager.java:36)
package com.cmnatic.mld.GameState;
import java.awt.Graphics2D;
import com.cmnatic.mld.Manager.GameStateManager;
import java.awt.Graphics2D;
public abstract class GameState {
protected GameStateManager gsm;
public GameState(GameStateManager gsm) {
this.gsm = gsm;
}
public abstract void init();
public abstract void update();
public abstract void draw(Graphics2D g);
public abstract void handleInput();
}
at com.cmnatic.mld.Main.GamePanel.init(GamePanel.java:95) at com.cmnatic.mld.Main.GamePanel.run(GamePanel.java:59)
package com.cmnatic.mld.Main;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import com.cmnatic.mld.Manager.GameStateManager;
import com.cmnatic.mld.Manager.Keys;
@SuppressWarnings("serial")
public class GamePanel extends JPanel implements Runnable, KeyListener {
// dimensions
// HEIGHT is the playing area size
// HEIGHT2 includes the bottom window
public static final int WIDTH = 128;
public static final int HEIGHT = 128;
public static final int HEIGHT2 = HEIGHT + 16;
public static final int SCALE = 3;
// game loop stuff
private Thread thread;
private boolean running;
private final int FPS = 30;
private final int TARGET_TIME = 1000 / FPS;
// drawing stuff
private BufferedImage image;
private Graphics2D g;
// game state manager
private GameStateManager gsm;
// constructor
public GamePanel() {
setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT2 * SCALE));
setFocusable(true);
requestFocus();
}
// ready to display
public void addNotify() {
super.addNotify();
if(thread == null) {
addKeyListener(this);
thread = new Thread(this);
thread.start();
}
}
// run new thread
public void run() {
init();
long start;
long elapsed;
long wait;
// game loop
while(running) {
start = System.nanoTime();
update();
draw();
drawToScreen();
elapsed = System.nanoTime() - start;
wait = TARGET_TIME - elapsed / 1000000;
if(wait < 0) wait = TARGET_TIME;
try {
Thread.sleep(wait);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
// initializes fields
private void init() {
running = true;
image = new BufferedImage(WIDTH, HEIGHT2, 1);
g = (Graphics2D) image.getGraphics();
gsm = new GameStateManager();
}
// updates game
private void update() {
gsm.update();
Keys.update();
}
// draws game
private void draw() {
gsm.draw(g);
}
// copy buffer to screen
private void drawToScreen() {
Graphics g2 = getGraphics();
g2.drawImage(image, 0, 0, WIDTH * SCALE, HEIGHT2 * SCALE, null);
g2.dispose();
}
// key event
public void keyTyped(KeyEvent key) {}
public void keyPressed(KeyEvent key) {
Keys.keySet(key.getKeyCode(), true);
}
public void keyReleased(KeyEvent key) {
Keys.keySet(key.getKeyCode(), false);
}
}
at java.lang.Thread.run(Unknown Source)
Sorry for the really long post, but that's the code that's related to the problem.
I'm really tired so I'll sleep for a few hours and check back :)
Thanks guys,
Upvotes: 0
Views: 10537
Reputation: 45
Okay, 3 years to late, but I got the same problem and also none of the answers worked. So I tried this and now it's working fine (so if anyone is searching for this):
Refresh the resouces folder in the Eclipse package explorer!
Now the image should show up in the explorer and then there is no more exception.
Upvotes: 0
Reputation: 3688
Supposing you have the following folder structure (use the lowercase convention)
project
| -- src
| -- resources
| | -- logo
| | -- logo.gif
modify your IntroState.init()
to
try {
logo = ImageIO.read(getClass().getResource("/resources/logo/logo.gif"));
}
Note the /
before resources
. This code works perfectly. Just tested with a simple application.
Upvotes: 3
Reputation: 11890
The problem is that getClass().getResourceAsStream("res/Logo/logo.gif")
returns null
. So, when you pass it as the first argument of ImageIO.read()
, it will throw an IllegalArgumentException
, as said in ImageIO
javadoc:
Throws:
IllegalArgumentException - if input is null.
According to the Class
javadoc, getResourceAsStream()
returns null
when it can't find the ressource:
Returns:
A InputStream object or null if no resource with this name is found
So, the problem is that res/Logo/logo.gif
cannot be found.
Here is what you have to check:
Upvotes: 0