-3

Lines 160-164 Hello, I am trying to implement a Least Significant Digit Radix Sort using Java. On line 130 I create the array of Linked Lists that will hold the sorted integers and initialize each element to an empty linked List on line 133. Everything works fine except when I want to iterate over this array of linked list to replace the elements in the original input array in lines 162 and 163. Everytime I run this code I get a NoSuchElementException however if I change line 162 from (bucket != null) to (! bucket.isEmpty()) the code works perfect. Isnt checking if the linkedlist header is equal to null the same thing as checking if the linked list is empty? Error

I am confused why checking if the linked list header is not equal to null gives me an error yet checking if the linked list is empty does not give me an error. Thank you.

vimuth
  • 5,064
  • 33
  • 79
  • 116
  • 6
    Hi. Welcome to Stack Overflow. Please edit the question to show code and error messages as text, not as images. – Old Dog Programmer Mar 27 '23 at 20:56
  • 6
    "Isnt checking if the linkedlist header is equal to null the same thing as checking if the linked list is empty?" It's not. Empty = empty bottle. Null = no bottle. There is a difference between having a container (bottle, bag, etc.) with nothing inside and having nothing. – teapot418 Mar 27 '23 at 20:58
  • 1
    [If you prefer images...](https://medium.com/code-thoughts/the-toilet-paper-dilemma-a1ba48411332) – teapot418 Mar 27 '23 at 21:02
  • 2
    Although it doesn't answer your question directly, I wouldn't use `List[]` I would use List `>` as it will be much easier to work with. You could even use a stream to set defaults: `List> foo = Stream.generate(ArrayList::new)` See this example https://www.geeksforgeeks.org/stream-generate-method-java/ – Christian Bongiorno Mar 27 '23 at 21:34
  • 1
    This is easier to understand if you're a C programmer. `null` is a value that only references can have. In C it is often numerically equal to zero. If you call `new` you have a reference to an object, and this reference would never be equal to `null`. If you called `list.isEmpty()` on a `null` reference, you wouldn't get true or false, you'd get NullPointerException. – markspace Mar 27 '23 at 21:37
  • You should (tldr, fail safe) check *both*!;) `while(!(bucket == null || bucket.isEmpty()))` – xerx593 Mar 27 '23 at 21:38
  • If you can guarantee that your algorithm always generates a new object and never leaves a `null` pointer around, then this isn't necessary. However, you're right that very often this isn't true and you normally have to check that a reference isn't `null` before you dereference it (call methods on it). – markspace Mar 27 '23 at 21:40
  • 1
    Expanding on Old Dog Programmer's comment, see "[Why should I not upload images of code/data/errors when asking a question?](//meta.stackoverflow.com/q/285551/90527)" Please add a [mcve] to the question. – outis Mar 28 '23 at 19:37

1 Answers1

1

No, they're not the same in Java.

In Java a variable (of a non-primitive type) defaults to null until you create or assign an object to it.

So if your method contains this:

List<String> myList;

then your variable myList will be a "null pointer" (null reference) because you've not assigned anything to it yet.

If your method then tries to call any method on this null pointer, such as doing this:

myList.size();

then you'll suffer a NullPointerException because myList is null and so there's no object there to call the size() method on.

But if you now create an object for that variable to point at:

myList = new LinkedList();

then the variable will no longer be a null pointer, it will now be a reference to a List<String> which happens to be empty (because you've added no items into it yet).

So now you can call methods such as myList.size() and myList.isEmpty() without getting a NullPointerException.

Bobulous
  • 12,967
  • 4
  • 37
  • 68