• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NullPointerException

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm writing the add(int position, T data) method for a doublyLinkedList. I'm attempting to use the method but am generating a NullPointerException error when I attempt to add to the end of a list.

For example, I am trying to do:



and I'm receiving an error for test.add(1,2).
Here is my code for my add method:


I am tracing the error back to the line in the else loop:

but I can't figure out what is causing this.

Thank you in advance for your help!
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carly Griffin wrote:Hi, I'm writing the add(int position, T data) method for a doublyLinkedList. I'm attempting to use the method but am generating a NullPointerException error when I attempt to add to the end of a list.


Rather than me try to debug your code, let me give you a good tip:
  A NullPointerException is only thrown by the JVM (as opposed to deliberately) when a null reference is used as if it were a real object.

And 99.99% of the time it is because a reference variable (or parameter) that you created is null at the time that you want to use it, or you pass to a method that hopes to use it.

Now all of that may seem obvious, but hopefully it highlights a few things that can help you find the problem for yourself (and you only have a dozen or so lines to check).
Specifically:
1. Look for the first line in the stack trace that refers to a method in your program. (in your case it will probably be the first one)
2. Look for a reference (ie, an object or variable), NOT a primitive, in that line.
Often, there will be only one, or it will be immediately obvious to you what the problem is.
But if not, for ALL the references you find, work backwards from the line you started at to work out when that variable was created or changed.

Computers and Java are not capricious so, if you have a problem, it is almost always YOUR fault. And learning how to discover errors for yourself will make you a better programmer - even if it doesn't seem like it at the moment.

HIH

Winston
 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have a real problem to your approach: you are trying to use linked list like an array, and basically--stop it! You are trying to insert into position, and position is empty, but position-1 is populated.


If you are going to work with linked lists, then work with linked lists, and lose the array thinking.

Carly Griffin wrote:Hi, I'm writing the add(int position, T data) method for a doublyLinkedList. I'm attempting to use the method but am generating a NullPointerException error when I attempt to add to the end of a list.

For example, I am trying to do:



and I'm receiving an error for test.add(1,2).
Here is my code for my add method:


I am tracing the error back to the line in the else loop:

but I can't figure out what is causing this.

Thank you in advance for your help!

 
Carly Griffin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Les - I'm just doing this according to how I was instructed, but thank you for taking the time to get back to me!

Winston - Thank you so much! I really enjoy debugging myself, but I just needed that extra push of understanding the thinking behind it (as I am relatively new to the language). I have since resolved the issue, but am encountering a new issue. I'm going through and attempting to throw exceptions where necessary, but am generating the error, "cannot find symbol symbol : class *insert name of exception here*".

For example:


generates the "cannot find symbol symbol : class IndexOutofBoundsException"

I've looked into throwing exceptions a lot and I truly don't understand what I could be doing wrong.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please post your complete instructions? We often see students misinterpret instructions so it would help us in giving you advice if we didn't have to guess at what your constraints are for this problem.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You probably have a small o in the name of the Exception when it should be a capital O.
You are right to throw that sort of Exception. Far better than letting it find a null and throw a null exception (=NPE).
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You are right to throw that sort of Exception. Far better than letting it find a null and throw a null exception (=NPE).


Well, sometimes it's fine to let the program throw a NullPointerException. That still points to a programmer error that should be corrected or a condition that needs to be checked prior to calling the method that can throw the NPE.

The reason I would say that it's a good to throw the IndexOutOfBoundsException in this case is this: It enforces a contract that the method should only be called with legal index values. The checking and throwing of an exception is more intentional rather than incidental. There could be code that follows that guard clause (the if-statement that checks for acceptable values of the parameter) that would throw an IndexOutOfBoundsException anyway, or it could, as Campbell said, just throw a NullPointerException. When you have a guard clause, you know for sure that the method will always throw an IndexOutOfBoundsException when given an invalid parameter. In other words, throwing the exception is by design, not by accident.
 
Les Morgan
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carly Griffin wrote:...I'm just doing this according to how I was instructed...



I would like to see that instruction. You absolutely can insert into a linked list into the Kth position, but the approach you are using is to treat the linked list as an array and it is not.

Think of a linked list as a piece of rope under tension, it has a beginning and an end and some length inbetween. If you want to insert something, say a red piece of rope, into the middle of it, then you have to secure the new piece and make the cut so the new piece will bridge that gap. If you make the cut before the new rope has been secured in, then you lose the part you didn't secure.

You traverse the list by following the links, and backtrack in a doubly linked list by following the back links, when a specific place is needed to stop, you should be using a counter to keep track of the steps. Your code does not make sufficient use of endpoint detection, because it is relying on an array type of concept, and not on the link list structure.

Les
reply
    Bookmark Topic Watch Topic
  • New Topic