java.lang.NumberFormatException-Can';我找不到错误所在
我收到这个信息:java.lang.NumberFormatException-Can';我找不到错误所在,java,Java,我收到这个信息: Exception in thread "main" java.lang.NumberFormatException: null at java.lang.Integer.parseInt(Integer.java:454) at java.lang.Integer.parseInt(Integer.java:527) at fdc.programming.VendingMachine.InsertMoney(VendingMachine.java:70
Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:454)
at java.lang.Integer.parseInt(Integer.java:527)
at fdc.programming.VendingMachine.InsertMoney(VendingMachine.java:70)
at fdc.programming.VendingMachineDriver.main(VendingMachineDriver.java:30)
Java Result: 1
我一直在努力想,如何做一个验证循环,这样只有正整数才能被接受,现在我放弃了,但我没有改变任何东西,把一切都恢复原状。现在,当我尝试输入一个数字时,它会给出上面的错误,但是Netbeans中没有错误,我可以用它来找出错误!请注意,我只为大学用Java完成了一个基本模块;)
我的代码是:
public class VendingMachine
{
String sinsertMoney, sinsertMoney2; // Money inserted value for parsing into int
String productName; // Name of product
int insertMoney, insertMoney2; // Money inserted by customer (int = pence)
int price; // Price of products on sale
int changeLeft; // Change left from inserted money after selection
int again; // variable for deciding program repeat
DecimalFormat pence = new DecimalFormat("#p"); // Format display output for pence
public void InsertMoney() {
String soption; // Variable for machine operation
productName = " Nothing";
insertMoney = 0; // Default inserted money initialised to zero
insertMoney2 = 0; // Default additional inserted money initialised to zero
price = 0; // Initialising money variables
// Vending machine welcome dialog
soption = JOptionPane.showInputDialog(
"============================================"
+ "\nWelcome to the College Vending Machine!"
+ "\n============================================"
+ "\n\nOptions: i for insert money, s for select item, q for quit."
+ "\n\n============================================");
if ("q".equals(soption)) { // If user chooses q: quit
JOptionPane.showMessageDialog(null, "Have a Nice Day!");
System.exit(0); // terminate application
}
if ("i".equals(soption)) { // if user chooses i: insert money
JOptionPane.showInputDialog(
"============================="
+ "\nPlease enter some money (in pence)"
+ "\n============================="); // Inserting money
insertMoney = Integer.parseInt(sinsertMoney); // Parsing for calculations
}
if ("s".equals(soption)) { // if user chooses s: select item
}
}
我看不出您在何处声明了
sinsertMoney
,但您似乎忘记了将调用结果分配给JOptionPane.showInputDialog
,因此,当您尝试解析该值时,该值仍然为空
试试这个:
sinsertMoney = JOptionPane.showInputDialog(
"============================="
+ "\nPlease enter some money (in pence)"
+ "\n============================="); // Inserting money
insertMoney = Integer.parseInt(sinsertMoney);
我看不出您在何处声明了
sinsertMoney
,但您似乎忘记了将调用结果分配给JOptionPane.showInputDialog
,因此,当您尝试解析该值时,该值仍然为空
试试这个:
sinsertMoney = JOptionPane.showInputDialog(
"============================="
+ "\nPlease enter some money (in pence)"
+ "\n============================="); // Inserting money
insertMoney = Integer.parseInt(sinsertMoney);
您需要在
sinsertMoney
中获取输入的值,如:
sinsertMoney = JOptionPane.showInputDialog(
"============================="
+ "\nPlease enter some money (in pence)"
+ "\n=============================");
并且还对
sinsertMoney
执行空检查,以执行取消操作和空字符串您需要在sinsertMoney
中获取输入的值,如:
sinsertMoney = JOptionPane.showInputDialog(
"============================="
+ "\nPlease enter some money (in pence)"
+ "\n=============================");
并且还对线程“main”java.lang.NumberFormatException:null尝试解析
null
字符串时的sinsertMoney
取消操作和空字符串执行null检查。检查sinsertMoney
是否为null
。sinsertMoney变量是什么?定义和初始化它的位置?声明和初始化sinsertMoney
的位置?线程“main”java.lang.NumberFormatException中的异常:null
您正在尝试解析null
字符串。检查sinsertMoney
是否为null
。sinsertMoney变量是什么?在哪里定义和初始化?在哪里声明和初始化sinsertMoney
?啊,一定是我睡眠不足lol我添加了变量初始化,我忘记了,但感谢您选择了错误,现在它又开始工作了…我可以回到验证的头痛:)啊,一定是我睡眠不足lol我添加了我忘记的变量初始化,但感谢您选择了错误,现在它又开始工作了…我可以回到验证的头痛:)