Need help loading images

hey guys so im starting my first java game & I ran into a problem. What I’m trying to do do is load the sprites from the player class then run them in the main. Im gonna have 2 more classes for enemies doing the same thing. here is my code so u can see. Here’s the error.

Exception in thread “main” java.lang.NullPointerException
at Game.run(Game.java:41)
at Game.main(Game.java:32)

Main class

import java.awt.*;
import javax.swing.ImageIcon;

public class Game {
	public Renderer s;
	public PlayerShip player;
	private Image bg;
	private static final DisplayMode modes1[] = {
		new DisplayMode(800,600,32,0),
		new DisplayMode(800,600,24,0),
		new DisplayMode(800,600,16,0),
		new DisplayMode(640,480,32,0),
		new DisplayMode(640,480,24,0),
		new DisplayMode(640,480,16,0),

	};;
	public Ani f;
	Image sf1;
	Image sf2;
	Image sf3;
	public static void main(String argv[]) {
		DisplayMode dm = new DisplayMode(300, 240, 32, DisplayMode.REFRESH_RATE_UNKNOWN);
		Game g = new Game();
		g.run(dm);
	}

	public void run(DisplayMode dm2){
		s = new Renderer();
		try{
			DisplayMode dm = s.find1stcompatmode(modes1);
			s.setFullScreen(dm);
			bg = new ImageIcon("C:\\Documents and Settings\\n17289\\My Documents\\JCreator LE\\MyProjects\\CompSci Game\\Sprites\\Hard Vacuum\\Misc\\Test2.bmp").getImage();
			player.loadplayer();
			movieLoop();

		}finally{
			s.restoreScreen();
		}
	}
	public void movieLoop(){
		long startTime = System.currentTimeMillis();
		long cumTime = startTime;
		while(cumTime - startTime<2000){
			long timePassed = System.currentTimeMillis() - cumTime;
			cumTime += timePassed;
			//player.f.update(timePassed);
			Graphics2D g = s.getGraphics();
			draw(g);
			g.dispose();
			s.update();
			try{
				Thread.sleep(10);
			}catch(Exception ex){}
			}
		}
	public void draw(Graphics g){
		g.drawImage(bg,0,0, null);
		//g.drawImage(f.getImage(), 540,405, null);
	}

}

Player clas

import java.awt.*;
import javax.swing.ImageIcon;
public class PlayerShip{
	public Ani f;
	Image sf1;
	Image sf2;
	Image sf3;

	public PlayerShip() {
	}

	public void loadplayer(){
		sf1 = new ImageIcon("C:\\Documents and Settings\\n17289\\My Documents\\JCreator LE\\MyProjects\\CompSci Game\\Sprites\\ayership\\stable fight\\f1playerstable.png").getImage();
		sf2 = new ImageIcon("C:\\Documents and Settings\\n17289\\My Documents\\JCreator LE\\MyProjects\\CompSci Game\\Sprites\\ayership\\stable fight\\f2playerstable.png").getImage();
		sf3 = new ImageIcon("C:\\Documents and Settings\\n17289\\My Documents\\JCreator LE\\MyProjects\\CompSci Game\\Sprites\\ayership\\stable fight\\f3playerstable.png").getImage();
		f = new Ani();
		f.addScene(sf1, 250);
		f.addScene(sf2, 250);
		f.addScene(sf3, 250);
	}
}

Screen class

import java.awt.*;
import javax.swing.JFrame;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;

public class Renderer{
	
	private GraphicsDevice vc;
	
	public Renderer(){
		GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
		vc = e.getDefaultScreenDevice();
	}
	
	public DisplayMode[] getCompatibleDisplayModes(){
		return vc.getDisplayModes();
	}
	
	public DisplayMode find1stcompatmode(DisplayMode modes[]){
		DisplayMode goodModes[] = vc.getDisplayModes();
		for(int x=0; x<modes.length; x++){
			for(int y =0; y<goodModes.length;y++){
				if(displayModeMatch(modes[x], goodModes[y])){
					return modes[x];
				}
			}
		}
		return null;
	}
	public DisplayMode getCurrentDisplayMode(){
		return vc.getDisplayMode();
	}
	public boolean displayModeMatch(DisplayMode m1, DisplayMode m2){	
		if(m1.getWidth() != m2.getWidth() || m1.getHeight() != m2.getHeight()){
			return false;
		}
		if(m1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m1.getBitDepth() != m2.getBitDepth()){
			return false;
		}
		if(m1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m1.getRefreshRate() != m2.getRefreshRate()){
			return false;
		}
		return true;
	}
	
