0

I want to figure out why my array that is declared outside of a for loop refuses to be properly initialized by a for loop in order to be used by another for loop. Googling "array null pointer exception" and "null arrays" kept bringing me to errors about not initializing the array. Hopefully that's not what's happening here. (I don't think it is.)

public static void main(String[] args)
    {
        Student[] students = new Student[25];      
        for(Student student : students)
        {
            int grade=(int)(Math.random()*4 +1);
            student = new Student("bob",grade);
            System.out.println(student); //debug 1
        }
        for(Student student : students)
        {
            // if (student.getGPA() == 0){
            //     System.out.println("No data entered for student.");
            // }
            System.out.println(student); //debug 2
        }
    }

This block is just main, and it's in another valid class named after the file. Student is a class that I excluded because it has nothing to do with the question being asked, but for reference it takes a String name and an int gradeLevel as parameters and has toString() and getGPA() methods (among others).

What I want from debug 2 and get from debug 1: something along the lines of

bob: Freshman; GPA: 0.00
bob: Senior; GPA: 0.00
bob: Freshman; GPA: 0.00
bob: Sophomore; GPA: 0.00
bob: Senior; GPA: 0.00

What I have from debug 2:

null
null
null
null
null

I want the print statement marked "debug 2" to print the same thing as what "debug 1" does, which is a column of the toString values of the students. Instead, I'm just getting a column of null for debug 2 but the correct output for debug 1.

The commented-out code was the source of null pointer exceptions because null objects do not have getGPA() methods. I have no idea why the array remains full of nulls, though. The fact that there aren't nulls from debug 1 tells me that the array is indeed being initialized, but that the values aren't staying in the array.

I probably completely misunderstand something or the use of something here.

d-h-h
  • 17
  • You can't manipulate an array like that, Java is not pass by reference. You need to explicitly assign the value to the array, so iterate using index and use `students[index] = new Student(..)` – Mark Rotteveel Dec 06 '17 at 09:38
  • Yes, the original duplicate doesn't even have an accepted answer, let alone be about this specific situation, which has nothing to do with pass by reference/value since the reason the code does not work is that the reference to the new student is never written back to the array. – JeremyP Dec 06 '17 at 09:42
  • Thank you, I was looking in the wrong place! The links provided in the duplicate thing were able to provide with the information I needed. – d-h-h Dec 06 '17 at 09:44
  • @JeremyP which would happen in "pass by reference", thus "this specific situation, which has nothing to do with pass by reference/value" is obviously wrong. And a dupe question doesn't need an ___accepted___ answer. – Tom Dec 06 '17 at 09:46
  • @Tom No. The issue in this question is entirely due to not writing the reference back to the array. There is no passing by value or reference involved because there are no function calls. This is obviously true because the dupe that you posted exhibits the same problem as this question and it uses a primitive type. – JeremyP Dec 06 '17 at 09:58
  • @Tom and it may not be a requirement that the dupe has an accepted answer, but it is a courtesy. The questioner wants an answer to their question. Unless you are closing the question to spite them, why not use a dupe that a) relates to their actual problem and b) has an accepted answer? – JeremyP Dec 06 '17 at 10:03
  • @JeremyP "The issue in this question is entirely due to not writing the reference back to the array." which wouldn't be necessary if Java were fully "Pass by reference", which would then also apply to references returned by methods. That "enhanced for" doesn't has method calls is obviously BS, as it is syntactic sugar for using an iterator and the compiler will translate it to that. "The questioner wants an answer to their question." and that obviously doesn't need to be in the accepted answer. Any other answer is suitable as well (as long as it delivers a solution tot he problem). – Tom Dec 06 '17 at 11:49
  • @Tom The questioner wouldn't be asking the question if he could pick out the right answer from a load of possibly right and possibly wrong answers. And the mechanism of how "for each" works is irrelevant. If he was obtaining the reference with a traditional for loop, and getting the reference by indexing the array, this problem would still be occurring. – JeremyP Dec 06 '17 at 14:42

0 Answers0