I have linked the file I want to load and when I debug, the SpriteSheet constructor shows that the path variable is storing the path I specify. When it trying to run the image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path)); line of code, it crashes with an Illegal Argument Exception. The only thing I can think is wrong is that the file I specified will not load, but I have no idea why.
Game class:
package com.swainchris.twodgame;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
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.swainchris.twodgame.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 = "2D Game";
public static boolean running = false;
private JFrame frame;
public int tickCount = 0;
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
private SpriteSheet spriteSheet = new SpriteSheet("/SS.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 tick(){
tickCount++;
for(int i = 0; i < pixels.length; i++){
pixels[i] = 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.setColor(Color.BLACK);
g.fillOval(50,50,50,50);
g.dispose();
bs.show();
}
public void run() {
long lastTime = System.nanoTime();
double nsPerTick = 1000000000D/60D;
int ticks = 0;
int frames = 0;
long lastTimer = System.currentTimeMillis();
double delta = 0;
while(running){
long now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
boolean shouldRender = true;
while(delta >= 1){
ticks++;
tick();
delta--;
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(shouldRender){
frames++;
render();
}
if(System.currentTimeMillis() - lastTimer >= 1000){
lastTimer += 1000;
frame.setTitle("2D Game! FPS: " + frames + " UPS: " + ticks);
frames = 0;
ticks = 0;
}
}
}
public static void main(String[] args) {
new Game().start();
}
}
SpriteSheet class:
package com.swainchris.twodgame.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);
for(int i = 0; i < pixels.length; i++){
pixels[i] = (pixels[i] & 0xff)/64;
}
for(int i = 0; i<8; i++){
System.out.println(pixels[i]);
}
}
}
Error:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at com.swainchris.twodgame.gfx.SpriteSheet.<init>(SpriteSheet.java:20)
at com.swainchris.twodgame.Game.<init>(Game.java:30)
at com.swainchris.twodgame.Game.main(Game.java:137)
Works now, all i did was copy all the code to a new project file. Didn't change anything at all besides the name of the project. No idea why this worked but it did.
Related
Right now I just wanted to get my game window shown on the screen and I thought all would be well, except for the fact that the color red isn't shown on the whole window. Not sure why, I've gone over my code a few times and can't figure anything out. This is my code:
package 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;
public class Game extends Canvas implements Runnable {
private final String GAME_NAME;
private final int GAME_WIDTH;
private final int GAME_HEIGHT;
private final int SCALE;
private boolean isRunning;
private JFrame frame;
private BufferedImage image;
private int[] pixels;
public Game(String name, int width, int aspectRatio[], int scale) {
// Variabe Initialization
this.GAME_NAME = name;
this.GAME_WIDTH = width;
this.GAME_HEIGHT = width / aspectRatio[0] * aspectRatio[1];
this.SCALE = scale;
this.image = new BufferedImage(GAME_WIDTH, GAME_WIDTH, BufferedImage.TYPE_INT_RGB);
this.pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
this.frame = new JFrame(GAME_NAME);
Init();
}
private void Init() {
// Frame Setup
setMinimumSize(new Dimension(GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE));
setMaximumSize(new Dimension(GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE));
setPreferredSize(new Dimension(GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE));
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);
}
private void Start() {
isRunning = true;
new Thread(this).start();
}
private void Stop() {
isRunning = false;
}
#Override
public void run() {
long lastTime = System.nanoTime();
long timer = System.currentTimeMillis();
final double ns = 1000000000d / 60d;
double delta = 0d;
int frames = 0;
int updates = 0;
while (isRunning) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta >= 1) {
updates++;
Update();
delta--;
}
Render();
frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
frame.setTitle(GAME_NAME + " | " + updates + " ups, " + frames + " fps");
updates = 0;
frames = 0;
}
}
}
public void Update() {
for (int y = 0; y < GAME_HEIGHT; y++) {
for (int x = 0; x < GAME_WIDTH; x++) {
pixels[x + y * GAME_WIDTH] = 0xffff0000;
}
}
}
public void Render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
requestFocus();
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("Game", 160, new int[]{12, 9}, 1).Start();
}
}
You've accidentally passed GAME_WIDTH as the height argument of the BufferedImage constructor call:
public Game(String name, int width, int aspectRatio[], int scale) {
// Variabe Initialization
this.GAME_NAME = name;
this.GAME_WIDTH = width;
this.GAME_HEIGHT = width / aspectRatio[0] * aspectRatio[1];
this.SCALE = scale;
this.image = new BufferedImage(GAME_WIDTH, GAME_WIDTH, BufferedImage.TYPE_INT_RGB);
// ^^^^^^^^^^
this.pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
this.frame = new JFrame(GAME_NAME);
Init();
}
You probably meant to use GAME_HEIGHT:
this.image = new BufferedImage(GAME_WIDTH, GAME_HEIGHT, BufferedImage.TYPE_INT_RGB);
I am trying to add the image sprite_sheet.png to my game project but I get these errors:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at az.rehman.gfx.SpriteSheet.<init>(SpriteSheet.java:16)
at az.rehman.game.Game.<init>(Game.java:29)
at az.rehman.game.Game.main(Game.java:127)
Here are my classes:
package az.rehman.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.getResource(path));
} catch (IOException e) {
System.out.println("SpriteSheet file exception");
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);
for(int i = 0; i < pixels.length; i++) {
pixels[i] = (pixels[i] & 0xff/64);
}
for(int i = 0; i < 8; i++) {
System.out.println(pixels[i]);
}
} // constructer end
} // class end
package az.rehman.game;
import az.rehman.gfx.SpriteSheet;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
public 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();
private 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 ticks = 0;
int frames = 0;
long lastTimer = System.currentTimeMillis();
double delta = 0;
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
boolean shouldRender = true;
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++;
for (int i = 0; i < pixels.length; i++) {
pixels[i] = 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();
}
}
I'm learning to make games using a a tutorial on youtube. Everything seems fine except when I run the program. A frame shows up with the accurate width I want but the height looks like it sets to a default no matter what value I give it.
package ca.vanzeben.game;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
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;
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; // able to move screen
public static final String NAME = "Juego";
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 Game() { //game constructor
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); // closes game completely
frame.setLayout(new BorderLayout());
frame.add(this,BorderLayout.CENTER); //adds canvas to JFrame and centers it
frame.pack();//keeps everything sized correctly
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public synchronized void start() {//so u can start from the applet
running = true;
new Thread(this).start();
}
public synchronized void stop(){
running = false;
}
public void run() {
long lastTime = System.nanoTime();
double nsPerTick = 1000000000.0/60; //nanoseconds per tick or per update
int ticks = 0;
int frames = 0;
long lastTimer = System.currentTimeMillis();
double delta = 0.0; //how many unprocessed nano seconds
while(running){
long now = System.nanoTime();
delta+=(now-lastTime)/nsPerTick;
lastTime = now;
boolean shouldRender = true;
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(frames + " frames " + ticks + " ticks ");
frames = 0;
ticks = 0;
}
}
}
public void tick(){ //updates the game, updates the logic
tickCount++;
for(int i =0;i<pixels.length;i++){
pixels[i] = i + tickCount;
}
}
public void render(){ //prints out ^
BufferStrategy bs = getBufferStrategy();
if(bs == null){
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
g.setColor(Color.BLACK);
g.drawRect(0, 0, getWidth(), getHeight());
g.dispose();
bs.show();
}
public static void main(String[] args){
new Game().start();
}
}
You defined Height and used HEIGHT. Change it to:
public static final int HEIGHT = WIDTH / 12 * 9;
This is how it looked like to me:
Before:
After:
So I am following a series of tutorials to learn how to make a game in java(i am still pretty new), and i think i followed his code exactly, but it still prints the stack trace when i run it. Here is my code...
Game.java
package com.game.src.main;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
public static final int WIDTH = 320;
public static final int HEIGHT = WIDTH / 12 * 9;
public static final int SCALE = 2;
public final String TITLE = "2D Space Game";
private JFrame frame = new JFrame(TITLE);
private boolean running = false;
private Thread thread;
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private BufferedImage spriteSheet = null;
private BufferedImage player;
public void init(){
BufferedImageLoader loader = new BufferedImageLoader();
spriteSheet = loader.loadImage("/sprite_sheet.png");
SpriteSheet ss = new SpriteSheet(spriteSheet);
player = ss.grabImage(1, 1, 32, 32);
}
public void run(){
long lastTime = System.nanoTime();
final double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long now = 0;
int updates = 0, frames = 0;
long timer = System.currentTimeMillis();
init();
while(running){
now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
if(delta >= 1){
tick();
updates++;
delta--;
}
render();
frames++;
if(System.currentTimeMillis() - timer > 1000){
timer += 1000;
frame.setTitle("2-D Space Game || Updates: " + updates + ", FPS: " + frames);
frames = 0;
updates = 0;
}
}
stop();
}
private void tick(){
}
private void render(){
BufferStrategy bs = this.getBufferStrategy();
try {
Thread.sleep(2);
} catch (InterruptedException e){
e.printStackTrace();
}
if(bs == null){
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
/////////////////////////////////////
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
g.drawImage(player, 100, 100, this);
/////////////////////////////////////
g.dispose();
bs.show();
}
private synchronized void start(){
if(running) return;
running = true;
thread = new Thread(this);
thread.start();
}
private synchronized void stop(){
if(!running) return;
try{
thread.join();
}catch(InterruptedException e){
e.printStackTrace();
}
System.exit(1);
}
public static void main(String args[]){
Game game = new Game();
game.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
game.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
game.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
game.frame.add(game);
game.frame.pack();
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setResizable(false);
game.frame.setLocationRelativeTo(null);
game.frame.setVisible(true);
game.start();
}
}
SpriteSheet.java
package com.game.src.main;
import java.awt.image.BufferedImage;
public class SpriteSheet {
private BufferedImage image;
public SpriteSheet(BufferedImage image){
this.image = image;
}
public BufferedImage grabImage(int col, int row, int width, int height){
return image.getSubimage((col * 32) - 32, (row * 32) - 32, width, height);
}
}
BufferedImageLoader.java
package com.game.src.main;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class BufferedImageLoader {
private BufferedImage image;
public BufferedImage loadImage(String path){
try {
image = ImageIO.read(getClass().getResource(path));
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
return image;
}
}
Now here is the stacktrace that shows up:
Exception in thread "Thread-0" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1388)
at com.game.src.main.BufferedImageLoader.loadImage(BufferedImageLoader.java:13)
at com.game.src.main.Game.init(Game.java:30)
at com.game.src.main.Game.run(Game.java:45)
at java.lang.Thread.run(Thread.java:745)
Can anyone help?
BTW:
sprite_sheet.png does exist
Ok I figured it out. My sprite_sheet.png was in my "res" folder, and i needed to add it to the build path
The issue here is with the way you are addressing your png, and that the location you are specifying is not in your build path, or where you expect it to be. If you provide me the pwdon your location I will update with actual path you should use
You either need to provide the full path
/home/me/myprogram/sprite_sheet.png
or use relative path
./sprite_sheet.png
Your program is looking for the file in the root directory /
You need to provide a full path relative to your CLASSPATH, or a relative path relative to the current class's package.
The error occured when I typed private SpriteSheet spriteSheet = new SpriteSheet("/sprite_sheet.png");
When I commented out that part, the program worked fine.
Any ideas? My pattern is 8-bit and the spritesheet is 32x32
Game.java:
package ca.swimmerwoad.adventuregame;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
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 ca.swimmerwoad.adventuregame.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();
private 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 ticks = 0;
int frames = 0;
long lastTimer = System.currentTimeMillis();
double delta = 0;
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
boolean shouldRender = true;
while (delta >= 1) {
ticks++;
tick();
render();
delta -= 1;
shouldRender = true;
}
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(shouldRender) {
frames++;
render();
if (System.currentTimeMillis() - lastTime > 1000) {
lastTimer +=1000;
System.out.println(frames + "," + ticks);
frames = 0;
ticks = 0;
}
}
}
}
public void tick() {
tickCount++;
for (int i=0;i < pixels.length; i++) {
pixels[i] = i + tickCount;
}
}
public void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0,0,getWidth(), getHeight());
g.drawImage(image, 0,0, getWidth(), getHeight(), null);
g.dispose();
bs.show();
}
public static void main(String[] args) {
new Game().start(); }
}
SpriteSheet.java
package ca.swimmerwoad.adventuregame.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);
for(int i = 0; i<pixels.length; i++ ){
pixels[i] = (pixels[i] & 0xff) / 64;
}
for(int i = 0; i<8; i++) {
System.out.println(pixels[i]);
}
}
}
And my error is:
Exception in thread "main" java.lang.NullPointerException
at sun.misc.MetaIndex.mayContain(Unknown Source)
at sun.misc.URLClassPath$JarLoader.getResource(Unknown Source)
at sun.misc.URLClassPath.getResource(Unknown Source)
at sun.misc.URLClassPath.getResource(Unknown Source)
at java.lang.ClassLoader.getBootstrapResource(Unknown Source)
at java.lang.ClassLoader.getResource(Unknown Source)
at java.lang.ClassLoader.getResource(Unknown Source)
at java.net.URLClassLoader.getResourceAsStream(Unknown Source)
at java.lang.Class.getResourceAsStream(Unknown Source)
at ca.swimmerwoad.adventuregame.gfx.SpriteSheet.<init>(SpriteSheet.java:20)
at ca.swimmerwoad.adventuregame.Game.<init>(Game.java:33)
at ca.swimmerwo
you have written
public SpriteSheet(String Path)
i think it should be
public SpriteSheet(String path)
otherwise the path from which you want to create the sprite,i.e the path variable is null, and hence the exception
the NPE is from this line
SpriteSheet.class.getResourceAsStream(path)
you have only caught IOException here
try {
image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path));
} catch (IOException e) {
e.printStackTrace();
}