线程“AWT-EventQueue-0”中的异常java.lang.NumberFormatException:对于输入字符串“”

时间:2014-09-25 17:09:00

标签: java swing awt

我很难玩余弦函数。就整数数据类型函数而言,其余代码工作正常,但我不能理解为什么编译器使用双数据类型函数给出错误。

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/


import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
*
* @author amjad
*/

public class CalculatorVisual extends JFrame implements ActionListener {

//Methods will be used from this class
CalculatorProps cp = new CalculatorProps();

JTextField val1, val2;
JButton btnPlus, btnMinus, btnMultiply, btnDiv, btnSine, btnCosine, btnTan, 
        btnPow, btnSqrt, btnModulo, btnLog;

public CalculatorVisual(){

    super("Calculator - Visual");

    Container c = getContentPane();
    val1 = new JTextField(20);
    val2 = new JTextField(20);

    btnPlus = new JButton("+");
    btnMinus = new JButton("-");
    btnMultiply = new JButton("X");
    btnDiv = new JButton("/");
    btnCosine = new JButton("Cos");

    btnPlus.addActionListener(this);
    btnMinus.addActionListener(this);
    btnMultiply.addActionListener(this);
    btnDiv.addActionListener(this);
    btnCosine.addActionListener(this);

    JPanel p = new JPanel();

    p.add(val1); 
    p.add(val2);
    p.add(btnPlus);
    p.add(btnMinus);
    p.add(btnMultiply);
    p.add(btnDiv);
    p.add(btnCosine);

    c.add(p);


}

@Override
public void actionPerformed(ActionEvent e) {
   int a = Integer.parseInt(val1.getText());
   int b = Integer.parseInt(val2.getText());

   if(e.getSource()== btnPlus){
       val1.setText("" + cp.sum(a, b));
       val2.setText("");
   }

   else if(e.getSource()== btnMinus){
       val1.setText("" + cp.sub(a, b));
       val2.setText("");
   }

   else if(e.getSource() == btnMultiply){
       val1.setText("" + cp.multiply(a, b));
       val2.setText("");
   }

   else if(e.getSource() == btnDiv){
       val1.setText("" + cp.divide(a, b));
       val2.setText("");
   }

   else if(e.getSource() == btnCosine){
       double txtDouble = (double)a;
       double d_result = cp.calcCos(txtDouble);
       val1.setText(Double.toString(d_result));
       val2.setText("");
   }
}

public static void main(String[] args) {
   CalculatorVisual calc = new CalculatorVisual();
   calc.setSize(300,300);
   calc.setVisible(true);
   calc.setDefaultCloseOperation(3);
}
}

这是堆栈跟踪

 Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
at CalculatorVisual.actionPerformed(CalculatorVisual.java:68)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
at CalculatorVisual.actionPerformed(CalculatorVisual.java:68)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

这是功能'我在CalculatorVisual类中使用的类。

public class CalculatorProps {

int val1,val2, int_result;
double d_val1, d_val2, d_result;

CalculatorProps(){

}

public int sum(int val1,int val2)
{
    return int_result = val1+val2;
}

public int sub(int val1,int val2){
    return int_result = val1-val2;
}

public int multiply(int val1,int val2){
    return int_result = val1*val2;
}

public int divide(int val1,int val2){
    return int_result = val1 / val2;
}

public int remainder(int val1,int val2){
        return int_result = val1 % val2;
}

double sqrt(double d_val1){
    double d_result=Math.sqrt(d_val1);
    return d_result;
}

double pow(double d_val1,double d_val2){
    double d_result=Math.pow(d_val1,d_val2);
    return d_result;
}

int fact(int val1){
    if(val1<=1)
        return 1;
    else
        return (val1*fact(val1-1));
}

public double calcSine(double d_val1){
    double d_result= Math.sin(d_val1);
    return d_result;
}

public double calcCos(double d_val1){
    double d_result = Math.cos(d_val1);
    return d_result;
}

public double calcTan(double d_val1){
    double radians = Math.toRadians(d_val1);
    return d_result = Math.tan(radians);
}

public double calcLog(double d_val1){
    double d_result = Math.log(d_val1);
    return d_result;
}
}

1 个答案:

答案 0 :(得分:1)

那么,必须发生的是val1val2在您点击按钮时没有文字。这正是错误消息所抱怨的内容。您应该检查该案例并在actionPerformed方法的顶部处理它。