Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    paul6lazo is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default Please help me. Parent and Child Class with Swing

    here is an overview of what i intended to do. i have a parent class namely OpAmps, and two Child Classes. The user will provide values for input resistance, feedback resistance and input voltage. When i click compute, it will show the Vout on textField4 and textField5. and when i click cancel the textfield will be empty. my problem is that i cant make it work. below is the code i made. please help me revised it to make it work. it is required to have parent/child class. thanks to all..

    here is the code
    java.txt

    here is the desired output
    Please help me. Parent and Child Class with Swing-java.jpg
    Last edited by paul6lazo; 09-07-2011 at 03:48 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: Please help me. Parent and Child Class with Swing

    What does "i cant make it work" mean? Do you see an error? Some weird behavior? Does it start smart-mouthing you? Does it gain sentience and go back in time to kill Sarah Connor to prevent the human resistance leader from overtaking the machines?

    Please read the link in my signature on asking questions the smart way, and please post an SSCCE with a specific question.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Please help me. Parent and Child Class with Swing

    Quote Originally Posted by KevinWorkman View Post
    What does "i cant make it work" mean? ....
    It looks like it's the silly season again, the time of homework dumps.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: Please help me. Parent and Child Class with Swing

    Quote Originally Posted by Fubarable View Post
    It looks like it's the silly season again, the time of homework dumps.
    Yep, I was just commenting on another forum about that. Batten down the hatches!
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: Please help me. Parent and Child Class with Swing

    Use code tags (see Fubarables links) when posting code.

    When you have errors (and this lot will not compile, so it's compilation errors) post the full message here.

  6. #6
    paul6lazo is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default Re: Please help me. Parent and Child Class with Swing

    i dont know what you mean.. i revised some part of the code. i just include a text file for the code.. here it is
    java.txt

    the error occurs after i clicked the compute. below is the error
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at OpAmps.OpAmps.<init>(OpAmps.java:18)
    	at OpAmps.InvertingAmplifier.<init>(InvertingAmplifier.java:3)
    	at OpAmps.OpAmps$1.actionPerformed(OpAmps.java:48)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$000(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    Last edited by paul6lazo; 09-07-2011 at 03:33 PM.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: Please help me. Parent and Child Class with Swing

    I have no idea what you;re running, but that first class does not compile.
    Java Code:
    public abstract class OpAmps {
        public double Rf = textField2.getText;
        public double Ri = textField1.getText;
        public double Vi = textField3.getText;
    None of those textXXX variables exist, and getText is not an attribute of the ones you do declare (in a method) further down.

    So the code you posted here is not the code you have just run.

  8. #8
    paul6lazo is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default Re: Please help me. Parent and Child Class with Swing

    what should i do? here what happens. eclipse said that textField1, textField2 and textField3 cannot be resolved. and eclipse suggest to add a field parameter and it became like
    Java Code:
     
                    public TextComponent textField1;
    		public TextComponent textField2;
    		public TextComponent textField3;
    		double Ri = Double.parseDouble(textField1.getText());
    		double Rf = Double.parseDouble(textField2.getText());
    		double Vi = Double.parseDouble(textField3.getText());
    		abstract double getVout();
    i edited my first post to remove the confusion. my code is in the text file
    Last edited by paul6lazo; 09-07-2011 at 03:47 PM.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Please help me. Parent and Child Class with Swing

    Quote Originally Posted by paul6lazo View Post
    what should i do?
    Go back to basics till you understand them.
    The Java™ Tutorials

    db

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: Please help me. Parent and Child Class with Swing

    Eclipse is offering suggestions, but when things are badly wrong then those suggestions are little more than guesses and should not be accepted as valid unless you know what you're doing.

    What's the point of assigning value to those three doubles from the text fields when those text fields have no data in them (indeed they are null at that point)?

    Surley you only want to do the assigment when the user has actually entered some values.

  11. #11
    paul6lazo is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default Re: Please help me. Parent and Child Class with Swing

    so assignning double is not correct because they are null at that point. so what will be the solution for that? how will i assigned it after the user put the values.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: Please help me. Parent and Child Class with Swing

    You really need to read the tutorials, since we are rapidly approaching the point where we are having to teach you some pretty basic concepts.

  13. #13
    paul6lazo is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default Re: Please help me. Parent and Child Class with Swing

    isn't it similar to JOptionPane
    for example:
    Java Code:
     
    String num=JOptionPane.showInputDialog("Enter the number");
    int number = Integer.parseInt(num);
    during load up it is also null but i already assign integer into it.

    what part of the basic are you referring to? pls be specific so that i can read that.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: Please help me. Parent and Child Class with Swing

    Classes, attributes, methods, scope.
    All the stuff that's in the first chunk of the tutorials linked to above.

  15. #15
    paul6lazo is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default Re: Please help me. Parent and Child Class with Swing

    ok thanks for the comments. eventhough in the end i didn't get any answers at all. so now it all depends on me.

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: Please help me. Parent and Child Class with Swing

    You were pointed at the tutorials once it became clear there was too much to explain.
    What more can you expect?

  17. #17
    paul6lazo is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default Re: Please help me. Parent and Child Class with Swing

    i posted in this forum because i need a quick answer. i have limited time. if i will be just pointed to tutorials so posting here in the first place is pointless.
    is it too much of a hassle by pointing what exactly is wrong with my code?

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: Please help me. Parent and Child Class with Swing

    Without rewriting it, yes.
    You seem to have fundamental problesm around scoping that can't simply be explained. You need to go through the basic tutorials without trying to write a GUI.

  19. #19
    paul6lazo is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default Re: Please help me. Parent and Child Class with Swing

    i already did it without gui, i use JOptionPane and it was a success.
    last question how will i delete my account here? because i only receive criticisms, not only you but also from others.
    i will just figure it myself. thanks for your time though.

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Please help me. Parent and Child Class with Swing

    Here is your code for all to see better and with one public class so it all compiles in one file:

    Java Code:
    package yr11.m09.a;
    
    import java.awt.GridLayout;
    import java.awt.TextComponent;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    abstract class OpAmps {
    
       public TextComponent textField1;
       public TextComponent textField2;
       public TextComponent textField3;
       double Ri = Double.parseDouble(textField1.getText());
       double Rf = Double.parseDouble(textField2.getText());
       double Vi = Double.parseDouble(textField3.getText());
    
       abstract double getVout();
    
       public static JPanel myPanel() {
          JPanel panel = new JPanel();
          panel.setLayout(new GridLayout(6, 2));
          panel.add(new JLabel("Input Resistance:"));
          final JTextField textField1 = new JTextField();
          panel.add(textField1);
          panel.add(new JLabel("Feedback Resistance:"));
          final JTextField textField2 = new JTextField();
          panel.add(textField2);
          panel.add(new JLabel("Input Voltage:"));
          final JTextField textField3 = new JTextField();
          panel.add(textField3);
          panel.add(new JLabel("Vout for Non-Inverting:"));
          final JTextField textField4 = new JTextField();
          textField4.setEditable(false);
          panel.add(textField4);
          panel.add(new JLabel("Vout for Inverting:"));
          final JTextField textField5 = new JTextField();
          textField5.setEditable(false);
          panel.add(textField5);
          JButton button1 = new JButton("Compute");
          panel.add(button1);
          button1.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent arg0) {
                OpAmps Inv = new InvertingAmplifier();
                textField5.setText(Double.toString(Inv.getVout()));
                OpAmps NonInv = new NonInvertingAmplifier();
                textField4.setText(Double.toString(NonInv.getVout()));
             }
          });
          JButton button2 = new JButton("Cancel");
          panel.add(button2);
          button2.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent arg0) {
                textField1.setText("");
                textField2.setText("");
                textField3.setText("");
                textField4.setText("");
                textField5.setText("");
             }
          });
          return panel;
       }
    }
    
    class InvertingAmplifier extends OpAmps {
       public double getVout() {
          return (Rf / Ri) * (-Vi);
       }
    
    }
    
    class NonInvertingAmplifier extends OpAmps {
       public double getVout() {
          return (1 + (Rf / Ri)) * (Vi);
       }
    }
    
    public class MainClass {
    
       public static void main(String[] args) {
          JFrame MyWindow = new JFrame("Operational Amplifiers");
          MyWindow.add(OpAmps.myPanel());
          MyWindow.setBounds(50, 100, 400, 130);
          MyWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          MyWindow.setVisible(true);
    
       }
    
    }
    There's an obvious problem here:

    Java Code:
       public TextComponent textField1;
       public TextComponent textField2;
       public TextComponent textField3;
       double Ri = Double.parseDouble(textField1.getText());
       double Rf = Double.parseDouble(textField2.getText());
       double Vi = Double.parseDouble(textField3.getText());
    Can you see that there is no way that this code will not cause NullPointerExceptions to occur? You've got un-initialized variables and then try to call methods on them. Then there are the issues of using static methods where it doesn't belong. Then there are issues of your actionPerformed which sorry to say is completely borked. I think you'll need to re-write this but to plan it out first before writing it.
    Last edited by Fubarable; 09-07-2011 at 05:15 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. substract Parent class object from child class
    By nikosv in forum New To Java
    Replies: 0
    Last Post: 12-08-2010, 12:30 AM
  2. Casting a child class into a parent class.
    By Unsub in forum New To Java
    Replies: 7
    Last Post: 01-30-2010, 01:39 AM
  3. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  4. Replies: 7
    Last Post: 05-06-2009, 12:42 PM
  5. Parent & Child window issues......
    By jithan in forum New To Java
    Replies: 2
    Last Post: 09-20-2008, 09:21 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
  •