This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Applets and the fly likes an applet compiled but can't run because of the nullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Applets
Bookmark "an applet compiled but can Watch "an applet compiled but can New topic
Author

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

Mellihoney Michael
Ranch Hand

Joined: Nov 27, 2002
Posts: 124

could anyone help me?
PS:
there is 1.gif and 2.gif in the code base dir.


a beginner in java
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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?


[Jess in Action][AskingGoodQuestions]
Mellihoney Michael
Ranch Hand

Joined: Nov 27, 2002
Posts: 124
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

Joined: Nov 27, 2002
Posts: 124
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Nov 27, 2002
Posts: 124
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Nov 27, 2002
Posts: 124
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: an applet compiled but can't run because of the nullPointerException