简体   繁体   中英

I'm trying to create a BMI-calculator with Java using GUI, but I recieve an syntaxerror

I'm trying to program an BMI calculator, but for some reason I can't get it to work. I'm quite a noob at Java so please be considerate :). Names of classes and variables are in Swedish, but aren't that hard to understand. eg langd is length and vikt is weight.

This is my code:

package prog2;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;

import javax.swing.*;

public class BMI extends JFrame{
JTextField langdfalt;
JTextField viktfalt;
DecimalFormat fmt = new DecimalFormat ("0.00");
JLabel resultat;
BMI(){
    super("BMI Kalkylator 1.0");
    setLayout(new FlowLayout());
    JLabel langd = new JLabel ("Längd (m):");
    add(langd);
    langdfalt = new JTextField(4);
    add(langdfalt);
    JLabel vikt = new JLabel ("Vikt (kg):");
    add(vikt);
    viktfalt = new JTextField(4);
    add(viktfalt);
    JButton berakna = new JButton ("Beräkna");
    berakna.addActionListener(new BeraknaLyss());
    add(berakna);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(330, 100);
    setVisible(true);
    setLocation(820, 400);
    JLabel bmiVisning = new JLabel("Ditt BMI-värde är");
    add (bmiVisning);
    JLabel resultat = new JLabel ("");
    add (resultat);

}

class BeraknaLyss implements ActionListener{    
    public void actionPerformed(ActionEvent ave){
        double vikt = Double.parseDouble(viktfalt.getText());
        double langd = Double.parseDouble(langdfalt.getText());
        int bmi = (int)beraknaBMI(vikt, langd);
        **resultat.setText(String.valueOf(bmi));**
}   
}
public double beraknaBMI(double vikt, double langd){
    return vikt / (langd * langd);
}


public static void main(String[] args) {
    new BMI();

}

}

And the error I'm getting is NullPointerException at row 43 which I bolded.

Here is the all the error code:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at prog2.BMI$BeraknaLyss.actionPerformed(BMI.java:43)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

You're shadowing the variable resultat . Replace

JLabel resultat = new JLabel("");

with

resultat = new JLabel();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM