Error In Java: IllegalArgumentException -
i working on game , getting error code:
exception in thread "main" java.lang.illegalargumentexception: input == null! @ javax.imageio.imageio.read(unknown source) @ com.mcserverpros.game.gfx.spritesheet.<init>(spritesheet.java:21) @ com.mcserverpros.game.game.<init>(game.java:32) @ com.mcserverpros.game.game.main(game.java:146) this game.java class:
package com.mcserverpros.game; import java.awt.borderlayout; import java.awt.canvas; import java.awt.dimension; import java.awt.graphics; import java.awt.image.bufferstrategy; import java.awt.image.bufferedimage; import java.awt.image.databufferint; import javax.swing.jframe; import com.mcserverpros.game.gfx.spritesheet; public class game extends canvas implements runnable { private static final long serialversionuid = 1l; public static final int width = 160; public static final int height = width / 12 * 9; public static final int scale = 3; public static final string name = "game"; private jframe frame; public boolean running = false; public int tickcount = 0; private bufferedimage image = new bufferedimage(width, height, bufferedimage.type_int_rgb); private int[] pixels = ((databufferint) image.getraster().getdatabuffer()) .getdata(); public spritesheet spritesheet = new spritesheet("/sprite_sheet.png"); public game() { setminimumsize(new dimension(width * scale, height * scale)); setmaximumsize(new dimension(width * scale, height * scale)); setpreferredsize(new dimension(width * scale, height * scale)); frame = new jframe(name); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new borderlayout()); frame.add(this, borderlayout.center); frame.pack(); frame.setresizable(false); frame.setlocationrelativeto(null); frame.setvisible(true); } public synchronized void start() { running = true; new thread(this).start(); } public synchronized void stop() { running = false; } public void run() { long lasttime = system.nanotime(); double nspertick = 1000000000d / 60d; int frames = 0; int ticks = 0; long lasttimer = system.currenttimemillis(); double delta = 0; while (running) { long = system.nanotime(); delta += (now - lasttime) / nspertick; lasttime = now; boolean shouldrender = false; while (delta >= 1) { ticks++; tick(); delta -= 1; shouldrender = true; } try { thread.sleep(2); } catch (interruptedexception e) { e.printstacktrace(); } if (shouldrender) { frames++; render(); } if (system.currenttimemillis() - lasttimer >= 1000) { lasttimer += 1000; system.out .println(ticks + " ticks" + ", " + frames + " frames"); frames = 0; ticks = 0; } } } public void tick() { tickcount++; (int = 0; < pixels.length; i++) { pixels[i] = + tickcount; } } public void render() { bufferstrategy bs = getbufferstrategy(); if (bs == null) { createbufferstrategy(3); return; } graphics g = bs.getdrawgraphics(); g.drawimage(image, 0, 0, getwidth(), getheight(), null); g.dispose(); bs.show(); } public static void main(string[] args) { new game().start(); } } and spritesheet.java class
package com.mcserverpros.game.gfx; import java.awt.image.bufferedimage; import java.io.ioexception; import javax.imageio.imageio; public class spritesheet { public string path; public int width; public int height; public int[] pixels; public spritesheet(string path) { bufferedimage image = null; try { image = imageio.read(spritesheet.class.getresourceasstream(path)); } catch (ioexception e) { e.printstacktrace(); } if (image == null) { return; } this.path = path; this.width = image.getwidth(); this.height = image.getheight(); pixels = image.getrgb(0, 0, width, height, null, 0, width); (int = 0; < pixels.length; i++) { pixels[i] = (pixels[i] & 0xff) / 64; } (int = 0; < 8; i++) { system.out.println(pixels[i]); } } }
check line:
imageio.read(spritesheet.class.getresourceasstream(path)) it seems spritesheet.class.getresourceasstream(path) returning null value. it's quite possible path isn't pointing actual file in filesystem. notice you're passing "/sprite_sheet.png", that's path relative source code's directory. take @ this post idea of how can build relative path works. if else fails use absolute path, won't portable.
Comments
Post a Comment