java.lang.numberformatexception空字符串java awt

java.lang.numberformatexception空字符串java awt,java,swing,exception-handling,awt,numberformatexception,Java,Swing,Exception Handling,Awt,Numberformatexception,单击“计算”按钮(button calc)后,计算值不会出现在相应的标签中,并且会显示一个异常,显示为java.lang.NumberFormatException:Empty string。我想不出解决办法。请提供帮助。在函数开始时说它发生在这里可能是相对安全的,不过您应该为我们提供实际的异常情况和行号 public class CustomCalculator extends Frame implements ActionListener{ Panel jp1 = new Pane

单击“计算”按钮(button calc)后,计算值不会出现在相应的标签中,并且会显示一个异常,显示为
java.lang.NumberFormatException:Empty string
。我想不出解决办法。请提供帮助。

在函数开始时说它发生在这里可能是相对安全的,不过您应该为我们提供实际的异常情况和行号

    public class CustomCalculator extends Frame implements ActionListener{

Panel jp1 = new Panel();
Panel jp2 = new Panel();
Panel jp3 = new Panel();
Panel jp4 = new Panel();
Panel jp5 = new Panel();
Panel center_merge = new Panel();


Label l2 = new Label("Quantity : ");
TextField l2a = new TextField(20);

Label l3 = new Label("Invoice Value : ");
TextField l3a = new TextField(20);

Label l4 = new Label("Exchange Rate : ");
TextField l4a = new TextField(20);

Label l5 = new Label("Costing(A) : ");
TextField l5a = new TextField();

Label l6 = new Label("(A + 1%)(B) : ");
Label l6a = new Label();

Label l7 = new Label("BCD (C) : ");
Label l7a = new Label("");

Label l8 = new Label("CVD (D) : ");
Label l8a = new Label("");

Label l9 = new Label("Custom Education Cess (E) : ");
Label l9a = new Label("");

Label l10 = new Label("Custom Sec & Higher Edu.Cess (F) : ");
Label l10a = new Label("");

Label l11 = new Label("Additional Duty Imports (G) : ");
Label l11a = new Label("");

Label l12 = new Label("Total (H) : ");
Label l12a = new Label("");

Label l13 = new Label("Costing+Total (I) : ");
Label l13a = new Label("");

Label l14 = new Label("(H/Quantity) (J) : ");
Label l14a = new Label("");

Label l15 = new Label("4% SAD (G/Quantity) (K) : ");
Label l15a = new Label("");

Label l16 = new Label("Net Costing (L) : ");
Label l16a = new Label("");

Label l17 = new Label("Transportation (M) : ");
TextField l17a = new TextField(5);

Label l18 = new Label("Godown Rate (N) : ");
TextField l18a = new TextField(5);

Label l19 = new Label("Brokerage (O) : ");
TextField l19a = new TextField(5);

Label l20 = new Label("Actual Costing (P) : ");
Label l20a = new Label("");

Label l21 = new Label("Small Gatepass (Q) : ");
Label l21a = new Label("");

Label l22 = new Label("Big Gatepass (R) : ");
Label l22a = new Label("");

Button l2b = new Button("reset");
Button l3b = new Button("reset");
Button l4b = new Button("reset");


Button master_reset = new Button("reset all");
Button calc = new Button("Calculate");



public CustomCalculator()
{
    super("Custom Calculator");
    this.setSize(800,700);

    jp1.setLayout(new FlowLayout());
    //jp1.setBorder(BorderFactory.createLineBorder(Color.GRAY));

    jp1.add(l2);
    jp1.add(l2a);
    jp1.add(l2b);

    jp1.add(l3);
    jp1.add(l3a);
    jp1.add(l3b);

    jp1.add(l4);
    jp1.add(l4a);
    jp1.add(l4b);

    jp2.setLayout(new GridLayout(6,2));
    //jp2.setBorder(BorderFactory.createLineBorder(Color.GRAY));

    jp2.add(l5);
    jp2.add(l5a);

    jp2.add(l6);
    jp2.add(l6a);

    jp2.add(l7);
    jp2.add(l7a);

    jp2.add(l8);
    jp2.add(l8a);

    jp2.add(l9);
    jp2.add(l9a);

    jp2.add(l10);
    jp2.add(l10a);

    jp3.setLayout(new GridLayout(6,2));
    //jp3.setBorder(BorderFactory.createLineBorder(Color.GRAY));

    jp3.add(l11);
    jp3.add(l11a);

    jp3.add(l12);
    jp3.add(l12a);

    jp3.add(l13);
    jp3.add(l13a);

    jp3.add(l14);
    jp3.add(l14a);

    jp3.add(l15);
    jp3.add(l15a);

    jp3.add(l16);
    jp3.add(l16a);

    jp4.setLayout(new GridLayout(6,2));
    //jp4.setBorder(BorderFactory.createLineBorder(Color.GRAY));

    jp4.add(l17);
    jp4.add(l17a);

    jp4.add(l18);
    jp4.add(l18a);

    jp4.add(l19);
    jp4.add(l19a);

    jp4.add(l20);
    jp4.add(l20a);

    jp4.add(l21);
    jp4.add(l21a);

    jp4.add(l22);
    jp4.add(l22a);

    center_merge.setLayout(new GridLayout(1,3));
    //center_merge.setBorder(BorderFactory.createLineBorder(Color.GRAY));
    center_merge.add(jp2);
    center_merge.add(jp3);
    center_merge.add(jp4);

    jp5.setLayout(new FlowLayout());
    //jp5.setBorder(BorderFactory.createLineBorder(Color.GRAY));
    jp5.add(calc);
    jp5.add(master_reset);

    this.setLayout(new BorderLayout());

    this.add(jp1,BorderLayout.NORTH);
    this.add(center_merge,BorderLayout.CENTER);
    this.add(jp5,BorderLayout.SOUTH);

    this.addWindowListener(new WindowAdapter(){   
        public void windowClosing(WindowEvent we)
        {
            System.exit(0);
        }
    });

    l2b.addActionListener(this);
    l3b.addActionListener(this);
    l4b.addActionListener(this);
    calc.addActionListener(this);
    master_reset.addActionListener(this);

    this.setVisible(true);

}


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

}


@Override
public void actionPerformed(ActionEvent ae) {
    double quantity = 0;
    double invoice_value = 0;
    double exchange_rate = 0;
    double A=0;
    double B=0;
    double C=0;
    double D=0;
    double E=0;
    double F=0;
    double G=0;
    double H=0;
    double I=0;
    double J=0;
    double K=0;
    double L=0;
    double M = 0;
    double N = 0;
    double O=0;
    double P=0;
    double Q=0;
    double R=0;

try
{
    quantity = Double.parseDouble(l2a.getText());
    invoice_value = Double.parseDouble(l3a.getText());
    exchange_rate = Double.parseDouble(l4a.getText());
    M = Double.parseDouble(l17a.getText());
    N = Double.parseDouble(l18a.getText());
    O = Double.parseDouble(l19a.getText());

    A = invoice_value*exchange_rate;
    B = A+(0.01*A);
    C = 0.075*B;
    D = 0.12*(B+C);
    E = 0.02*(C+D);
    F = 0.01*(C+D);
    G = 0.04*(B+C+D+E+F);
    H = C+D+E+F+G;
    I = A+H;
    J = H/quantity;
    K = G/quantity;
    L = J-K;
    P = L+M+N+O;
    Q = (0.12*B)/quantity;
    R = Q+K;



    if(ae.getActionCommand().equals("calc"))
      {

        l5a.setText(String.valueOf(A));
        l6a.setText(String.valueOf(B));
        l7a.setText(String.valueOf(C));
        l8a.setText(String.valueOf(D));
        l9a.setText(String.valueOf(E));
        l10a.setText(String.valueOf(F)); 
        l11a.setText(String.valueOf(G));
        l12a.setText(String.valueOf(H));
        l13a.setText(String.valueOf(I));
        l14a.setText(String.valueOf(J));
        l15a.setText(String.valueOf(K));
        l16a.setText(String.valueOf(L));
        l20a.setText(String.valueOf(P));
        l21a.setText(String.valueOf(Q));
        l22a.setText(String.valueOf(R));        

      }
    else if(ae.getActionCommand().equals("master_reset"))
      {
            l5a.setText("");
            l2a.setText("");
            l3a.setText("");
            l4a.setText("");
    }
    }
    catch (Exception ex)
    {
        l5a.setText(ex.toString());
        // l3a.setText(ex.toString());
    }

    }

}

确保每个字段中都有数字文本。我建议在执行此操作之前,放置一个日志行或一个警报行来说明它们的值。更好的是,您可以捕获调试器中的第一行,并查看调用getText()的所有项的值。我打赌其中一个有一个空字符串。

行交换率=Double.parseDouble(l4a.getText());给出此异常,因为l4a中没有值,您正试图将其解析为双精度值,
尝试打印catch子句中的异常。

stacktrace应至少将您指向正确的行。我猜它几乎是在所有的Double.parseDouble(..)的某个地方。为什么AWT而不是Swing?关于放弃使用AWT组件的许多好理由,请参见我的答案。当我打印NumberFormatException时,它会打印在所有标签中,实际打印计算值的位置。仍然无法解决这个问题。这些价值观似乎没有被接受或是其他什么。请原谅我的天真。我是应用程序构建新手。这一定是因为您试图转换为double的值之一不是有效数字(可能为空),我尝试输入除l4a字段外的所有框,因为我看不到该字段的输入字段。尝试对所有要转换为double system.out.println的文本字段执行system.out.println(“l2a.getText”+l2a.getText());quantity=Double.parseDouble(l2a.getText());并查看它是否有值,对所有其他文本字段执行相同的转换。
quantity = Double.parseDouble(l2a.getText());
invoice_value = Double.parseDouble(l3a.getText());
exchange_rate = Double.parseDouble(l4a.getText());
M = Double.parseDouble(l17a.getText());
N = Double.parseDouble(l18a.getText());
O = Double.parseDouble(l19a.getText());