【问题标题】:NullPointerException and Array IO [duplicate]NullPointerException 和数组 IO [重复]
【发布时间】:2017-06-01 14:38:24
【问题描述】:

好的,所以我是 java 的初学者,我正在实现一个存储数组并使用冒泡排序对其进行排序的类。

import java.io.*;
public class Array
{
    private int array[];
    private int n;
    private BufferedReader br;
    public Array() throws IOException
    {
        System.out.print("Enter the size of the array : ");
        br=(new BufferedReader(new InputStreamReader(System.in)));
        n = Integer.parseInt(br.readLine());
        System.out.println("Enter "+ n +" integers");
        for(int i=0;i<n;i++)
        {

            array[i]=Integer.parseInt(br.readLine());
        }

    }
    public void showArray()
    {
        int i;
        for (i=0; i<n;i++)
        {
            System.out.print(array[i]+"  ");
        }
    }
    public void bubbleSort()
    {
        int max,last,i,j;
        last = n;
        for(i = 0; i<n ; i++)
        {
            max=0;
            for(j=0; j<last; j++)
            {
                max = (array[max]>array[j]) ? max : j ;

            }
            j = array[last];
            array[last] = array[max];
            array[max] = j;
            last--;
        }
    }
    public static void main(String [] args) throws IOException
    {
            Array a1 = new Array();
            System.out.println("Unsorted Array : ");
            a1.showArray();
            System.out.println("Sorted Array : ");
            a1.bubbleSort();
            a1.showArray();
    }
}

所以当我运行它时它会给出错误:

shubham@shubham-Inspiron-3542:~$ java Array
Enter the size of the array : 4
Enter 4 integers
11 9 7 16 4
Exception in thread "main" java.lang.NumberFormatException: For input string: "11 9 7 16 4"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
    at Array.<init>(Array.java:16)
    at Array.main(Array.java:48)

我完全不明白是什么问题。我可以猜到在解析输入流时存在一些问题。 编辑: 当我在输入后按 Enter 时,我得到了这个:

shubham@shubham-Inspiron-3542:~$ java Array
Enter the size of the array : 5
Enter 5 integers
22
Exception in thread "main" java.lang.NullPointerException
    at Array.<init>(Array.java:16)
    at Array.main(Array.java:48)

编辑 2: 拆分输入流摆脱了 NumberFormatException 错误但 NullPointerException 错误仍然存​​在

【问题讨论】:

  • 11 9 7 16 4(注意中间的空格)不是有效数字。
  • 你必须在每个数字后按回车
  • 使用 readline().split(" ") 并将字符串值解析为整数。
  • 这并不能帮助我摆脱 NullPointerException
  • 这个问题并没有完全回答我的问题,因为我已经清楚地将类 Array 实例化为对象 a1,但我仍然收到该错误。我现在怀疑这是不是因为我在 main 函数中实例化了容器类。

标签: java arrays io nullpointerexception


【解决方案1】:

您正在阅读的这一行:"11 9 7 16 4"

不是整数,而是由空格分隔的整数序列。 您需要将它们拆分,然后将每个人解析(转换)为一个整数。

你应该这样做:

String[] stringArray = br.readLine().split(" ");

然后对该数组中的每个元素执行Integer.parseInt(...);

【讨论】:

  • 之后我得到:shubham@shubham-Inspiron-3542:~$ java Array 输入数组的大小:5 输入 5 个整数 44 23 62 17 19 线程“main”java 中的异常。 lang.NullPointerException at Array.(Array.java:17) at Array.main(Array.java:49)
  • 当然你会......数组对象为空......
  • 看不懂,数组对象怎么为空?
  • 我确实在 main() 函数中将 Array 实例化为 a1
  • 好吧,这很尴尬,我已经意识到我的错误了。对不起。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-21
  • 1970-01-01
  • 1970-01-01
  • 2012-07-17
  • 1970-01-01
相关资源
最近更新 更多