1

I have the problem with the following method. It is supposed to create an array, fill it up with objects, and return the filled array. However, for a reason unknown to me, the returned array is full of null values.

 public Rodent[] getRodents(int amount) {
        Random rng = new Random();
        Rodent[] rodentArray = new Rodent[amount];

        for (Rodent r : rodentArray) {
            switch (rng.nextInt(3)) {
                case 0:
                    r = new Mouse(); // Subclass of Rodent
                    break;
                case 1:
                    r = new Gebril(); // Subclass of Rodent
                    break;
                case 2:
                    r = new Hamster(); // Subclass of Rodent
                    break;
                default:
                    r = new Rodent(); // For safety
            }
            System.out.println(r.getName()); // Test command inside the loop. Prints the name of all object correctly.
        }

        for (Rodent r : rodentArray) { // Test loop outside of previous loop.
            System.out.println(r.getName()); // Throws null pointer exception.
        }

        return rodentArray; // Returns null filled array
    }

EDIT:

Found my answer. Thanks. Now i need to stop thinking about the enhanced for loop like a pointer.

1
  • You are simply reassigning a local variable. If you get into the habit of declaring your local variables as final when they are constant then this error would be flagged up by the compiler. Commented Apr 20, 2014 at 18:51

2 Answers 2

4

r is a local variable in your loop that does not exist outside a given iteration. Your code is equivalent to the code below where it is maybe easier to see that the r is discarded at the end of each iteration:

for (int i = 0; i < rodentArray.length; i++) {
    Rodent r = rodentArray[i];
    //your switch here
}

So in your case you should probably use a standard for loop and fill the array with rodentArray[i] = new Mouse(); or with other animals as appropriate.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, r is a variable. But it is not needed. It is just supposed to point out to an array element. Mouse m1 = new Mouse(); Mouse m2 = m1; m1 = null; m2 will still hold the object. Thats why i think that losing r variable is not a problem.
@user2964403 it is a local variable which is a reference to the array element. Reassigning the variable does not change the value it is referencing. This is Java 101.
0

You are never putting newly created rodent object into array. Here is the correct code -

public Rodent[] getRodents(int amount) {
    Random rng = new Random();
    Rodent[] rodentArray = new Rodent[amount];

    for(int count = 0; count < rodentArray.length; count++) {
            Rodent r;
            switch (rng.nextInt(3)) {
            case 0:
                r = new Mouse(); // Subclass of Rodent
                break;
            case 1:
                r = new Gebril(); // Subclass of Rodent
                break;
            case 2:
                r = new Hamster(); // Subclass of Rodent
                break;
            default:
                r = new Rodent(); // For safety
        }
        rodentArray[count] = r;
        System.out.println(r.getName()); // Test command inside the loop. Prints the name of all object correctly.
    }

    for (Rodent r : rodentArray) { // Test loop outside of previous loop.
        System.out.println(r.getName()); // Throws null pointer exception.
    }

    return rodentArray; // Returns null filled array
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.