0

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.

5
  • 1
    Please don't include line numbers in your copy data, and format your code with the code button. I edited your question Commented May 19, 2013 at 18:42
  • 1
    Ontopic: If there is an exception, there will be a line number in the exception information. Commented May 19, 2013 at 18:42
  • Hi sorry I am new on the website. Thanks for the edit. Commented May 19, 2013 at 18:43
  • That is why I had the number written on each line so the question would be easier to understand. Bascially the correct answer I have picked is Track most = tracks.get(0); not exactly sure why though Commented May 19, 2013 at 18:44
  • @fge: No the first track has been taken two lines before, so tracks.get(i) is the right way. Commented May 19, 2013 at 18:45

3 Answers 3

4

t.getPlayCount() can throw a NullPointerException if the ArrayList tracks contains a 'null' at some index.

and also Track most = tracks.get(0); can throw a NullPointerException if tracks is not initialized.

Sign up to request clarification or add additional context in comments.

Comments

0

The line Track most = tracks.get(0); can throw a NullPointerException since the code does not show if track was initialized. If it was not, it will be null and it there is no method get in null (there is no method at all, actually).

3 Comments

cant line 6 'tracks.get(i)' also be null for the same reason then?
Yes, but it can't raise a NullPointerException, since the tracks.get(0) already did it.
thanks that makes sense, can you have a look at a similar question for me please regarding null and run time error: stackoverflow.com/questions/16638349/runtime-error-java
0

Java is an object oriented programming language and Objects serve as a basic building block.

In many real life there are cases where objects cannot be created.

A null object represents a special value which says that this object does not exist.

For e.g If I say Person p;. At this point java will assign null value to p. Now If I try something like p.getName() , obviously java will not be able to give me the name of the person since I never created one. Therefore whenever you try to access something using a variable which is null, java trows an error.

There are lots of resources covering this topic if you need a better understanding. Please note that using default/place holder values like null is a very common concept used in even non object oriented languages.

For your question above. Tracks can be null since someone can just do ArrayList<Tracks> tracks and never initialize it using the new keyword.

Furthermore you can do tracks.add(null); which stores a null value into the list. Therefore when you do a tracks.get(i) , you might receive a null value.

And as explained above invoking get or getPlayCount() can therefore cause null pointer exceptions.

I would highly recommend you read up on some of the introductory materials on Java since I believe this topic would be discussed thoroughly in such materials.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.