0

what is my console giving me trouble in main, keeps giving me an nullexception error in my void on the for loop. why is it going null??? can it be because in my argument myArray is not going to createIntegers? i cant display my array in main

    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); 


    }
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216

2 Answers2

0

First, your createIntegers() method doesn't actually fill in the array. It's just printing out the elements in the array -- which will all be zero. You need to change the System.out.println(numarray[i]); line to set numarray[i] appropriately.

Second, your program won't even compile as it stands because in main() you do not have myArray declared anywhere. And you aren't doing anything with the return value of the call to output.createIntegers(num). You need to declare myArray and assign it to what output.createIntegers(num) returns.

QuantumMechanic
  • 13,795
  • 4
  • 45
  • 66
0

The program only created a dynamic array called "namarray" in the creating array method, there is no array called "myArray" in the memory, therefore output.printArray(myArray) which is calling "myArray" by reference is calling an inexistent location. One possible solution to fix this is changing the output.printArray(myArray); to output.printArray(namarray); Hope it works! Good luck! Also, about the null exception: What is a NullPointerException, and how do I fix it?

Community
  • 1
  • 1
Albert
  • 29
  • 6