所以这是我在控制台中的错误
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)
这是我实际的程序代码。
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);
}
}
我不确定是什么问题,我真的需要帮助解决这个问题 我经历了一遍又一遍,看到它的第二双眼睛,以及他们不确定我的问题
提前感谢任何可以帮助我的人,非常感谢
答案 0 :(得分:1)
Stacktrace信息应足以解决此问题。你的提示是这一行:
String
在某一时刻(第67行)初始化Div_mod2期间,您似乎尝试分配Integer
值以转换为数字java.lang.NumberFormatException: For input string: ""
。因为这似乎是空的(再次通过堆栈跟踪的错误消息):
NumberFormatException
该过程失败。您的代码未显示任何错误的原因是String
https://docs.oracle.com/javase/7/docs/api/java/lang/NumberFormatException.html是RuntimeException(因此不需要try-catch块)。
我会建议一些事情:
1)尝试找出无效spring.sleuth.sampler.percentage
值的来源。
2)阅读什么是RuntimeExceptions
3)阅读如何从堆栈跟踪中提取有用信息。