Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Null Pointer on Single Linked Lists  RSS feed

 
Daniel Kim
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I'm currently making linked list but I have no idea why my code is giving me NullPointer Exceptions because everything seems right

Heres part of the code thats not cooperating.


It seems that the code entered into C.addElement(w) recognizes the w as null after completing the for loop once.
Since w becomes null after w = w.getNext();
(w.element.toString().equals(chal.element.toString())) seems to break as chal directs to a null Node.
 
Campbell Ritchie
Marshal
Posts: 55722
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Never never write == false or == true or similar. Not only is it poor style, but also error‑prone. Every now and then we see somebody who wrote = instead of == by mistake and now has two errors for the price of one.
Not
if (b == true) ...
but
if (b) ...
Not
if (b == false) ...
but
if (!b) ...

Call the addElement method add() (if possible). That way it will match the normal names in the List interface.

Those methods look long and complicated. It should be easy to make an add() method for a singly linked list.
Don't make a public method accept a Node as its parameter. Make it take an element instead. You do not want outside code passing nodes, but Elements. You should have a private field which points to the last Node.I cannot tell why you are having null exceptions in that method. That does not look like an ordinary add method. What is that loop supposed to do? You don't normally use loops for adding to a linked list.
 
Daniel Kim
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch
. . .
I cannot tell why you are having null exceptions in that method. That does not look like an ordinary add method. What is that loop supposed to do? You don't normally use loops for adding to a linked list.


The code must be able to take object elements from other single linked lists and make it into a new single linked list.
The Union method adds 2 singular linked lists together into a new linked list and prints out the entire list of Node's elements which are Object ints.
The add function must also be able to find if the Node with the object int is already in the current NodeList and be able to detect and refuse it while returning false.

Yea its a bit confusing because I complicate things with my code :P
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which line of code is given the NPE?
 
Daniel Kim
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Which line of code is given the NPE?


Specifically this one
(w.element.toString().equals(chal.element.toString()))

But this happens because of the code


It adds the Element w once normally, but after it finishes making one
and w= w.getNext(); happens
w becomes Null even though there is supposed to be a Object Int 2 after w normally.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you have this code:


First, the equals operator (==) will test for whether the two references point to the same object. You probably want equals(), but that would depend on how it was implemented in Node.java.

So you're probably not going to get equality, so the if is false, then where does chal get set?
 
Daniel Kim
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Well, you have this code:
. . .
So you're probably not going to get equality, so the if is false, then where does chal get set?


if its false, then it moves onto the
(w.element.toString().equals(chal.element.toString())) code
to see if the new Node;s data is equal to the node currently in the NodeList.
If it is not both then it moves onto the next Node currently in the list to check for that until w.getNext() is trailer again.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is, where does chal get set? I think it's null. Have you tested this?
 
Daniel Kim
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:The question is, where does chal get set? I think it's null. Have you tested this?


Chal gets set in the


I believe it works well once, but then w gets set to null somehow with w = w.getNext();

The testing example used right now is.


So it should work 3 times before it completes, but it only works once
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Kim wrote:
Chal gets set in the


I'm confused. I don't see "chal" in the above code.
 
Daniel Kim
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:. . .
I'm confused. I don't see "chal" in the above code.

I'm not sure what you mean? AddElement is just a method with the variable chal which could be replace by anything.
w , in this case, becomes chal in the addElement
 
Daniel Kim
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Kim wrote:
Knute Snortum wrote:
Daniel Kim wrote:
Chal gets set in the


I'm confused. I don't see "chal" in the above code.

I'm not sure what you mean? AddElement is just a method with the parameter chal which could be replace by anything.
w , in this case, becomes chal in the addElement


changed variable to parameters
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. *sigh* Going blind.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe the problem is

Isn't it possible that head.getNext() will be null at the end of the list?
 
Daniel Kim
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Maybe the problem is

Isn't it possible that head.getNext() will be null at the end of the list?

I don't get how it becomes null though.
I expect it to be null after possibly 3 tries as the test method adds in 3 Nodes, but right after the first one doesn't make sense to me. :/
 
Campbell Ritchie
Marshal
Posts: 55722
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't quote the previous posts in their entirety; that simply makes the thread longer without additional value. Only quote the parts you are answering; I have removed lots of unnecessary quoted text.

I am afraid I think you are going to have to remove some text yourself too. Sorry about this, but I think you have got yourself tied in knots. You are confusing yourself by not knowing where particular variables are, and your code is so convoluted that you can't find your way around it yourself. I don't like saying this, but I think you will be better off deleting all the code you have posted and starting again. Start by turning your computer off and writing on paper what your add method will do. Then write down how you plan to use add to implement the union method. Remember that ∪ (the union operator) is usually applied to sets not lists, so you will have to work out a definition for it. Another thing to do is to find a diagram of a singly‑linked list and have a good look at it. Remember that there is always a null at the end of the list. If you find a good diagram it will show you how a linked list works and it will be really easy to understand
 
Campbell Ritchie
Marshal
Posts: 55722
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Kim wrote:. . .
The add function must also be able to find if the Node with the object int is already in the current NodeList and be able to detect and refuse it while returning false.
. . .
If that is the case you are not creating a List. Lists always support duplicates. You are creating something else.
What is an Object int or an object int? An int is a primitive, so it is not an object. You may mean an Integer object.

You need to think about what you are doing and how you intend to design your app. Have you got a contains() method? Whatever you do, you need to do it in small pieces; if you try to program the whole app in one go and anything goes wrong, you will never be able to work out where it has gone wrong.
 
Daniel Kim
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I somehow fixed it by changing C.addElement(w) to C.addElement(new Node(w.element.toString(), null))
And it made all my methods work

Can anyone explain how it is any different?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!