Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default 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.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Converted JFrame to JApplet, not getting null pointers for objects of classes

    cut and pasted the creation of new objects that was in the main method and put them in
    the init() method.
    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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default 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 01:23 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Converted JFrame to JApplet, not getting null pointers for objects of classes

    stack overflow error
    That is often caused by a recursive call in the constructor to the constructor. Check that the constructor does not call itself.\

    Have you read the tutorial about applets:
    http://docs.oracle.com/javase/tutori...let/index.html

  5. #5
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default 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.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Converted JFrame to JApplet, not getting null pointers for objects of classes

    Have you found the cause of the stack overflow?

  7. #7
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default 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.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Converted JFrame to JApplet, not getting null pointers for objects of classes

    so it uses more memory
    It will use all available memory. There is nothing to stop it from continuing except when it runs out of memory.

    not create multiple objects in the main method
    The main method is not used in applets. Look at using the init() method.

    yes I have looked at that.
    If you have read the tutorial about applets, you would know this.
    Last edited by Norm; 02-29-2012 at 08:42 PM.

  9. #9
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default 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)

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Converted JFrame to JApplet, not getting null pointers for objects of classes

    Hard to say without seeing the code.
    Normally initialization is done in a constructor for desktop apps and the init() method for applets.
    Doing too much in a main method can be a problem.

Similar Threads

  1. Positioning objects within a JApplet.
    By TaintedMike in forum New To Java
    Replies: 4
    Last Post: 01-30-2012, 05:56 AM
  2. JApplet in JFrame...
    By Rolf83 in forum Java Applets
    Replies: 8
    Last Post: 06-03-2011, 06:15 PM
  3. Making a JApplet into a JFrame into a .jar
    By aqualicy in forum Java Applets
    Replies: 2
    Last Post: 05-14-2010, 05:29 AM
  4. JFrame to JApplet or JApplet to JApplet
    By ramesh.8189 in forum AWT / Swing
    Replies: 13
    Last Post: 02-08-2009, 07:14 AM
  5. pointers and wrapper classes
    By becky in forum New To Java
    Replies: 11
    Last Post: 02-07-2009, 04:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •