java.lang.numberformatException 空字符串 java awt

标签 java swing exception awt numberformatexception

    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());
    }

    }

}

单击计算按钮(按钮 calc)后,计算值不会出现在相应的标签中,并且会显示异常,提示 java.lang.NumberFormatException: Empty string。我无法找出解决方案。请帮忙。

最佳答案

行exchange_rate = Double.parseDouble(l4a.getText());给你这个异常,因为 l4a 中没有值,并且你试图将其解析为 double 值, 尝试在 catch 子句中打印异常。

关于java.lang.numberformatException 空字符串 java awt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22304421/

相关文章:

java.text.ParseException : Unparseable date exception

java - Maven 编译 : Failure executing javac

java - 如何从外部未知类为 2D 游戏引擎 API 进行绘制

程序中的 Java 运行时错误 InputMismatchException

java - 电子邮件地址中的撇号引发异常

java - 在关闭 JFrame 之前我将如何执行函数

Java Swing 内存使用

c# - 将 Messagebox 放在任何项目的任何 UserControl 中的任何位置时出现 InvalidOperationException

c# - 使用套接字获取对象处置异常

java - 如何在android中同步/限制某些异步http调用