简体   繁体   中英

JFrame, JTextArea, and Setting Variables

I have a program that I want to use a JFrame and 8 text areas to set the values of 8 variables. When I fill in all of the text areas, the program runs. When I leave a text area blank the program fails. I get the following error:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String

Then I get a long list of what I think are the locations the error occurred?

So I tried to use this if statement to say that if the text areas that set the values of x3,y3,x4,y4 were left blank, the program would still run.

I have parts of the program that will handle just having 2, 3, and 4 coordinate pairs respectively.

if(x3 == null && y3 == null && x4 == null && y4 == null){
r1 = new Regression(x1,y1,x2,y2,x3,y3,x4,y4);
r1.computation();
}

Under x3 == null and the others I get: The operator == is undefined for the argument type(s) double, null

Any ideas on how I can get the program to run without having all of the text areas filled?

I'm new to Java and I'm just beginning to get comfortable with it.

EDIT: Full Code Below

    package javaPackage;

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


public class Frame extends JFrame {

    private static final long serialVersionUID = 1L;
    public double x1;
    public double y1;
    public double x2;
    public double y2;
    public double x3;
    public double y3;
    public double x4;
    public double y4;


    public Regression r1;
    public Regression2 r2;

    public static void main(String[] args){
        new Frame().setVisible(true);
    }

    public Frame(){

        super("Senior Design");
        setSize(600, 600);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new FlowLayout());



        final JTextArea x1Test= new JTextArea (1,1);
        final JTextArea y1Test= new JTextArea (1,1);
        final JTextArea x2Test= new JTextArea (1,1);
        final JTextArea y2Test= new JTextArea (1,1);
        final JTextArea x3Test= new JTextArea (1,1);
        final JTextArea y3Test= new JTextArea (1,1);
        final JTextArea x4Test= new JTextArea (1,1);
        final JTextArea y4Test= new JTextArea (1,1);
        add(x1Test);
        add(y1Test);
        add(x2Test);
        add(y2Test);
        add(x3Test);
        add(y3Test);
        add(x4Test);
        add(y4Test);

        JButton button = new JButton("Compute");
        add(button);
        button.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                x1 = Double.parseDouble(x1Test.getText()); //need to convert to doubles
                y1 = Double.parseDouble(y1Test.getText());
                x2 = Double.parseDouble(x2Test.getText());
                y2 = Double.parseDouble(y2Test.getText());
                x3 = Double.parseDouble(x3Test.getText());
                y3 = Double.parseDouble(y3Test.getText());
                x4 = Double.parseDouble(x4Test.getText());
                y4 = Double.parseDouble(y4Test.getText());


                if(x3 == null && y3 == null && x4 == null && y4 == null){
                r1 = new Regression(x1,y1,x2,y2,x3,y3,x4,y4);
                r1.computation();
                }


                r2=new Regression2(x1,y1,x2,y2,x3,y3,x4,y4);//initializes the variables
                r2.computation();//starts the computation


            }
        });


    }
    }

NumberFormatException / "Any ideas on how I can get the program to run without having all of the text areas filled?"

You're probably trying to parse the text (during initialization) before any event has been fired. It's a mistake I see a lot here. Like

JTextField field = new JTextField();
double num = Double.parseDouble(field.getText());

That's will undoubtedly get you a NumberFormatException as there is not text (empty String as the error says).

You need to wait until some event is fired before you want to parse the text, not during initialization. Like for example a button press

private double num;
private JTextField field = new JTextField();
private JButton button  = new JButton("Nutton");

...
button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        num = Double.parseDouble(field.getText());
    }
});

"Under x3 == null and the others I get: The operator == is undefined for the argument type(s) double, null"

It makes sense, if x3 is a double . As I stated i my comment, primitives (int, double, etc) can't be null. They are defaulted at 0, If you want to check if they haven't been initialized, then check them against 0, not null

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