Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling an applet from another class of the same package

 
Rose Jac
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rose Jac
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic