File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes Calling an applet from another class of the same package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Calling an applet from another class of the same package" Watch "Calling an applet from another class of the same package" New topic
Author

Calling an applet from another class of the same package

Rose Jac
Ranch Hand

Joined: May 20, 2011
Posts: 33
Hi,

I have an applet in class 'A'.
I have another class = B.
Now, I have created a set(...) method in A and this set method will be called by B after instantiating A and all the parameters required to build the applet wll be set.
And after that, i have called the init() method of A using the same A's object.

This is not working. A nullPointerException is present. I am not able to call the applet after setting the parameters of the applet.
Am i doing it the wrong way?

Thanks!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

The init() method is called after the object is created. Before this time the applet has no access to anything that makes it an applet. Especially methods like getCodeBase(), getDocumentBase() and getParameter(String) are not available until init() has been called.

Can you perhaps show us the exception, and the part of code where the exception is thrown?

I'll move this thread to our Applets forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rose Jac
Ranch Hand

Joined: May 20, 2011
Posts: 33
The applet:


The other class:



Ouptut:
Exception in thread "main" java.lang.NullPointerException
at manet.Ball.<init>(Motion.java:21)
at manet.Motion.set(Motion.java:67)
at manet.matt.main(matt.java:20)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Do help.
Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

Obviously the reference you're using on line 21 is null. That reference is b. So we trace back, and notice that it's bounds - a variable that isn't initialized until init() is called, which happens after you call set and create the balls. However, that will still give you a NullPointerException since Component.createImage, which you are calling on line 80 will return null if the component is not displayable. Your applet isn't like this because it's not contained in a web page, not shown in appletviewer and not shown at all. But I don't see the point in this entire Image though - it will create an off-screen image that you never display.

That whole code is a horrible mess though. I'm sure you've been told before to use a javax.swing.Timer instead of a thread. You are violating the rules specified in Concurrency in Swing. You are calling deprecated methods of Thread that you really shouldn't (stop() and resume()). You are using the results of getGraphics() which you never should.
Rose Jac
Ranch Hand

Joined: May 20, 2011
Posts: 33
Hi,

I am sorry for the trouble but i did read about timers. The thing is I do not have an ActionPerformed in my program.
The nth program i am trying (given below) does work fine but it is not the smooth motion i wanted to generate.

If there is a way to generate smooth motion using timers in this program, do mention.
And do note, i want the ball displayed in the frame to move every 20seconds and then stop for few seconds, say, 4 seconds. This continues till the window is closed.

Rose Jac
Ranch Hand

Joined: May 20, 2011
Posts: 33
And i want to display this window when an "OK" button is pressed in another class, which i am not able to do.
Do help.

Thanks
Rose Jac
Ranch Hand

Joined: May 20, 2011
Posts: 33
So sorry again. I have used timer here, though i dont think the circle is stopping at all!
And the circle is just too fast.


Thanks.
And do tell how to open this window with the press of a button in another class.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4522
    
    5

1. In an earlier thread of yours, it was suggested more than once that you use javax.swing.Timer. Not java.util.Timer, which requires special treatment when used in conjunction with Swing components.

2. Why do you have a while(true) loop in the TimerTask's run() method?


luck, db
There are no new questions, but there may be new answers.
 
 
subject: Calling an applet from another class of the same package
 
Similar Threads
Passing parameters to an Applet without using param tag
NullPointerException thrown when generating SWING GUI
Refresh problem in applet
Calling the Methods into another package
Session timeouts and listeners....