Java 如何获取数组并在main中输出我的printArray

Java 如何获取数组并在main中输出我的printArray,java,arrays,loops,arraylist,Java,Arrays,Loops,Arraylist,我的控制台主要给了我麻烦,在for循环的void中不断给我一个nullexception错误。为什么它是空的??? 这可能是因为在我的参数中myArray不会创建整数吗? 我无法在主屏幕中显示我的阵列 public class DimentionalArray { int[] createIntegers(int size_of_array) { //******* FILL IN CODE ********* // Your code w

我的控制台主要给了我麻烦,在for循环的void中不断给我一个nullexception错误。为什么它是空的??? 这可能是因为在我的参数中myArray不会创建整数吗? 我无法在主屏幕中显示我的阵列

    public class DimentionalArray {

    int[] createIntegers(int size_of_array)
    {
       //*******  FILL IN CODE *********
       // Your code will create an array of ints as large as specified in size_of_array
       // Fill the array in with the values: 0, 100, 200, 300, ....
       // Return the array that you just created
        int[] numarray = new int[size_of_array];
        int mutilply = 100;
        for(int i =0; i<size_of_array; i++)
        {
            System.out.println(numarray[i]);
        }
        return numarray;

    }
    void printArray(int[] myArray)
    {
        //*******  FILL IN CODE *********
        // Print out your array with one number per line.  Get the size of the
        // array from the "myArray" parameter (no hard coding the size)

        for(int i = 0; i<myArray.length; i++) // NULL EXCEPTION ON THIS LINE WHY??
        {
            System.out.println(myArray[i]);
        }


    }

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter size of array to create: ");
        int num = keyboard.nextInt();

        //*******  FILL IN CODE *********
        // Construct an instance of the OneDimensionalArrays class
        // Using this object instance, call createIntegers to create 
        // an array of integers.  Don't forget to save the results
        // Then call the printArray method to print out the contents
        // of your array.
        DimentionalArray output = new DimentionalArray();
        output.createIntegers(num);
       output.printArray(myArray); 


    }
公共类维度数组{
int[]createIntegers(数组的int size_)
{
//*******填写代码*********
//您的代码将创建一个整数数组,其大小与\u数组的大小\u中指定的大小相同
//用以下值填充数组:0、100、200、300。。。。
//返回刚创建的数组
int[]numarray=新int[数组的大小];
int multilply=100;

对于(int i=0;i首先,您的
createIntegers()
方法实际上并没有填充数组。它只是打印出数组中的元素——这些元素都将为零。您需要更改
System.out.println(numaray[i]);
行以适当地设置
numaray[i]


其次,您的程序甚至无法按原样编译,因为在
main()
中,您没有在任何地方声明
myArray
。并且您没有对调用
output.createIntegers(num)
的返回值做任何操作。您需要声明
myArray
并将其分配给
output.createIntegers(num)的对象
返回。

程序在创建数组方法中只创建了一个名为“namarray”的动态数组,内存中没有名为“myArray”的数组,因此输出。通过引用调用“myArray”的printArray(myArray)正在调用一个不存在的位置。 解决此问题的一个可能的解决方案是将output.printary(myArray);更改为output.printary(namarray); 希望成功!祝你好运! 另外,关于null异常:

我看不出main()调用中的myArray参数是在哪里声明或分配的。