I am a beginner in Java and I have a few problems with null. I need help with understanding the following questions. I have answered them correctly, however I do not know the exact reason behind them.
Question:
Suppose that tracks has been declared with type ArrayList and consider the following:
public Track mostPlayed() {
Track most = tracks.get(0);
int i = 1;
while(i < tracks.size()) {
Track t = tracks.get(i);
if (t.getPlayCount() > most.getPlayCount()) {
most = t;
}
i++;
}
return most;
}
Suppose that a NullPointerException is thrown during an execution of the mostPlayed method. Assuming single-threaded execution, only one of the following lines of code is possible because of this exception. Which one?
I had picked line 2 as it seemed the only logical answer, but I would like further explanation behind it as I don't understand the concept completely.