Codex
Codex

Reputation: 11

Exception in thread "main" java.lang.NullPointerException while implementing JFrame

My task is to create a java program implementing GUI about a BMI Converter that takes in height and weight. Currently it is no finished but i am receiving the following error:

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

while debugging it with a friend of mine, he suggested that my error is that i havent initialized my variable "frame" in BMIGUI code. But upon second look, it seems correct according to me..

Please help. Thanks

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

      }
   }
}

the main class

 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);
    }
}

Upvotes: 1

Views: 1728

Answers (2)

Sage
Sage

Reputation: 15418

You haven't initialized the frame declared in your BMIGUI class context although you are referencing it. Such as:

frame.getContentPane().add (panel);

And other places. As BMIGUI itself your frame window, In fact you don't need to declare another frame instance in BIMGUI context at all, replace frame reference with this which will refer to the current referencing instance.

This is always considered as good practice:

  1. always Override getPreferredSize(Dimension) instead of setPreferredSize()
  2. calling pack() instead of using setSize(Dimension) : The pack() method sizes the frame so that all its contents are at or above their preferred sizes.

please, follow java's usual coding convention. The first name of a class start with capital later, but all of the letter should not be, such as BIMGUI should be BimGui. We use all capital letter to represent constant field: public static final int DATAMARKER

Upvotes: 1

Fabio Mignogna
Fabio Mignogna

Reputation: 164

add (panel);

instead of

frame.getContentPane().add (panel);

why declare a JFrame in a class that extends a JFrame?

Upvotes: 0

Related Questions