Nullpointer exception Java -
i beginner in java , have few problems null
. need understanding following questions. have answered them correctly, not know exact reason behind them.
question:
suppose tracks has been declared type arraylist , consider following:
public track mostplayed() { track = tracks.get(0); int = 1; while(i < tracks.size()) { track t = tracks.get(i); if (t.getplaycount() > most.getplaycount()) { = t; } i++; } return most; }
suppose nullpointerexception thrown during execution of mostplayed
method. assuming single-threaded execution, 1 of following lines of code possible because of exception. one?
i had picked line 2 seemed logical answer, further explanation behind don't understand concept completely.
the line track = tracks.get(0);
can throw nullpointerexception
since code not show if track
initialized. if not, null
, there no method get
in null
(there no method @ all, actually).
Comments
Post a Comment