	public void setFullScreen(DisplayMode dm){
		JFrame f = new JFrame();
		f.setUndecorated(true);
		f.setIgnoreRepaint(true);
		f.setResizable(false);
		vc.setFullScreenWindow(f);
		if(dm != null && vc.isDisplayChangeSupported()){
			try{
				vc.setDisplayMode(dm);
			}catch(Exception ex){}
		}
		f.createBufferStrategy(2);
	}
	
	public Graphics2D getGraphics(){
		Window w = vc.getFullScreenWindow();
		if(w != null){
			BufferStrategy s = w.getBufferStrategy();
			return(Graphics2D)s.getDrawGraphics();
		}else{
			return null;
		}
	}
	
	public void update(){
		Window w = vc.getFullScreenWindow();
		if(w != null){
			BufferStrategy s = w.getBufferStrategy();
			if(!s.contentsLost()){
				s.show();
			}
		}
	}
	public Window getFullScreenWindow(){
		return vc.getFullScreenWindow();
	}
	
	public int getWidth(){
		Window w = vc.getFullScreenWindow();
		if(w != null){
			return w.getWidth();
		}else{
			return 0;
		}
	}
	public int getHeight(){
		Window w = vc.getFullScreenWindow();
		if(w != null){
			return w.getHeight();
		}else{
			return 0;
		}
	}
	public void restoreScreen(){
		Window w = vc.getFullScreenWindow();
		if(w != null){
			w.dispose();
		}
		vc.setFullScreenWindow(null);
	}
	
	public BufferedImage createCompatibleImage(int w, int h, int t){
		Window win = vc.getFullScreenWindow();
		if(win != null){
			GraphicsConfiguration gc = win.getGraphicsConfiguration();
			return gc.createCompatibleImage(w,h,t);
		}
		return null;
	}
}

Animation class

import java.awt.Image;
import java.util.ArrayList;

public class Ani {
	
	private ArrayList<OneScene> scenes;
	private int sceneIndex;
	private long movieTime;
	private long totalTime;
	
	public Ani(){
		scenes = new ArrayList<OneScene>();
		totalTime = 0;
		start();
	}
	
	public synchronized void addScene(Image i, long t){
		totalTime+= t;
		scenes.add(new OneScene(i, totalTime));
	}
	public synchronized void start(){
		movieTime = 0;
		sceneIndex = 0;
	}
	
	public synchronized void update(long timePassed){
		if(scenes.size()>1){
			movieTime += timePassed;
			if (movieTime >= totalTime){
				movieTime = 0;
				sceneIndex = 0;
			}
			while(movieTime >  getScene(sceneIndex).endTime){
				sceneIndex++;
			}
		}
	}
	public synchronized Image getImage(){
		if(scenes.size()==0){
			return null;
		}else{
			return getScene(sceneIndex).pic;
		}
	}
	protected OneScene getScene(int x){
		return (OneScene)scenes.get(x);
	}
}
	class OneScene{
		Image pic;
		long endTime;
		public OneScene(Image pic, long endTime){
			this.pic = pic;
			this.endTime = endTime;
		}
}

thx in advance guys. Im probably gonna be back with more questions

I dont think Java can load the Windows-specific .bmp files. Try to convert it into a gif or jpeg if loading it via ImageIcon.

You get this Exception if you try to call a method or access a property on a null object.

Just look at Game.java line 41 (the real line 41 - the posted line numbers doesn’t match the error!).

I would suspect that the problem is player.loadPlayer() since you call loadPlayer() on the player variable that is uninitialized, so defaults to null…

Java does support bitmap images.

You are absolutely correct because it says the call to run(DisplayMode) is called at line 32 in main(String[]), which is line 24 in his code paste. So assuming there is an 8-line difference, line 33 is where the error is being thrown, which is “player.loadPlayer()”. Since “player” has not been initialized, then the NullPointerException is being thrown.

EDIT: @OP, Aha! You’ve been watching Bucky’s (thenewboston’s) Java Gaming Youtube Tutorials haven’t you? I recognized that Ani and Renderer class from somewhere: Bucky’s Animation and Screen class. I still use the Animation class for my 2D animations to this day :wink:

OH LOL thx guys. Im such an idiot. I cant believe i didnt initialize the object lol. Yeah i got that class from thenewboston :stuck_out_tongue: I figured I might aswell get started somewhere. thx guys I RLY appreciate it :smiley:

