繁体   English   中英

将JTextField解析为整数

[英]Parsing JTextField to Integer

我不知道代码有什么问题。 你们能帮我吗?

int a = Integer.parseInt(aTextField.getText());
int b = Integer.parseInt(bTextField.getText());
int c = Integer.parseInt(cTextField.getText());
int discriminant = (int)Math.pow((double)b, 2) - 4*a*c;
int x1 = (int)(-b + (double)Math.sqrt(discriminant) / 2*a);
int x2 = (int)(-b - (double)Math.sqrt(discriminant) / 2*a);

public DiscriminantSoft(){
    super("Quadratic Equation");
    setLayout(new FlowLayout());

    headerLabel = new JLabel("ax^2 + bx + c = 0");
    headerLabel.setToolTipText("You have to write a, b and c in order to calculate discriminant.");
    add(headerLabel);

    aTextField = new JTextField(3);
    add(aTextField);
    bTextField = new JTextField(3);
    add(bTextField);
    cTextField = new JTextField(3);
    add(cTextField);

    thehandler handler = new thehandler();
    aTextField.addActionListener(handler);
    bTextField.addActionListener(handler);
    cTextField.addActionListener(handler);

}

private class thehandler implements ActionListener{

    public void actionPerformed(ActionEvent event){
        String string = "";

        if(discriminant == 0){
                string=String.format("Discriminant: " +discriminant +" x: " +x1, event.getActionCommand());
        }
        else if(discriminant > 0){
            string=String.format("Discriminant: " +discriminant +" x: " +x1 +" x2: " +x2, event.getActionCommand());
        }
        else if(discriminant < 0){
            string=String.format("Discriminant is under zero! No x found", event.getActionCommand());
        }

        JOptionPane.showMessageDialog(null, string);
    }

}

trim.main(tuna.java:5)处DiscriminantSoft。(DiscriminantSoft.java:17)处的线程“ main”中的java.lang.NullPointerException异常。

int a = Integer.parseInt(aTextField.getText()); 是第17行

您试图在类上下文中获取文本字段的文本:未初始化文本字段时。

int a = Integer.parseInt(aTextField.getText()); 
                           // ^  text fields haven't yet initialized
int b = Integer.parseInt(bTextField.getText());
int c = Integer.parseInt(cTextField.getText());
int discriminant = (int)Math.pow((double)b, 2) - 4*a*c;
int x1 = (int)(-b + (double)Math.sqrt(discriminant) / 2*a);
int x2 = (int)(-b - (double)Math.sqrt(discriminant) / 2*a);

public DiscriminantSoft(){
///....... your code

   aTextField = new JTextField(3); // <---- you are initializing them here
   add(aTextField);
   bTextField = new JTextField(3);
   add(bTextField);
   cTextField = new JTextField(3);
    add(cTextField);
}

每当访问一个类时,在类上下文中声明的字段都会首先被初始化。 如果未使用new给出初始化语句,则将它们初始化为默认值:默认情况下,对象初始化为null

请阅读Java语言规范第12章。

在类上下文中读取文本字段的文本内容实际上没有任何意义,因此请尝试在操作事件或至少在初始化它们之后读取文本。 最好将DocumentListenerJTextComponentDocumentListener一起使用,以在文本内容更改时生成事件。

查看官方教程,了解如何使用DocumentListener

尝试使用toString()并请确保文本字段输入不为null

int a = Integer.parseInt(aTextField.getText().toString());

我已经在评论中发布了这样的内容,但我想我会再次发布它,以防OP希望将此线程标记为已回答。 抱歉,这似乎有点自恋。

这段代码引发了NPE,因为您正在尚未初始化的组件上调用getText()。 在尝试调用对象上的任何方法之前,需要确保已初始化对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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