Levi Allon Levi Allon - 6 months ago 33
Java Question

Why do I get a null pointer exception from .run()?

My code makes an animation. I have no clue why there is an exception, I think it is related to my run method in my first class.

The exception:

Exception in thread "main" java.lang.NullPointerException
at Anime_Main.movLoop(Anime_Main.java:43)
at Anime_Main.run(Anime_Main.java:28)
at Anime_Main.main(Anime_Main.java:9)


Class 1 (Main class):

import javax.swing.*;
import java.awt.Image;
import java.awt.*;
public class Anime_Main {

public static void main(String[] args) {
DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
Anime_Main am = new Anime_Main();
am.run(dm);
}

private Screen s;
private Anime an;

public void loadPics() {
Image face1 = new ImageIcon("C:\\Test\\black_closed.jpg").getImage();
Image face2 = new ImageIcon("C:\\Test\\black_drag.jpg").getImage();
an = new Anime();
an.addScn(face2, 300);
an.addScn(face1, 300);
}

public void run(DisplayMode dm) {
Screen s = new Screen();
try {
s.setFullScreen(dm, new JFrame());
loadPics();
movLoop();
} finally {
s.restoreScreen();
}
}

public void movLoop() {
long startTime = System.currentTimeMillis();
long runTime = startTime;

while (runTime - startTime < 10000) {
long timePassed = System.currentTimeMillis() - runTime;
runTime += timePassed;
an.update(timePassed);

Graphics g = s.getFullScreenWindow().getGraphics();
draw(g);
g.dispose();
try {
Thread.sleep(200);
} catch (Exception e) {

}
}
}

public void draw (Graphics g) {
g.drawImage(an.getimage(), 0, 0, null);

}

}


My second Animation class:

import java.util.ArrayList;
import java.awt.Image;
public class Anime {
private ArrayList scenes;
private int scnIndex;
private long movieTime;
private long totTime;

public Anime() {
scenes = new ArrayList();
totTime = 0 ;
start();
}

public synchronized void addScn(Image i, long t) {
totTime += t;
scenes.add(new oneScn(i, totTime));
}

public synchronized void start() {
movieTime = 0;
scnIndex = 0;
}

public synchronized void update(long timePassed) {
if(scenes.size() > 1) {
movieTime += timePassed;
if (movieTime >= totTime) {
movieTime = 0;
scnIndex = 0;
}
while (movieTime > getScene(scnIndex).endTime) {
scnIndex++;
}

}
}

public synchronized Image getimage() {
if(scenes.size() == 0) {
return null;
} else {
return getScene(scnIndex).pic;
}
}

private oneScn getScene(int x) {
return (oneScn)scenes.get(x);
}

private class oneScn {
Image pic;
long endTime;

public oneScn(Image pic, long endTime) {
this.pic = pic;
this.endTime = endTime;
}
}
}


And my last class (The screen class which makes the code fullscreen etc.) :

import javax.swing.JFrame;
import java.awt.*;
public class Screen {

private GraphicsDevice device;

public Screen() {
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
device = env.getDefaultScreenDevice();

}

public void setFullScreen(DisplayMode dm, JFrame window) {
window.setUndecorated(true);
window.setResizable(false);
device.setFullScreenWindow(window);

if (dm != null && device.isDisplayChangeSupported()) {
try {
device.setDisplayMode(dm);
} catch (Exception e) {

}
}

}

public Window getFullScreenWindow () {
return device.getFullScreenWindow();
}

public void restoreScreen() {
Window w = device.getFullScreenWindow();
if(w != null) {
w.dispose();
}
device.setFullScreenWindow(null);
}
}

Answer

Debugging your code, you would have found the following: the method run() creates a new local variable s, and the member variable s is therefore never set. So the method movloop tries to call a method of a null value.

So in your first line of run method, replace Screen s = new Screen(); with this.s = new Screen();