• Post Reply Bookmark Topic Watch Topic
  • New Topic

Linked List - Inserting node into specific position - I'm stuck on a nullpointerexception  RSS feed

 
Casey Hyland
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I downloaded source code for an assignment that tells me to modify the program ONLY by adding one method to one class and then the test code to test that class. The method should insert a new node into the third position on the list. I'm not sure how much anyone needs to see to be able to help, but here's the method I coded:



Since the nullpointerexception was pointing to current = current.getNext first, then maybe the problem is with my linking pN to current rather than previous? The exception also points to where I call the above method in the test class, so maybe I didn't call it properly? Here's part of the test class where I have put my code so far:



Only the parts I labeled in the code were coded by me. The rest was already there. If someone could help me understand why I'm getting this exception and help me get past it, then maybe I can start at least generating some proper errors and get rolling again lol.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcometo the Ranch

I had to change those // comments because they made the lines too long.

That code looks as if you are guessing. You can get code by guessing; if you take 1000000 different guesses, there is a good chance one of them will work. Or, you can work out what you want first, and get it to work first time.

What methods do they have for finding positions in that List? If you can add things at the top of the List, and remove things from the top of the List, it should be easy to combine them to insert an element in the 3rd position. If you can find a node and its successor, then you can find the 3rd node and add something there.
There are lots of other ways you can do it.



 
Casey Hyland
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcometo the Ranch

I had to change those // comments because they made the lines too long.

That code looks as if you are guessing. You can get code by guessing; if you take 1000000 different guesses, there is a good chance one of them will work. Or, you can work out what you want first, and get it to work first time.

What methods do they have for finding positions in that List? If you can add things at the top of the List, and remove things from the top of the List, it should be easy to combine them to insert an element in the 3rd position. If you can find a node and its successor, then you can find the 3rd node and add something there.
There are lots of other ways you can do it.





Thank you And I kind of was guessing lol. After a while at least. I got frustrated with the exception and kinda just started throwing lines of code around to get rid of the stupid red letters. I'm just going to rework my code. I found an example online that I don't really understand. It doesn't use current or previous. Can someone explain what's happening here with the first two lines? I've never seen code that has something like .getNext().getNext()

 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yo're welcome

No, we can't help if you simply download something; there ought to be an explanation with the download. And we won't try to explain guesswork!
We will help you thinking, though. What you need to do is change to paper and pencil and draw a diagram of that list and also work out what add 3rd means:
  • Java lists are usually 0-based, so element 3 has 3 elements preceding and is actually the 4th element.
  • Are you going to call myList.add(3, myElement)? That would match the List interface's method.
  • Depending on the structure of your List, I can think of at least two ways to add a 3rd element.

    What are you going to do if the list is too small? You can't add a 3rd element to a 1‑element list. You will probably have to find an appropriate Exception to throw.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Casey Hyland wrote:
    Campbell Ritchie wrote:I had to change those // comments because they made the lines too long.
    Thank you And I kind of was guessing lol.

    But unfortunately, you've added ANOTHER piece of code with a very long line in it (I've broken it up this time).
    80 characters MAX. Please.
    And that includes long strings, long method calls, AND comments.

    You're basically shooting yourself in the foot if you don't, because if your thread is difficult to read, you're unlikely to get much help.

    However, I will say this: without knowing what the PlayerNode class looks like, it's difficult to say for certain what's wrong.

    Winston
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!