Glad to help :slight_smile:

In order to keep the number of new threads down I’ll just ask my question here. I created another animation which I’m trying to load. this time i tried to not make any stupid mistakes :stuck_out_tongue: Ill post the parts i changed. this is in the Game class only. I have everything loaded & playing except the new animation.

	public void run(DisplayMode dm2){
		s = new Renderer();
		try{
			DisplayMode dm = s.find1stcompatmode(modes1);
			s.setFullScreen(dm);
			loadmenu();
			player.loadplayer();
			movieLoop();
		}finally{
			s.restoreScreen();
		}
	}
	public void movieLoop(){
		long startTime = System.currentTimeMillis();
		long cumTime = startTime;
		while(cumTime - startTime<2000){
			long timePassed = System.currentTimeMillis() - cumTime;
			cumTime += timePassed;
			player.f.update(timePassed);
			a.update(timePassed);
			Graphics2D g = s.getGraphics();
			draw(g);
			g.dispose();
			s.update();
			try{
				Thread.sleep(10);
			}catch(Exception ex){}
			}
		}
	public void loadmenu(){
		bg = new ImageIcon("J:\\New folder (2)\\Untitled-2.jpg").getImage();
		txt1 = new ImageIcon("C:\\Users\\Nima\\workspace\\ulimate gunner.png").getImage();
		txt2 = new ImageIcon("C:\\Users\\Nima\\workspace\\ulimate gunner2.png").getImage();
		txt3 = new ImageIcon("C:\\Users\\Nima\\workspace\\ulimate gunner3.png").getImage();
		txt4 = new ImageIcon("C:\\Users\\Nima\\workspace\\ulimate gunner4.png").getImage();
		a = new Ani();
		a.addScene(txt1, 80);
		a.addScene(txt2, 80);
		a.addScene(txt3, 80);
		a.addScene(txt4, 80);
	}
	
	public void draw(Graphics2D g){
		g.drawImage(bg,0,0, null);
		g.drawImage(player.f.getImage(), 400,450, null);
		g.drawImage(a.getImage(), 400, 150, null);
	}

thx in advance.

We don’t need to keep the number of threads down, so this could have been fine as a new thread.

Anyway, in your movieLoop() method, your while loop keeps running until the difference between cumTime and startTime equals or exceeds 2000 milliseconds, so your animation only runs for 2 seconds. Did you mean your animation is not running at all? There doesn’t seem to be any errors or bugs so everything should work as is.

Also, this line “cumTime += timePassed” could be replaced with “cumTime = System.currentTimeMillis()” since they both do the exact same thing.

oh i c sry about that.
yeah the animation doesn’t run at all :frowning:

It really does look fine. Try putting in System.out.printlns in the loop and other methods to see if they are actually being called.

already tried that :frowning:

What did you see? Were the methods being called?

Ok so apparently my images are messed up :stuck_out_tongue: lol. Other pictures work fine. I just don’t know exactly whats wrong with it. I can see it find on my picture viewer, but…when I load the image i can’t see it at all… I can’t wait till I start to get the hang of this junk. Just remade the image

cumTime
Just what kind of program are you making, young man?

Yay someone uses JCrestorLE like me! 8)

You want an animation of the player? but you didn’t update its locations :slight_smile:


public void draw(Graphics2D g){
    g.drawImage(bg,0,0, null);
    g.drawImage(player.f.getImage(), 400,450, null);
    g.drawImage(a.getImage(), 400, 150, null);
}

and why must “cumTime” name? lol

What’s JCrestorLE, do you mean JCreator? And how does his code show that he uses it?
Also, please take another look at the Ani class. He is trying to animate, not move it.

Bahahahahah that’s what I thought at first but he actually means cumulative time :wink:

@r4king
typo, it must be JCreator. I know from here


bg = new ImageIcon("C:\\Documents and Settings\\n17289\\My Documents\\JCreator LE\\MyProjects\\CompSci Game\\Sprites\\Hard Vacuum\\Misc\\Test2.bmp").getImage();

:point:

Aha, I didn’t notice ::slight_smile:
But I still have better eyes than you :point:

Im making a very naught program :stuck_out_tongue: haha it means cumulative time haha. I don’t blame you tho. All my friends were laughing when they saw that haha. It turns out that my Image was using something called Indexed color & I had to change it to RGB. My animations are working fine now :slight_smile:

LoL what do you mean?

glad to hear that :slight_smile: