There will be a NullPointerException on line 19 where you try to use "abstractImage," which will be null. "createImage" will fail if called from an applet constructor, because the applet's graphical environment won't be set up yet. You really shouldn't try to do any useful work in an applet's constructor -- do it in init(), instead, that's what it's for. Move all the constructor code to init() and you should be OK. I'm not quite sure what's with "update()", by the way; it explicitly calls paint(), which you've explicitly overridden to do precisely nothing. Are you confused about what's happening there, or is it just work in progress?
Thank you! As for the update() method,I just want to let the statements in it run - paint(offScreenGraphic);g.drawImage(abstractImg, 100, 100, this); - by calling the repaint() method. Of course,it is better to place the statements in the paint(),but I don't want to draw anything when the applet loads.
a beginner in java
posted 16 years ago
no,it still doesn't work.....
java.lang.NullPointerException at Test.init(Test.java:29) at sun.applet.AppletPanel.run(AppletPanel.java:341) at java.lang.Thread.run(Thread.java:536)
why? [ October 07, 2003: Message edited by: Mellihoney Michael ]
Well,let's see. I don't see anything that would cause a NPE here. I do see that the paint/update situation has gotten worse, because now paint() calls itself recursively, which will give a StackOverflowError.
the paint() method calls itself, always, the first thing it does, unconditionally. If you ever do this in Java or any other language, you'll get a stack overflow, because the call stack just gets deeper and deeper and deeper and nothing will ever stop it. The thing is, I just can't imagine why you're making that call. Why don't you just say
This will copy the image out of your offScreenGraphic onto the screen, which is what it looks like you want to accomplish. Otherwise, you could explain what you are trying to do and I'm sure someone can help you figure out how.
Thank you,it does work if I remove that line. ------------------------------------------------------------------ I don't know how it works because I am a freshman in this area. I just make a reference of the following code which simulates an absolute space.It is very cool.
But I don't know why the stack overflow cause a NullPointerException?
a beginner in java
permaculture is largely about replacing oil with people. And one tiny ad:
Devious Experiments for a Truly Passive Greenhouse!