简体   繁体   中英

Why is setText of JTextField not working in the actionPerformed method?

im making a currency calculator and i have an issue with not being able to set the result of the calculation into the result JTextField, which is the object otherTextField.

Here is my class:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ValutaKalkulator implements ActionListener {
    private double nokValue;
    private double otherValue;
    private JButton buttonRemoveNok;
    private JButton removeOther;
    private JButton removeBoth;
    private JButton exitButton;
    private JButton usdButton;
    private JButton sekButton;
    private JButton gbpButton;
    private JButton eurButton;
    private JLabel nokLabel;
    private JTextField nokTextField;
    private JLabel otherLabel;
    private JTextField otherTextField;

    public ValutaKalkulator() 
    {
        JFrame frame = new JFrame();
        frame.setTitle("VALUTAKALKULATOR");
        buttonRemoveNok = new JButton("Fjern NOK");
        removeOther = new JButton("Fjern annen valuta");
        removeBoth = new JButton("Fjern begge");
        exitButton = new JButton("Avslutt");
        usdButton = new JButton("USD");
        sekButton = new JButton("SEK");
        gbpButton = new JButton("GBP");
        eurButton = new JButton("EUR");
        nokLabel = new JLabel("NOK");
        JTextField nokTextField = new JTextField();
        otherLabel = new JLabel("Annen valuta");
        otherTextField = new JTextField();

        buttonRemoveNok.addActionListener(this);
        removeOther.addActionListener(this);
        removeBoth.addActionListener(this);
        exitButton.addActionListener(this);
        usdButton.addActionListener(this);
        sekButton.addActionListener(this);
        gbpButton.addActionListener(this);
        eurButton.addActionListener(this);

        JPanel pnlSouth = new JPanel(new GridLayout(1, 4));
        JPanel pnlCenter = new JPanel(new GridLayout(2, 2));
        JPanel pnlNorth = new JPanel(new GridLayout(1, 4));

        pnlNorth.add(nokLabel);
        pnlNorth.add(nokTextField);
        pnlNorth.add(otherLabel);
        pnlNorth.add(otherTextField);

        pnlCenter.add(gbpButton);
        pnlCenter.add(eurButton);
        pnlCenter.add(usdButton);
        pnlCenter.add(sekButton);

        pnlSouth.add(buttonRemoveNok);
        pnlSouth.add(removeOther);
        pnlSouth.add(removeBoth);
        pnlSouth.add(exitButton);

        frame.add(pnlNorth, BorderLayout.NORTH);
        frame.add(pnlSouth, BorderLayout.SOUTH);
        frame.add(pnlCenter, BorderLayout.CENTER);

        frame.setVisible(true);
        frame.pack();

    }

    public String calculateFromNok(String nokValueString, String toValue)
    {
        double result = 0;
        double nokValue = Double.valueOf(nokValueString);
        switch(toValue)
        {
            case "GBP":
            result = nokValue * 10.8980 / 100;
            break;

            case "EUR":
            result = nokValue * 9.2450 / 100;
            break;

            case "USD":
            result = nokValue * 8.5223 / 100;
            break;

            case "SEK":
            result = nokValue * 96.48 / 100;
            break;
        }
        String resultString = Double.toString(result);
        return resultString;
    }

    public void actionPerformed(ActionEvent event)
    {
        String text = event.getActionCommand();

        switch(text)
        {
            case "USD":
            String txt = nokTextField.getText();
            String txt2 = calculateFromNok(txt, "USD");
            otherTextField.setText(txt2);
            break;

        }
    }
}

I know the currencies are not correct and there are other logical flaws and bad variable names, but my question for now is why can i not put the result from the method calculateFromNok into my JTextField otherTextField?

Help appreciated thx!

You are creating an initialize local variable JTextField nokTextField .
So private JTextField nokTextField; is not initializing. That's why it's giving you nullpointerexception.

Just Change At Line Number 37:

JTextField nokTextField = new JTextField();

To

nokTextField = new JTextField();

Your issue is with the line:

JTextField nokTextField = new JTextField();

Change it to:

nokTextField = new JTextField();

The reason this happens is that you previously defined nokTextField at the top, and then you initialized a separate variable with the same name in the constructor. Using an IDE such as Eclipse makes these types of errors very easy to catch. I suggest you use one.

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