动画示例中的Java语言指针异常?

动画示例中的Java语言指针异常?,java,compiler-errors,Java,Compiler Errors,动画课 Exception in thread "main" java.lang.NullPointerException at Images.MovieLoop(Images.java:45) at Images.run(Images.java:26) at Images.main(Images.java:8) 我之所以包含animation类,是因为编译器将突出显示这三个方法调用作为问题的根源 import java.awt.Image; import java.util.*; publi

动画课

Exception in thread "main" java.lang.NullPointerException
at Images.MovieLoop(Images.java:45)
at Images.run(Images.java:26)
at Images.main(Images.java:8)
我之所以包含animation类,是因为编译器将突出显示这三个方法调用作为问题的根源

import java.awt.Image;
import java.util.*;
public class Animation {

private ArrayList scenes;
private int sceneindex;
private long movietime;
private long totaltime;
//CONSTRUCTOR
public Animation(){
    scenes = new ArrayList();
    totaltime =0;
    start();

}
//Add scene to array list and sets time for each scene
//For example. If you have 3 scenes you would add t to total time three times. So if you had 
//3 Scenes, one for 1s, one for 2s, one for 3s. Total time would be 6s. and you would call addscene 3 times
public synchronized void addScene(Image i, long t)
{ 
    totaltime+=t;
    scenes.add(new OneScene(i, totaltime));

}
//start animation from beggininign inignngingingnig 
public synchronized void start(){
movietime = 0;
sceneindex = 0;
}
//change scenes 


//movie time is the sum of all the time passed from last update
//If you have more than one scene. timepassed gets added to movietime.
//if movietime is greater than or equal to total time(ie. animation is complete) restart the animation
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;
    }
}
//Getscene
private OneScene getScene(int x){
    return (OneScene)scenes.get(x);
}
//Scenes are gonna be 
private class OneScene{
    Image pic;
    long endTime;

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

    }
}
}
请注意:这是一个很长的评论

让我们从
Graphics g=s.getFullScreenWindow().getGraphics()开始
-
getGraphics
从来都不是一个好主意,它可能返回
null

您不应该尝试从EDT以外的任何线程更新任何UI组件,也不应该以这种方式直接绘制到它,相反,您应该使用
paintComponent

切勿
处理
非自己创建的任何
图形
上下文,这将阻止绘制其他组件

您应该避免覆盖
绘制
,尤其是顶层容器,如果没有其他原因,它不是双缓冲的(顶层容器),并且您还将覆盖任何其他子组件

查看更多详细信息


您应该尝试使用
ImageIO
而不是
ImageIcon
<如果无法读取文件,code>ImageIO
将抛出异常,其中as
ImageIcon
只是默默地失败,没有什么帮助。

显示错误消息欢迎使用StackOverflow!为了让其他人找到错误,如果您添加错误消息的stacktrace,这会有所帮助。'Exception In thread“main”java.lang.NullPointerException at Images.MovieLoop(Images.java:45)at Images.run(Images.java:26)at Images.main(Images.java:8)'我猜这是
Graphics g=s.getFullScreenWindow().getGraphics()
import java.awt.Image;
import java.util.*;
public class Animation {

private ArrayList scenes;
private int sceneindex;
private long movietime;
private long totaltime;
//CONSTRUCTOR
public Animation(){
    scenes = new ArrayList();
    totaltime =0;
    start();

}
//Add scene to array list and sets time for each scene
//For example. If you have 3 scenes you would add t to total time three times. So if you had 
//3 Scenes, one for 1s, one for 2s, one for 3s. Total time would be 6s. and you would call addscene 3 times
public synchronized void addScene(Image i, long t)
{ 
    totaltime+=t;
    scenes.add(new OneScene(i, totaltime));

}
//start animation from beggininign inignngingingnig 
public synchronized void start(){
movietime = 0;
sceneindex = 0;
}
//change scenes 


//movie time is the sum of all the time passed from last update
//If you have more than one scene. timepassed gets added to movietime.
//if movietime is greater than or equal to total time(ie. animation is complete) restart the animation
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;
    }
}
//Getscene
private OneScene getScene(int x){
    return (OneScene)scenes.get(x);
}
//Scenes are gonna be 
private class OneScene{
    Image pic;
    long endTime;

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

    }
}
}
    a.update(timepassed); 
    MovieLoop(); 
     I.run(dm);