Results 1 to 2 of 2
  1. #1
    fernando is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Error: non-static variable height cannot be referenced from a static context at line

    Hi, I have 2 classes (really 3 but the third doesn't need to be involved here)
    This program calculates the BMI of a person (I'll work on the formulas as soon as I can get this problem fixed)
    I want to get the input (text well it will be a number in this case) from the heights Text Field (GUI.java) (comments explaining at the code area) and put it in a formula under the class getHeight() (bodymassindex.java).

    Java Code:
    GUI .javapackage bmi;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.*;
    import bmi.GUI.*;
    
    public class GUI extends JFrame implements Observer {
    //Create JTextFields
    private JTextField heighttf;
    private JTextField weighttf;
    private JTextField bmitf;
    private JTextField wvtf;
    private bodymassindex Body;
    private static int height;
    
    
    //Default Constructor
    public GUI(bodymassindex bm) {
    super();
    Body = bm;
    Container contents = getContentPane();
    
    //Create JPanels
    JPanel panel1 = new JPanel();
    panel1.setLayout(new BorderLayout());
    
    JPanel panel2 = new JPanel();
    panel2.setLayout(new GridLayout (1, 3, 5, 20));
    
    JPanel panel3 = new JPanel();
    panel3.setLayout(new GridLayout (1, 3, 5, 20));
    
    JPanel panel4 = new JPanel();
    panel4.setLayout(new GridLayout (1, 2, 5, 20));
    
    //Create JTextFields
    JTextField heighttf = new JTextField();
    JTextField weighttf = new JTextField();
    JTextField bmitf = new JTextField();
    JTextField wvtf = new JTextField();
    
    
    //Set Editable to False on bmitf and wvtf
    heighttf.setEditable(true);
    weighttf.setEditable(true);
    bmitf.setEditable(false);
    wvtf.setEditable(false);
    
    //Create JLabels
    JLabel heightl = new JLabel("Height");
    JLabel weightl = new JLabel("Weight");
    JLabel bmil = new JLabel("BMI");
    
    //Create JButtons
    JButton calcb = new JButton("Calculate");
    
    //Add Panels to Panel1
    panel1.add(panel2,BorderLayout.NORTH);
    panel1.add(panel3,BorderLayout.CENTER);
    panel1.add(panel4,BorderLayout.SOUTH);
    
    //Add buttons, text fields, etc to panel2
    panel2.add(heightl);
    panel2.add(weightl);
    panel2.add(bmil);
    
    //Add buttons, text fields, etc to panel3
    panel3.add(heighttf);
    panel3.add(weighttf);
    panel3.add(bmitf);
    
    //Add buttons, text fields, etc to panel4
    panel4.add(wvtf);
    panel4.add(calcb);
    
    //Set Text Align to Center
    weighttf.setHorizontalAlignment(JTextField.CENTER);
    heighttf.setHorizontalAlignment(JTextField.CENTER);
    bmitf.setHorizontalAlignment(JTextField.CENTER);
    wvtf.setHorizontalAlignment(JTextField.CENTER);
    
    //Resgister Listener
    weighttf.addActionListener(new weighttfhandler());
    heighttf.addActionListener(new heighttfhandler());
    calcb.addActionListener(new calcbuttonhandler());
    
    //Frame information
    contents.add(panel1);
    setTitle("BMI Calculator");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(200, 100);
    setVisible(false);
    bmitf.setText("" +Body.getbmi());
    }
    
    
    
    public void update (Observable o, Object arg) {
    
    
    }
    
    private class calcbuttonhandler implements ActionListener {
    public void actionPerformed (ActionEvent e) {
    System.out.println("The JButton Works!");
    Body.calc();
    }
    }
    public class postActionEvent {
    String heightget = heighttf.getText();
    int height = Integer.parseInt(heightget);
    }
    public class heighttfhandler implements ActionListener {
    public void actionPerformed (ActionEvent e) {
    String heightget = heighttf.getText();
    int height = Integer.parseInt(heightget);
    System.out.println("Hello" + height);
    
    }
    }
    
    private class weighttfhandler implements ActionListener {
    public void actionPerformed (ActionEvent e) {
    if (e.getSource() == weighttf) {
    int weight =
    Integer.parseInt(weighttf.getText().trim());
    System.out.println("" + weight);
    }
    }
    }
    }
    Java Code:
    bodymassindex .java
    package bmi;
    
    import javax.swing.*;
    import java.util.*;
    
    public class bodymassindex extends Observable {
    private void GUI();
    private int height;
    private int weight;
    private int bmi;
    private char wv;
    private String heightString;
    
    
    public bodymassindex () {
    super();
    
    height = 0;
    weight = 0;
    bmi = 0;
    }
    
    public void calc() {
    getHeight();
    getWeight();
    getbmi();
    setwvtf();
    setChanged();
    notifyObservers();
    }
    
    public int getHeight() {
    height = GUI.postActionEvent.height;
    return height;
    }
    
    public int getWeight() {
    
    return weight;
    }
    public int getbmi() {
    getHeight();
    getWeight();
    bmi = weight + height;
    return bmi;
    }
    public char setwvtf() {
    return wv;
    }
    }
    I get an error:
    Java Code:
    non-static variable height cannot be referenced from a static context at line 33 (33:34)
    Thanks.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    The Observer/Observable can become awkward to implement because of having to extend Observable. I suggest either not using it or trying a PropertyChangeListener approach. If you can do without it you could have the gui update the Body through/via its listeners.
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    //import javax.*;
    //import bmi.GUI.*;
    
    public class BMITest extends JFrame {
        //Declare JTextFields
        private JTextField heighttf;
        private JTextField weighttf;
        private JTextField bmitf;
        private JTextField wvtf;
        private Body body;
    //    private static int height;
    
        //Default Constructor
        public BMITest(Body body) {
            super();
            this.body = body;
            Container contents = getContentPane();
    
            //Create JPanels
            JPanel panel1 = new JPanel();
            panel1.setLayout(new BorderLayout());
    
            JPanel panel2 = new JPanel();
            panel2.setLayout(new GridLayout (1, 3, 5, 20));
    
            JPanel panel3 = new JPanel();
            panel3.setLayout(new GridLayout (1, 3, 5, 20));
    
            JPanel panel4 = new JPanel();
            panel4.setLayout(new GridLayout (1, 2, 5, 20));
    
            //Instantiate JTextFields
            // These local declarations hide the member variables.
            // Remove the declarations so the statements will/can
            // instantiate the member variables.
            /*JTextField*/ heighttf = new JTextField();
            /*JTextField*/ weighttf = new JTextField();
            /*JTextField*/ bmitf = new JTextField();
            /*JTextField*/ wvtf = new JTextField();
    
            //Set Editable to False on bmitf and wvtf
            heighttf.setEditable(true);
            weighttf.setEditable(true);
            bmitf.setEditable(false);
            wvtf.setEditable(false);
    
            //Create JLabels
            JLabel heightl = new JLabel("Height");
            JLabel weightl = new JLabel("Weight");
            JLabel bmil = new JLabel("BMI");
    
            //Create JButtons
            JButton calcb = new JButton("Calculate");
    
            //Add Panels to Panel1
            panel1.add(panel2,BorderLayout.NORTH);
            panel1.add(panel3,BorderLayout.CENTER);
            panel1.add(panel4,BorderLayout.SOUTH);
    
            //Add buttons, text fields, etc to panel2
            panel2.add(heightl);
            panel2.add(weightl);
            panel2.add(bmil);
    
            //Add buttons, text fields, etc to panel3
            panel3.add(heighttf);
            panel3.add(weighttf);
            panel3.add(bmitf);
    
            //Add buttons, text fields, etc to panel4
            panel4.add(wvtf);
            panel4.add(calcb);
    
            //Set Text Align to Center
            weighttf.setHorizontalAlignment(JTextField.CENTER);
            heighttf.setHorizontalAlignment(JTextField.CENTER);
            bmitf.setHorizontalAlignment(JTextField.CENTER);
            wvtf.setHorizontalAlignment(JTextField.CENTER);
    
            //Resgister Listener
            weighttf.addActionListener(new weighttfhandler());
            heighttf.addActionListener(new heighttfhandler());
            calcb.addActionListener(new calcbuttonhandler());
    
            //Frame information
            contents.add(panel1);
            setTitle("BMI Calculator");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(200, 100);
            setVisible(false);
            bmitf.setText("" + body.getbmi());
        }
    
        private class calcbuttonhandler implements ActionListener {
            public void actionPerformed (ActionEvent e) {
                //System.out.println("The JButton Works!");
                updateBody();
                int bmIndex = body.getbmi();
                System.out.println("bmi = " + bmIndex);
                bmitf.setText(String.valueOf(bmIndex));
            }
        }
    
        public class heighttfhandler implements ActionListener {
            public void actionPerformed (ActionEvent e) {
                String heightget = heighttf.getText();
                int height = Integer.parseInt(heightget);
                System.out.println("Hello" + height);
                body.setHeight(height);
            }
        }
    
        private class weighttfhandler implements ActionListener {
            public void actionPerformed (ActionEvent e) {
                if (e.getSource() == weighttf) {
                    int weight =
                        Integer.parseInt(weighttf.getText().trim());
                    System.out.println("" + weight);
                    body.setWeight(weight);
                }
            }
        }
    
        private void updateBody() {
            String w = heighttf.getText();
            int weight = w.equals("") ? 0 : Integer.parseInt(w);
            body.setHeight(weight);
            String h = weighttf.getText().trim();
            int height = h.equals("") ? 0 : Integer.parseInt(h);
            body.setWeight(height);
        }
    
        public static void main(String[] args) {
            BMITest test = new BMITest(new Body());
            test.setLocation(300,200);
            test.setVisible(true);
        }
    }
    
    class Body {
        private int height;
        private int weight;
        private int bmi;
        private char wv;
        private String heightString;
    
        public Body() {
            height = 0;
            weight = 0;
            bmi = 0;
        }
    
        public int getHeight() { return height; }
    
        public int getWeight() { return weight; }
    
        public void setHeight(int height) { this.height = height; }
    
        public void setWeight(int weight) { this.weight = weight; }
    
        public int getbmi() {
            bmi = weight + height;
            return bmi;
        }
    
        public char setwvtf() {
            return wv;
        }
    }

Similar Threads

  1. Replies: 3
    Last Post: 03-20-2009, 01:35 AM
  2. Replies: 0
    Last Post: 04-17-2008, 12:07 AM
  3. Help with code (static error)
    By oceansdepth in forum New To Java
    Replies: 1
    Last Post: 03-28-2008, 05:32 AM
  4. Replies: 1
    Last Post: 08-07-2007, 06:05 AM
  5. Help with static variable counter
    By silvia in forum New To Java
    Replies: 1
    Last Post: 07-19-2007, 08:53 PM

Posting Permissions

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