compiler errors - Java Lang nullpointer exception in animation example? -
import java.awt.*; import javax.swing.imageicon; import javax.swing.jframe; public class images extends jframe { public static void main(string[] args) { displaymode dm = new displaymode(800,600,32, displaymode.refresh_rate_unknown); images = new images(); i.run(dm); } private screen s; private image bg; private image pic; private boolean nloaded; animation a; public void run(displaymode dm) { nloaded = false; s = new screen(); try{ s.setfullscreen(dm, this); loadpics(); movieloop(); try{ thread.sleep(50000); }catch(exception ex){} }finally{ s.restorescreen(); } } public void movieloop(){ long startingtime = system.currenttimemillis(); long cumtime = startingtime; while(cumtime-startingtime < 5000) { long timepassed = system.currenttimemillis() - cumtime; cumtime += timepassed; a.update(timepassed); graphics g = s.getfullscreenwindow().getgraphics(); draw(g); g.dispose(); try{ thread.sleep(20); }catch(exception ex){} } } public void draw(graphics g) { g.drawimage(bg, 0,0, null); g.drawimage(a.getimage(),0,0,null); } //create load pictures public void loadpics() { bg = new imageicon("c:\\gamepics\\backgroundimage.jpg").getimage(); pic = new imageicon("c:\\gamepics\\smileyicon3.png").getimage(); nloaded = true; repaint(); } public void paint(graphics g){ if(g instanceof graphics2d){ graphics2d g2 = (graphics2d)g; g2.setrenderinghint(renderinghints.key_text_antialiasing, renderinghints.value_text_antialias_on); } if(nloaded) { g.drawimage(bg, 0, 0, null); g.drawimage(pic, 300,300, null); } } }
im not understanding did wrong ive overlooked best can. im practicing simple animation , keep getting 3 null pointer exceptions. ive researched best can , apparently nullpointerexceptions in java have trying size of null arrays? compiler hasn't marked of lists problems im little confused. appreciated. of errors commented out. there 3 of them , in images class
errors:
exception in thread "main" java.lang.nullpointerexception @ images.movieloop(images.java:45) @ images.run(images.java:26) @ images.main(images.java:8)
animation class
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 array list , sets time each scene //for example. if have 3 scenes add t total time 3 times. if had //3 scenes, 1 1s, 1 2s, 1 3s. total time 6s. , call addscene 3 times public synchronized void addscene(image i, long t) { totaltime+=t; scenes.add(new onescene(i, totaltime)); } //start animation beggininign inignngingingnig public synchronized void start(){ movietime = 0; sceneindex = 0; } //change scenes //movie time sum of time passed last update //if have more 1 scene. timepassed gets added movietime. //if movietime greater or equal total time(ie. animation complete) restart 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 gonna private class onescene{ image pic; long endtime; public onescene(image pic, long endtime) { this.pic = pic; this.endtime = endtime; } } }
i included animation class because compiler highlighting these 3 method calls source of problem
a.update(timepassed); movieloop(); i.run(dm);
please note: long comment
let's start graphics g = s.getfullscreenwindow().getgraphics();
- getgraphics
never idea, can return null
.
you should never try , update ui component thread other edt , should never draw directly in manner, instead, should using paintcomponent
.
you should never dispose
of graphics
context did not create yourself, prevent other components been painted.
you should avoid overriding paint
, of top level container, if no other reason, it's not double buffered (the top level container), , painting on other child components.
check out performing custom painting more details.
you should try using imageio
instead of imageicon
. imageio
throw exceptions if can't read file, imageicon
fails silently, no helpful.
Comments
Post a Comment