Null issue needs an explanation

For a beginner, this is a tricky one. Let us walk through this code together.

The first important lines are those:

String[] y = new String[Size];
int[] x = new int[Size];

There is a major difference between those two arrays. x is an array of primitives, while y is an array of objects. Primitives have a well-defined default values in Java (typically 0), so the array x is initialized with all cells containing a 0. For objects there is no such thing as a default value. Since you can write your own class, Java does not know what a good default value for, e.g., a Car is. Therefore the default value for objects is null. For the moment, let us define null as a value signaling "This cell is empty" 1. So each cell of y contains null as its value.

Now to the next important lines:

int Min = x[0];
[...]
String Min_studen = y[0];

As explained earlier, x is initialized with all 0. Concluding from this, Min is now 0. We have a problem here because in a typical scenario we will never enter a grade < 0 and therefore Min will never be overwritten. In genereal, if we are looking for some maximum or minimum, we should initialize them with Integer.MIN_VALUE (for the maximum) or Integer.MAX_VALUE (for the minimum) respectively. There are similar constants for long, float and double. Theses constants typically avoid problems like the given one. Since Min is never overwritten, Min_studen is never overwritten either. The initial values of y are nulls, as is Min_studen's value.


Some remarks on your code:

  • Variable names should be written in camelCase.
  • The [] of an array is typically written following the type, without a blank: String[] instead of String [].
  • After a {, there should always be a linebreak.
  • Variable names should be meaningful and descriptive. One could use studentGrades and studentNames instead of x and y.

There are many sites explaining coding conventions and style guides, some of them are here and here 2. You might not want to look at them right now, but keep them in mind for later, if you got the basics of Java down and read them.


1The actual meaning of null is quite close to the description I gave, but the reasoning is a little bit more complex and requires the reader to have knowledge about heap- and stackmemory and what is stored where.

2There is no such thing as "the styleguid". You are of course free to define your own. Style guides are a means to make code easier to read. Please keep this in mind whenever you change your style guide or parts of it.


null basically means that a variable or object is empty. Let's take a look at this section:

String[] y = new String[Size];
int[] x = new int[Size];
...
int Min = x [0];
...
String Min_studen = y[0];

At this point, x[0] and y[0] are still empty/null elements because no values were assigned to them. As a result, Min is 0, and Min_studen is null.

Skip forward to where Min and Min_studen are used:

else if (x[i] < Min) { Min = x[i];
    Min_studen = y [i];
}

Since no element in x[]is defined to be less than 0, this if statement will never run, causing Min_studen to display null when printed.

There is no clear solution as there are several logic errors, but you will need to make sure Min is not null so that the program will run.

Tags:

Java