Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-01-2007, 05:55 PM
Member
 
Join Date: Jul 2007
Posts: 40
fernando is on a distinguished road
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).

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); } } } }
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:
Code:
non-static variable height cannot be referenced from a static context at line 33 (33:34)
Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-01-2007, 11:25 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
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.
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; } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Explicit static initialization with the static clause Java Tip java.lang 0 04-17-2008 01:07 AM
Help with code (static error) oceansdepth New To Java 1 03-28-2008 06:32 AM
Error: Non-static method append(char) cannot be referenced from a static context paul Advanced Java 1 08-07-2007 07:05 AM
non-static member can not be referenced from a static context christina New To Java 1 07-31-2007 08:11 PM
Help with static variable counter silvia New To Java 1 07-19-2007 09:53 PM


All times are GMT +3. The time now is 03:37 PM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org