有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JAVA循环的lang.NullPointerException数组

我遇到了一个“java.lang.NullPointerException”问题。 这段代码的目标是用一个特定大小的数组生成随机数(在本例中,10个数字,如驱动程序中所示,但并不重要),计算平均值,找到最大值、最小值和特定数(如驱动程序中所示,2和3,但不重要)。我想我可以通过一些研究来编写所有这些代码,但在完成所有工作并编译之后,我尝试运行这些代码,出乎意料的是,我发现了java。lang.NullPointerException。我不知道这是什么,但通过一些研究,我了解到了(我想)问题所在。我想使用“for循环”访问数组,但我不能,因为数组的值为null。我不知道为什么,也不知道这段代码中的所有错误,因为我仍在尝试运行它。我只能编译,但正如我所说,我在运行时遇到了NPE问题。然而,对于我来说,构造函数以我编写的方式接收参数和“for循环”在逻辑上是可以接受的。 因此,当我运行时,信息就是:

java.lang.NullPointerException at ArrayLab.initialize(ArrayLab.java:19) at Driver.main(Driver.java:16)

我的问题是: 我的代码怎么了?为什么我的数组是空的,并且它没有在驱动程序中分配值?如何为该数组赋值?我的构造函数有什么问题吗?那么“for循环”呢?我能做些什么来修复它

public class ArrayLab
{
    private int[] ArrayNum;

    public ArrayLab(int Num)
    {        
        int[] ArrayNum = new int[Num];        
    }

    public void initialize()
    {        
        for (int ANUM = 0; ANUM <= ArrayNum.length; ANUM++)
        {               
            ArrayNum[ANUM] = (int) Math.round((Math.random() * 10));          
        }        
    }    

    public void print()
    {
        System.out.println(ArrayNum);
    }

    public void printStats()
    {
        double Sum = 0;
        int Max = 0;
        int Min = 0;

        for (int ANUM = 0; ANUM < ArrayNum.length; ANUM++)
        {            
            Sum = Sum + ArrayNum[ANUM];     

            if (ArrayNum[ANUM] > ArrayNum[Max])
            {
                Max = ANUM;
            }            

            if (ArrayNum[ANUM] < ArrayNum[Min])
            {
                Min = ANUM;
            }
        }

        double Average = Sum/ArrayNum.length;
        System.out.println("Average Value: " + Average);
        System.out.println("Maximum Value: " + Max);
        System.out.println("Minimum Value: " + Min);
    }       

    public void search(int GivenNumber)    
    {
        for (int ANUM = 0; ANUM < ArrayNum.length; ANUM++)
        {            
            if(GivenNumber == ArrayNum[ANUM])
            {
                System.out.println(GivenNumber + " was found.");
            }

            else
            {
                System.out.println(GivenNumber + " was not found.");
            }
        }
    }
}

public class Driver
    {
        public static void main(String[] args)
        {
            //create new instance of the ArrayLab class with parameter of 10
            ArrayLab array = new ArrayLab(10);

            //print out the array created by ArrayLab constructor
            array.print();

            //initialize the array with random values
            array.initialize();

            //print the randomized array
            array.print();

            //print stats for the array
            array.printStats();

            //search for 3
            array.search(3);

            //search for 2
            array.search(2);
        }
    }

共 (1) 个答案

  1. # 1 楼答案

    在构造函数中,您已经重新声明了数组。因此,实际阵列没有分配任何内存

    private int[] ArrayNum;
    
    public ArrayLab(int Num)
    {        
        ArrayNum = new int[Num];        
    }