Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java NullPointerException和数组IO_Java_Arrays_Io_Nullpointerexception - Fatal编程技术网

Java NullPointerException和数组IO

Java NullPointerException和数组IO,java,arrays,io,nullpointerexception,Java,Arrays,Io,Nullpointerexception,好的,我是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

好的,我是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();
    }
}
import java.io.*;
公共类数组
{
私有整数数组[];
私人int n;
专用缓冲读取程序br;
公共数组()引发IOException
{
System.out.print(“输入数组的大小:”);
br=(新的BufferedReader(新的InputStreamReader(System.in));
n=Integer.parseInt(br.readLine());
System.out.println(“输入”+n+“整数”);

对于(int i=0;i您正在阅读的这一行:“11 9 7 16 4”

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

你应该这样做:

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

然后对该数组中的everyy元素执行
整数.parseInt(…);

11 9 7 16 4
(注意中间的空格)不是有效数字。在每个numebruse readline()之后必须按enter键。拆分(“”)并将字符串值解析为整数。这并不能帮助我摆脱NullPointerException这个问题并没有完全回答我的问题,因为我已经清楚地将类数组实例化为对象a1,但我仍然得到了这个错误。我现在的疑问是,这是否是因为我在主函数中实例化了容器类n、 完成后,我得到:shubham@shubham-Inspiron-3542:~$java数组输入数组的大小:5输入5个整数44 23 62 17 19线程“main”中的异常java.lang.NullPointerException at Array。(Array.java:17)at Array.main(Array.java:49)当然你们会的…数组对象是空的…我不明白,数组对象怎么是空的?我确实在main()函数中将数组实例化为a1好吧,这很尴尬,我意识到了我的错误。对不起。
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)
String[] stringArray = br.readLine().split(" ");