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