Java 我的代码没有显示错误,但不显示';t调试并在cosole中显示错误

Java 我的代码没有显示错误,但不显示';t调试并在cosole中显示错误,java,Java,下面是我进入控制台的错误 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 Div

下面是我进入控制台的错误

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 Div_mod2.<init>(Div_mod2.java:67)
at Div_mod2$1.run(Div_mod2.java:34)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
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)
我不确定到底是什么问题,我真的需要帮助解决这个问题 我一次又一次地检查了所有的东西,还有第二双眼睛,他们不确定我的问题


提前感谢任何能帮助我的人,非常感谢Stacktrace信息足以解决此问题。您的提示如下:

在Div_mod2.(Div_mod2.java:67)

在初始化Div_mod2的某个点(第67行)时,您似乎试图分配一个
字符串
值,以转换为一个数字
整数
。由于这似乎是空的(同样是stacktrace的错误消息):

java.lang.NumberFormatException:对于输入字符串:“”

这个过程失败了。代码没有显示任何错误的原因是
NumberFormatException
是一个运行时异常(因此不需要try-catch块)

我想提出一些建议:

1) 尝试找出无效
字符串
值的来源

2) 了解什么是运行时异常


3) 阅读如何从stacktrace中提取有用信息。

这是因为有此说明。Integer.parseInt(textField.getText());在创建和显示面板之前,您正在内部调用它。初始值为空(“”)。您应该将此指令移动到某些操作侦听器方法。@AlexAce很乐意提供帮助!
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.SystemColor;
import java.awt.TextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Div_mod2 extends JFrame {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;

/**
 * Launch the application.
 */
public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Div_mod2 frame = new Div_mod2();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}


/**
 * Create the frame.
 */
public Div_mod2() {

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 700, 477);
    contentPane = new JPanel();
    contentPane.setBackground(SystemColor.activeCaption);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setVisible(true);
    contentPane.setLayout(null);



    textField = new JTextField();
    textField.setBackground(SystemColor.controlHighlight);
    textField.setFont(new Font("Arial Black", Font.BOLD, 25));
    textField.setBounds(394, 41, 171, 66);
    contentPane.add(textField);
    textField.setColumns(10);
    int num1 = 0, num2 = 0;
    num1 = Integer.parseInt(textField.getText());
    int division1;
    int division2;
    int mod1,mod2;
    division1 = num1 / num2; 
    division2 = num2 / num1; 
    mod1 = num1 % num2;
    mod2 = num2 % num1;

    JLabel lblNewLabel = new JLabel("Inscrire le premier entier :");
    lblNewLabel.setFont(new Font("Arial Black", Font.BOLD, 18));
    lblNewLabel.setBounds(25, 38, 325, 66);
    contentPane.add(lblNewLabel);

    JLabel lblInscrireLeDeuxime = new JLabel("Inscrire le deuxi\u00E8me entier : ");
    lblInscrireLeDeuxime.setFont(new Font("Arial Black", Font.BOLD, 18));
    lblInscrireLeDeuxime.setBounds(25, 147, 325, 66);
    contentPane.add(lblInscrireLeDeuxime);

    textField_1 = new JTextField();
    textField_1.setBackground(SystemColor.controlHighlight);
    textField_1.setFont(new Font("Arial Black", Font.BOLD, 25));
    textField_1.setColumns(10);
    textField_1.setBounds(383, 144, 171, 66);
    contentPane.add(textField_1);
    num2 = Integer.parseInt(textField.getText());



    JLabel lblNewLabel_1 = new JLabel("a");
    lblNewLabel_1.setVisible(false);
    lblNewLabel_1.setFont(new Font("Arial Black", Font.BOLD, 18));
    lblNewLabel_1.setBounds(25, 240, 143, 39);
    contentPane.add(lblNewLabel_1);
    lblNewLabel_1.setText(String.valueOf(num1+" / "+num2+" = "+division1));

    JLabel label_3 = new JLabel("a");
    label_3.setVisible(false);
    label_3.setFont(new Font("Arial Black", Font.BOLD, 18));
    label_3.setBounds(25, 288, 143, 39);
    contentPane.add(label_3);
    label_3.setText(String.valueOf(num1+" / "+num2+" = "+division2));

    JLabel label = new JLabel("a");
    label.setVisible(false);
    label.setFont(new Font("Arial Black", Font.BOLD, 18));
    label.setBounds(25, 388, 143, 39);
    contentPane.add(label);
    label.setText(String.valueOf(num1+" / "+num2+" = "+mod1));

    JLabel label_1 = new JLabel("a");
    label_1.setVisible(false);
    label_1.setFont(new Font("Arial Black", Font.BOLD, 18));
    label_1.setBounds(25, 338, 143, 39);
    contentPane.add(label_1);
    label_1.setText(String.valueOf(num1+" / "+num2+" = "+mod2));

    JButton btnNewButton = new JButton("Division & Modulo");
    btnNewButton.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            lblNewLabel_1.setVisible(true);
            label_1.setVisible(true);
            label_3.setVisible(true);
            label.setVisible(true);

            }
    });
    btnNewButton.setForeground(SystemColor.windowBorder);
    btnNewButton.setBackground(SystemColor.activeCaptionBorder);
    btnNewButton.setFont(new Font("Arial Black", Font.BOLD, 18));
    btnNewButton.setBounds(394, 288, 243, 77);
    contentPane.add(btnNewButton);

}
}