线程“main”中的异常 java.lang.NumberFormatException: null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19850436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:41:19  来源:igfitidea点击:

Exception in thread "main" java.lang.NumberFormatException: null

javaexceptionnull

提问by RequiemDream

I have tried everything I possibly could with this, but I still could not find any solution to this, and here is the error, I tried to change the null in line 16, etc but to no avail, and I'm a new Java programmer so I do not have much experience using this language, all help appreciated!

我已经尝试了所有可能的方法,但我仍然找不到任何解决方案,这是错误,我尝试更改第 16 行中的 null 等但无济于事,我是一个新的 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 DebugEight4.main(DebugEight4.java:11)

线程“main”中的异常 java.lang.NumberFormatException: null at java.lang.Integer.parseInt(Integer.java:454) at java.lang.Integer.parseInt(Integer.java:527) at DebugEight4.main(DebugEight4.爪哇:11)

import javax.swing.*;
public class DebugEight4
{
 public static void main(String[] args) 
{
  int x = 0, y;
  String array[] = new String[100];
  String entry;
  int i = Integer.parseInt(array[1]);
  final String STOP = "XXX";
  StringBuffer message = new
      StringBuffer("The words in reverse order are\n");

  entry = JOptionPane.showInputDialog(null,
    "Enter any word\n" +
    "Enter " + STOP + " when you want to stop"); 
  while(!(entry.equals(STOP)))
  {
     array[x] = entry;
     x++;
     entry = JOptionPane.showInputDialog(null,
        "Enter another word\n" +
        "Enter " + x + " when you want to stop"); 
  }
  for(y = 0; y > 0; ++y)
  {
     message.append(array[y]);
     message.append("\n");
  }
  JOptionPane.showMessageDialog(null, message);
 }
}

回答by MouseLearnJava

The error is caused by the following code:

该错误是由以下代码引起的:

 int i = Integer.parseInt(array[1]);

In your code, you just declare the String array as below. But all of the element is null in array.

在您的代码中,您只需如下声明 String 数组。但是array 中的所有元素都是空的。

 String array[] = new String[100];

Please initizlize them first.

请先初始化它们。

Actually, variable i is not used in the context, so just remove the code "int i = Integer.parseInt(array[1]);".

实际上,变量 i 没有在上下文中使用,因此只需删除代码“int i = Integer.parseInt(array[1]);”。

And I do not think the following code for for-loop is correct. The only way to stop it is that the index exceed the range of array.

而且我认为 for-loop 的以下代码不正确。阻止它的唯一方法是索引超出数组的范围。

for(y = 0; y > 0; ++y)
{
 message.append(array[y]);
 message.append("\n");
}

回答by Preet Sangha

There is nothing in the array except empty strings.

除了空字符串外,数组中没有任何内容。

  String array[] = new String[100];
  //...
  int i = Integer.parseInt(array[1]);

an empty string cannot be parsed into an Integer.

空字符串不能解析为整数。

For example:

例如:

  String array[] = new String[100];     <<-- Create an array of empty string
  array[1] = "1";                       <<-- set second item in array to a parseable value
  int i = Integer.parseInt(array[1]);   <<-- Parse the value out