java.lang.IllegalArgumentException:input == null

时间:2014-02-23 01:45:13

标签: java

我正在参加这个Mini Ludum Dare#49

我已经完成了我的代码,但在运行和编译时我遇到了一个问题(很多)。

我在控制台中获取此输出 “

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)

” 现在,我可以列出所有类文件的代码。

(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);
        }
    }

}

(在com.cmnatic.mld.Manager.GameStateManager.setState(GameStateManager.java:46) 在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)     在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)

对于很长的帖子感到抱歉,但那是与问题相关的代码。

我真的很累,所以我会睡几个小时然后再回来看看:)。

谢谢你们,

3 个答案:

答案 0 :(得分:2)

假设您具有以下文件夹结构(使用小写约定)

project
  | -- src
  | -- resources
  |       | -- logo
  |             | -- logo.gif

IntroState.init()修改为

try {
    logo = ImageIO.read(getClass().getResource("/resources/logo/logo.gif"));
}

注意/之前的resources。这段代码完美无缺。刚刚使用简单的应用程序进行测试。

答案 1 :(得分:0)

问题是getClass().getResourceAsStream("res/Logo/logo.gif")会返回null。因此,当您将其作为ImageIO.read()的第一个参数传递时,它会抛出IllegalArgumentException,如ImageIO javadoc中所述:

  

<强>抛出:

IllegalArgumentException - if input is null.

根据Class javadocgetResourceAsStream()在无法找到资源时返回null

  

<强>返回:

A InputStream object or null if no resource with this name is found

所以,问题是无法找到res/Logo/logo.gif

以下是您需要检查的内容:

  • 该文件存在吗?
  • 这是正确的道路吗?
  • 是正确的吗?
  • 你错过了路径开头的“/”(从而使它相对而不是绝对)?

答案 2 :(得分:0)

好的,3年到很晚,但我遇到了同样的问题,也没有一个答案奏效。 所以我尝试了这个,现在它工作正常(所以如果有人在搜索这个):

刷新Eclipse包资源管理器中的resouces文件夹!

现在图像应该显示在资源管理器中,然后就不会有例外了。