Results 1 to 10 of 10
- 02-29-2012, 07:01 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 9
- Rep Power
- 0
Converted JFrame to JApplet, not getting null pointers for objects of classes
Ok, so I have a Java application that I made a few days ago. It checks out as far as all of the basic capability checks are concerned. I have three classes (each its own .java file) and I create new objects from them and use the classes' setter methods to set those objects. When I run the program as a JFrame it works fine.
Here is what I did: change the class that extends JFrame (let's call it MainWindow) and change it to
MainWindow extends JApplet
added the init(), start(), stop() and destroy() methods
cut and pasted the creation of new objects that was in the main method and put them in
the init() method.
Then if I run the program (now a JApplet) in Eclipse the Objects are never set to anything other than null. I have no idea why everything checks out fine when the program runs in a JFrame, but if it is a JApplet it does not work. I assume it has something to do with something I don't know about JApplets, but I am pretty certain that the init() method is more or less the main method of a normal java program.
Thanks for your help.
- 02-29-2012, 11:50 AM #2
Re: Converted JFrame to JApplet, not getting null pointers for objects of classes
Don't know why you were creating new objects in the main method, other than an instance of the class it's contained in -- which you don't do with an Applet, since the class is instantiated by the plugin or AppletViewer. But one difference could be if you have a non-default constructor the code in which, for an Applet, runs before the init() method.cut and pasted the creation of new objects that was in the main method and put them in
the init() method.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-29-2012, 12:09 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 9
- Rep Power
- 0
Re: Converted JFrame to JApplet, not getting null pointers for objects of classes
I was under the impression the main method is where the new objects should be created (normally, I have no clue about JApplets...)
public static void main (String args[])
{
//In my program, I have three classes. I create new instances of them in main. Class 1 calls methods local to class2 and class3 objects, so I need to set the objects that I can reference.
Class 3 calls methods local to class 1 and class 3 objects.
class1 class1object = new class1();
class2 class2object = new class2();
class3 class3object = new class3();
//Class 1 calls methods local to class2 and class3 objects, so I need to set the objects that I can //reference.
class1object.setclass2object(class2object);
class1object.setclass3object(class3object);
//Class 2 calls methods local to class 1 objects, so I need to set the objects that I can reference.
class2object.setclass1object(class1object);
//Class 3 calls methods local to class1 and class2 objects, so I need to set the objects that I can //reference.
class3object.setclass1object(class1object);
class3object.setclass2object(class2object);
}
Where else, and how else would I create the objects?
It seems like this person is doing the same thing here:
java - Lesson 16 - Gas Mileage - Multiple Classes Project - Stack Overflow
As far as the JApplet is concerned, I'm not sure what you mean by a non-default constructor..
EDIT: If I take the object initializations above and put them in the constructor of class 1 (where main is if it is not a JApplet) I get a stack overflow error where a new instance of class 1 is created. I assume this has something to do with the java plugin or AppletViewer having already done that?Last edited by cplredhartsock; 02-29-2012 at 12:23 PM.
- 02-29-2012, 01:28 PM #4
Re: Converted JFrame to JApplet, not getting null pointers for objects of classes
That is often caused by a recursive call in the constructor to the constructor. Check that the constructor does not call itself.\stack overflow error
Have you read the tutorial about applets:
http://docs.oracle.com/javase/tutori...let/index.html
- 02-29-2012, 06:12 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 9
- Rep Power
- 0
Re: Converted JFrame to JApplet, not getting null pointers for objects of classes
I believe it comes down to my way of creating amd setting objects. In the example I gave above for what I did in he main method before I changed it to a JApplet I create a new object of each class and then set those objects to the local variables of each class. This works, but the moderator above spoke as if this was a bad way to do this. I could see if I had a ton of classes that having to use a setter for each object would result in a ton of code...
As far as the JApplet is concerned, yes I have looked at that.
- 02-29-2012, 06:16 PM #6
Re: Converted JFrame to JApplet, not getting null pointers for objects of classes
Have you found the cause of the stack overflow?
- 02-29-2012, 07:32 PM #7
Member
- Join Date
- Feb 2012
- Posts
- 9
- Rep Power
- 0
Re: Converted JFrame to JApplet, not getting null pointers for objects of classes
Yeah, it is when I do:
class1 class1object = new class1object();
inside of class1's constructor.
I did however fix my original issue of having a null pointer exception.
The java plugin (or the AppletViewer) creates a new instance of the class with init() (main class), so when I created another instance of the main class and set the class2 and class3 objects they weren't null *to the second instance of class1* but to the instance that was the JApplet they were.
So, to fix this I changed my code to:
// class2object doesn't reference a class3object so no set method is called
class2 class2object = new class2();
class3 class3object = new class3();
this.setPasswordSecurer(class2object);
this.setSqlInterface(class3object);
class2object.setMainWindow(this);
class3object.setInventoryManagerMainWindow(this);
class3object.setSecurePasswordsForStorage(password Securer);
and put it in class 1's constructor.
I also experimented and put that same code in the top of class1' (the main class) init() and that seemed to work as well.
***However, is one method a better place to put that code than another, that is, should object initializations happen in the init method or in the class constructor? On the JApplet tutorial page an example is given with two classes, one a JPanel subclass and one the JApplet class. In the JApplet class' init() the JPanel class is initialized, but I don't know if this is just standard practice or if doing that sort of thing in the constructor will cause issues.***
***Going back to DarrylBurke's comment: "Don't know why you were creating new objects in the main method, other than an instance of the class it's contained in -- which you don't do with an Applet, since the class is instantiated by the plugin or AppletViewer. But one difference could be if you have a non-default constructor the code in which, for an Applet, runs before the init() method."
If you should not create multiple objects in the main method, where do you create and initialize them? I am still not getting this as I was taught just to create a new object from each class in main (for a standalone JFrame application, not a JApplet) and then call setters as needed to initialize them. ***
**Also, I now know that a stack overflow will occur if a constructor calls itself. This is a recursive call that never ends, so it uses more memory than has been allocated for the call stack. Is this correct?**
Thanks.
- 02-29-2012, 07:39 PM #8
Re: Converted JFrame to JApplet, not getting null pointers for objects of classes
It will use all available memory. There is nothing to stop it from continuing except when it runs out of memory.so it uses more memory
The main method is not used in applets. Look at using the init() method.not create multiple objects in the main method
If you have read the tutorial about applets, you would know this.yes I have looked at that.Last edited by Norm; 02-29-2012 at 07:42 PM.
- 03-01-2012, 05:14 AM #9
Member
- Join Date
- Feb 2012
- Posts
- 9
- Rep Power
- 0
Re: Converted JFrame to JApplet, not getting null pointers for objects of classes
I know the main method is not used in applets, I'm asking if my method of object creation and initialization is the correct or best way (putting that stuff in init() for applets and main for standalone applications)
- 03-01-2012, 01:23 PM #10
Similar Threads
-
Positioning objects within a JApplet.
By TaintedMike in forum New To JavaReplies: 4Last Post: 01-30-2012, 04:56 AM -
JApplet in JFrame...
By Rolf83 in forum Java AppletsReplies: 8Last Post: 06-03-2011, 05:15 PM -
Making a JApplet into a JFrame into a .jar
By aqualicy in forum Java AppletsReplies: 2Last Post: 05-14-2010, 04:29 AM -
JFrame to JApplet or JApplet to JApplet
By ramesh.8189 in forum AWT / SwingReplies: 13Last Post: 02-08-2009, 06:14 AM -
pointers and wrapper classes
By becky in forum New To JavaReplies: 11Last Post: 02-07-2009, 03:59 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks