-1

The goal: create array with x size by random numbers and sort numbers.

Array.java

import java.util.Random;

/**
 * Created by cazorla19 on 05.03.16.
 */
public class Array {

    int myArray[];
    Random rand;

    Array() {
    }

    Array (int x) {
        Array myArray[];
        myArray = new Array[x];
        for (int i=0; i<x; i++) {
            myArray[i] = new Array();
        }
    }

    int[] ArrayFill () {
        for (int i=0; i<myArray.length; i++) {
            int j = rand.nextInt();
            myArray[i] = j;
        }
        return myArray;
    }

    int[] ArraySort (){
        System.out.println ("Here is the Bubble sort!");
        for (int i=0; i<myArray.length; i++) {
            for (int j=0; j<myArray.length - i - 1; j++) {
                if (myArray[j] > myArray[j+1]) {
                    int temp = myArray[j];
                    myArray[j] = myArray [j+1];
                    myArray[j+1] = temp;
                }
            }
        }
        return myArray;
    }

    void ArrayPrint() {
        for (int i=0; i<myArray.length; i++) {
            System.out.print (myArray[i] + ", ");
        }
    }

}

ArrayDo.java

/**
 * Created by cazorla19 on 05.03.16.
 */
public class ArrayDo extends Array {

    public static void main (String args[]) {
        int x = 10;
        Array labArray = new Array(x);
        labArray.ArrayFill();
        labArray.ArrayPrint();
        labArray.ArraySort();
        labArray.ArrayPrint();
    }
}

Kind of exception.

Exception in thread "main" java.lang.NullPointerException
    at Array.ArrayFill(Array.java:23)
    at ArrayDo.main(ArrayDo.java:10)

That's where I tried to use length of array. Tried to figured out through this issues [1][2]. It's thought I can't change array values from NULL despite of I did in array constructor. Did anyone force with similar issue? I know it should be pretty easy, but still can't realize what's wrong.

cazorla19
  • 284
  • 3
  • 12

3 Answers3

5

Your constructor makes no sense. It initializes a local array of a different type then the array member with the same name, which means the array member remains uninitialized.

It should simply be :

Array (int x) {
    myArray = new int[x];
}

You don't need an array of Array instances (Array myArray[]).

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Right. I did it beforehand, but Java interpreter got stuck on null exception. I thought it's a problem with null-valued array, but problem was lied on Random function. Thanks. – cazorla19 Mar 05 '16 at 15:59
4

The problem is constructor Array(int x) doesn't initializes class variable int myArray[]. It only initializes local variable Array myArray[] defined within constructor.

AVI
  • 5,516
  • 5
  • 29
  • 38
0

Initialize the below values and you should be good to go!

int myArray[]=new int[10];
Random rand=new Random();
ashwinsakthi
  • 1,856
  • 4
  • 27
  • 56
  • 1
    Oh yeah! Actually issue was in random. I didn't declare it properly, variable wasn't Random class instance, so array was good. Thanks a lot! – cazorla19 Mar 05 '16 at 12:00