FuzzyPeanuts
FuzzyPeanuts

Reputation: 9

Can't figure out why I am getting null values

The program is supposed to print out the amount of times each name has been selected (this is indicated by number of asterisks (*). I have most of the code working but for some reason the output contains two null values for each name and I am not sure how to fix the problem. Also if you have the time, I'm also struggling to find out which name has the most amount of asterisks. Here's an example output:

1: nullnull************************ Conner

2: nullnull********************************** John etc... (it does this for all 10 names)

public class RandomStudentsLab {
    public static void main(String[] args){

        //create an array with 10 students
        String [] StudentList = new String[10];
        String [] StarString = new String[10];
        String [] FinalString = new String[10];

        //add 10 names to the student list
        StudentList[0] = "Conner";
        StudentList[1] = "John";
        StudentList[2] = "Alex";
        StudentList[3] = "Robert";
        StudentList[4] = "James";
        StudentList[5] = "Carl";
        StudentList[6] = "Sarah";
        StudentList[7] = "Bob";
        StudentList[8] = "Ethan";
        StudentList[9] = "Chris";


        //loop 250 times selecting each student randomly

        for(int i=0; i<250; i++){
            int randomNum = (int)((Math.random()*10));
            for(int x=0; x<10; x++){
                if(randomNum == x){
                    StarString[x] += "*";
                }
            }
        }



        for(int z=0; z<10; z++){
            System.out.println((z+1)+": "+(FinalString[z] += StarString[z] + " "+StudentList[z]));      
        }

    }

}

Upvotes: 0

Views: 663

Answers (2)

valegians
valegians

Reputation: 940

As mentioned by Karthik in his answer you never initialized (as in you never assigned values) the FinalString array. That is what is causing your error.

You can easily tell since your System.out.println() is printing:

Number : nullnull Stars Name

So clearly your issue is with FinalString, it is the only variable not printing correctly.

System.out.println((z+1)+": "+(FinalString[z] += StarString[z] + " "+StudentList[z]));

You are making the same mistake with the StarString array. You are lucky enough to get away with it in this case since you end up adding variables to StarString in your loop.

However, NOT initialising ANY variable is >>horrible<< practice. You never know what was previously stored in memory, this could lead to your variables being assigned some 'alien' data that was leftover on the memory by some other program. Secondly, and this is the issue in your question, if you forget to initialise you can run into null errors and such.

So as a matter of good coding practice always initialise your variables to something. Even if you are using the variable two lines later - it doesn't matter. When you create your variable assign it a value:

If it's an integer then 0 or -1. It's better if it's a value that won't occur in your program, so if you have an error in your code you can spot it easily because your integer will be -1 instead of x y z. If it's a string then name it 'banana' or 'peanuts' or whatever. If it's an object make sure you initialise all of the attributes And so on...

P.S. Not sure if I came accross as harsh, but it's absolutely not my intention. Good coding practices are simply important and will be extremely helpful in the future.

Best of luck!

EDIT:

Little update to reflect your comment on another answer. When you create FinalString here String [] FinalString = new String[10]; your are not assigning any values to it, unlike what you did with the names.

So when your code gets to the final for loop here:

for(int z=0; z<10; z++){
        System.out.println((z+1)+": "+(FinalString[z] += StarString[z] + " "+StudentList[z]));      
    }

}

}

And you try to do a System.out.println() for FinalString[z], well FinalString[z] still does not have a value. At no point in your code did you write

FinalString[0] = "Banana";

So obviously it will print null instead.

Upvotes: 0

Karthik Janarthanan
Karthik Janarthanan

Reputation: 54

Elements in FinalString and StarString arrays are still not initialized. So with += operator its calling toString on the null element and prefix "null" to each string.

Upvotes: 2

Related Questions