• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

an applet compiled but can't run because of the nullPointerException

 
Mellihoney Michael
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

could anyone help me?
PS:
there is 1.gif and 2.gif in the code base dir.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mellihoney Michael
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mellihoney Michael
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mellihoney Michael
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then
how to solve it???
I don't want to see the graphics refresh on the applet...
[ October 10, 2003: Message edited by: Mellihoney Michael ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this piece of your code,

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.
 
Mellihoney Michael
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic