java.lang.numberformatexception空字符串java awt

时间:2014-03-10 15:32:43

标签: java swing exception-handling 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());
    }

    }

}

单击“计算”按钮(按钮计算)后,计算出的值不会出现在相应的标签中,并显示一个例外java.lang.NumberFormatException: Empty string。我无法弄清楚解决方案。请帮忙。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

虽然你应该为我们提供实际的例外情况和行号,但这可能是一个相对安全的赌注,它说它发生在你的函数的开头。

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

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