0

I am creating a body mass index calculator to practice creating a GUI. However, I cannot figure out why I am getting the error shown below. I'm thinking that I'm trying to display the value of BMI incorrectly. Can someone help please?

Exception in thread "main" java.lang.NullPointerException at Source.(Source.java:21) at Main.main(Main.java:5)

import javax.swing.JFrame; 

public class Main {
public static void main (String args []) {
    Source sourceObject = new Source();
    sourceObject.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    sourceObject.setSize(275,180); 
    sourceObject.setVisible(true); 

}

}

import java.awt.FlowLayout; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane;

public class Source extends JFrame { 

private JLabel item1;
private JLabel item2;
private JLabel item3;
private String weight, height;
private int BMI;

public Source () {
    super("Title"); 
    setLayout(new FlowLayout()); 
    item1 = new JLabel("Text"); 
    item1.setToolTipText("This appears on hover"); 
    weight = JOptionPane.showInputDialog(null, "Weight: ");
    height = JOptionPane.showInputDialog(null, "Height: ");
    item3.setText(String.valueOf(BMI));
    add(item1);
    add(item2);
    add(item3);

}
int BMICalc() {
    int weig = Integer.parseInt(weight);
    int heig = Integer.parseInt(height);
    int BMI = (weig)/(heig * heig);
    return BMI;

}


}
NineBerry
  • 26,306
  • 3
  • 62
  • 93
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – xenteros Jan 21 '17 at 22:35

2 Answers2

1

Actually item2 and item3 are declared but never instantiated.

But the actual problem is you are calling a method on item3.

Instantiate all the fields that have a known value in the Source constructor.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • Where do you see me calling a method on item2? – helloumarian Jan 21 '17 at 21:53
  • I would say item3 sorry – davidxxx Jan 21 '17 at 21:53
  • I still dont understand what is wrong, can you please explain a little bit more? – helloumarian Jan 21 '17 at 21:57
  • item3 is null and you call a method on it. A variable referencing a null object should be first instantiated if you want to perform method invocations on. In the constructor of Source, instantiate the label : `item3 = new JLabel();` – davidxxx Jan 21 '17 at 22:08
  • Any ideas why the result is always 0? – helloumarian Jan 21 '17 at 22:33
  • The solution is hidden in my answer below ;) – Andreas Dolk Jan 21 '17 at 22:33
  • I tried different calculations like weight * height just to make sure its not the fault of me applying wrong equation. – helloumarian Jan 21 '17 at 22:34
  • @helloumarian you have a `int BMI` field declared in the `Source` class but you don't value it with the call of the BMICalc() method. So `BMI` has always its default value for an int : `0`. Before this instruction `item3.setText(String.valueOf(BMI));` do that : `BMI = BMICalc();` it should solve your problem. – davidxxx Jan 21 '17 at 22:48
0

You missed to create the JLabels 2 and 3. Try this:

public Source () {
    super("Title"); 
    setLayout(new FlowLayout()); 
    item1 = new JLabel("Text"); 
    item1.setToolTipText("This appears on hover"); 

    item2 = new JLabel("Text");

    weight = JOptionPane.showInputDialog(null, "Weight: ");
    height = JOptionPane.showInputDialog(null, "Height: ");
    BMICalc();        

    item3 = new JLabel("Text");
    item3.setText(String.valueOf(BMI));
    add(item1);
    add(item2);
    add(item3);
}
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268