2013-12-07 27 views
1

我的任務是創建一個實現GUI的Java程序,該GUI實現了有關需要身高和體重的BMI轉換器的GUI。目前,它是沒有完成,但我收到以下錯誤:在實現JFrame時線程「main」java.lang.NullPointerException中的異常

Exception in thread "main" java.lang.NullPointerException 
    at BMIGUI.<init>(BMIGUI.java:60) 
    at BMITest.main(BMITest.java:7) 

而與我的一個朋友調試它,他建議我的錯誤是,我還沒有在初始化代碼BMIGUI我的變量「框架」。但經過第二次看,這似乎是正確的,根據我..

請幫助。由於

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.text.DecimalFormat; 
public class BMIGUI extends JFrame 
{ 
    private final int WIDTH = 275; 
    private final int HEIGHT = 100; 

    DecimalFormat fmt = new DecimalFormat ("0.00"); 

    private JFrame frame; 
    private JPanel panel; 
    private JLabel heightLabel, weightLabel, BMILabel, resultLabel; 
    private JTextField height, weight; 
    private JButton calculate; 


    public BMIGUI() 
    { 
     super("BMI Converter"); 

     heightLabel = new JLabel ("Your height in meters:"); 
     weightLabel = new JLabel ("Your weight in kilograms: "); 

     //create a "this is your BMI" label 
     BMILabel = new JLabel ("Your BMI is ") ; 
     //create a result label to hold the BMI value 
     resultLabel = new JLabel ("") ; 

     //create a JTextField to hold the person's height in inches 
     height = new JTextField (5); 
     //create a JTextField to hold the person's weight in pounds 
     weight = new JTextField (5) ; 

     //create a button to press to calculate BMI 
     calculate = new JButton ("calculate BMI"); 
     //create a BMIListener and make it listen for the button to be pressed 
     calculate.addActionListener (new BMIListener()) ; 

     //set up the JPanel to go on the JFrame 
     panel = new JPanel(); 
     panel.setPreferredSize (new Dimension(WIDTH, HEIGHT)); 
     panel.setBackground (Color.yellow); 

     //add the height label and height textfield to the panel 
     panel.add (heightLabel) ; 
     panel.add (height) ; 
     //add the weight label and weight textfield to the panel 
     panel.add (weightLabel) ; 
     panel.add (weight) ; 
     //add the button to the panel 
     panel.add (calculate) ; 
     //add the BMI label to the panel 
     panel.add (BMILabel) ; 
     //add the label that holds the result to the panel 
     panel.add (resultLabel) ; 

     //add the panel to the frame 
    frame.getContentPane().add (panel); 
    } 


    public void display() 
    { 
     frame.pack(); 
     frame.setVisible(true); 
    } 


    private class BMIListener implements ActionListener 
    { 

     public void actionPerformed (ActionEvent event) 
     { 
     String heightText, weightText; 
     double heightVal, weightVal, bmi; 

    //get the text from the height and weight textfields 
    heightText = height.getText(); 
    weightText = weight.getText(); 

    //Use Integer.parseInt to convert the text to integer values 
    heightVal = Double.parseDouble (heightText) ; 
    weightVal = Double.parseDouble (weightText) ; 

    //Calculate the bmi = 703 * weight in pounds/(height in inches)^2 
    bmi = weightVal/(heightVal*heightVal) ; 

    //Put result in result label. Use Double.toString to convert double to string. 
    resultLabel.setText(fmt.format(bmi)) ; 

     } 
    } 
} 

主類

import javax.swing.JFrame; 

public class BMITest extends JFrame 
{ 
    public static void main(String args[]) 
    { 
     BMIGUI frame = new BMIGUI(); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(350,100); 
     frame.setVisible(true); 
    } 
} 

回答

1

您還沒有初始化,雖然你引用它在你的BMIGUI類上下文中聲明的frame。如:

frame.getContentPane().add (panel); 

等地。由於BMIGUI本身就是你的框架窗口,實際上你根本不需要在BIMGUI上下文中聲明另一個框架實例,用替換this,它將引用當前的引用實例。

這一直被認爲是很好的做法:

  1. 始終覆蓋getPreferredSize(Dimension)代替setPreferredSize()
  2. 調用pack(),而不是使用setSize(Dimension):該pack()方法尺寸框架,使所有的內容都在或高於其首選尺寸。

please,follow java's usual coding convention。班級的第一個名字後面以大寫字母開頭,但所有的字母都不應該是,例如BIMGUI應該是BimGui。我們全部使用大寫字母來表示恆領域:中public static final int DATAMARKER

+0

我明白你所說的提示是關於命名約定的。謝謝你的提示。我會毫不猶豫地記住它。至於框架,是行「私人JFrame框架」;在BMIGUI構造函數之外的實例變量中是不正確還是誤用? – Codex

+0

謝謝。重新閱讀你的陳述後,我明白了。它現在正在工作。非常感謝! – Codex

+0

請務必檢查[關於頁面](http://stackoverflow.com/about)以瞭解本網站的工作方式並歡迎使用StackOverflow – Sage

0
add (panel); 

代替

frame.getContentPane().add (panel); 

爲什麼在擴展一個JFrame類聲明一個JFrame?

+0

謝謝!你一直在幫助很大! – Codex

相關問題