This week's book giveaway is in the Go forum.
We're giving away four copies of Head First Go and have Jay McGavren on-line!
See this thread for details.
Win a copy of Head First Go this week in the Go forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Problems with NullPointerException in self written LinkedList/HashTable class  RSS feed

 
Ranch Hand
Posts: 53
3
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

Im struggling with NullPointerExceptions when using my self written HashTable .
I want to handle input like this:

add hi
add ih
del hi
find hi
find ih

My output should consist only of "true"/"false"'s that will be printed depending on the success of the find method. In my main method I declared when which method should be called.
I get NullPointerExceptions when using Input like the one above. The problem is that I'm havng trouble to figure out why they occur. I know that it is because I'm trying to call a method on a null reference. The problem only occurs when I add more than one element to the same LinkedList and then delete one of those and then try to find it. Instead of "false" I get NPE. I think the problem lies in the remove method.
NPE aside, I feel like my code isnt really pretty. I might just need a good night of sleep and then take another look at this but I thought you'll probably be able to give me a few nudges in the right direction!



Here is the code of the two relevant classes LinkedList and Hashtable. I'm pretty confident about my Link and TestHashTable classes.


 
Marshal
Posts: 24199
54
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Louis Müller wrote:I get NullPointerExceptions when using Input like the one above. The problem is that I'm havng trouble to figure out why they occur. I know that it is because I'm trying to call a method on a null reference.



Yes, it does. But knowing that somewhere in that code you're calling a method on a null reference isn't particularly helpful, since there's a lot of code. Fortunately the stack trace tells you exactly where you're doing that. So look there. If you can't understand it then you can certainly post it here and ask questions about it.
 
Louis Müller
Ranch Hand
Posts: 53
3
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I definetely should have mentioned this:

when trying the input:
add hi
add ih
del ih
find ih


I'll get an exception similar to this (I adjusted the line and clipped it):


Exception in thread "main" java.lang.NullPointerException
at LinkedList.find(Main.java:51)
 
Louis Müller
Ranch Hand
Posts: 53
3
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is it even possible that a NullPointerException occurs in my find method. To me it looks like I would always catch those through my while and if conditions.


Im really stuck on this.
 
Marshal
Posts: 63496
207
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lines 4‑6 are redundant. What happens if data is null inlne 8?
 
Louis Müller
Ranch Hand
Posts: 53
3
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd get a NPE? First I thought this would be the solution but I think I excluded this as the problem when I changed my code like this:


with Input
add hi
add ih
del ih
find ih


I got

ih data
Exception in thread "main" java.lang.NullPointerException
hi
at LinkedList.find(Main.java:8)

(adjusted the line)

 
Campbell Ritchie
Marshal
Posts: 63496
207
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remove the test for!= null from line 4. You are trying to find nulls, not avoid them. System.out.println(Object) will happily accept null as an argument.
 
Campbell Ritchie
Marshal
Posts: 63496
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Decide whether your data structure accepts null entries or not. If a prohibited null is passed, throw an NPE instead of adding the null. If nulls are permitted, you will have to use a test for equality that accepts nulls.
...x == null ? y == null : x.equals(y)...
Or try this.

But be sure to get the location of the original exception right.
 
Louis Müller
Ranch Hand
Posts: 53
3
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, I think null entries should be accepted. If thisLink is null I have reached the end of my LinkedList.
But my primary concern is how it is possible for a NPE to occur at this part of my code. Because it occurs in Line 8 and I have confirmed that it cant be due to the data oder get.Data part. Therefore it has to be the thisLink. part
But as far as I understood this, thisLink can not be null because it is in the while loop of Line 7 determined like this.
That is why Im having difficulties to grasp the problem here.

Since it only occurs when I add Strings with the same HashCode and then delete one of the two and THEN try to find one of the two, I feel like it has something to do with the combination of the remove method (you can find it in Line 46 of my original post) and the find method.

Your Link brought me to the equals() method and I dont understand what exactly  it has to do with my problem. Would be thankful for clarification!
 
Campbell Ritchie
Marshal
Posts: 63496
207
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Louis Müller wrote:Hm, I think null entries should be accepted. If thisLink is null I have reached the end of my LinkedList. . . .

No, if you add nulls to your List, it should be the data field that points to null.

But my primary concern is how it is possible for a NPE to occur at this part of my code. Because it occurs in Line 8 and I have confirmed that it cant be due to the data oder get.Data part. Therefore it has to be the thisLink. part
But as far as I understood this, thisLink can not be null because it is in the while loop of Line 7 determined like this.  . . .

Have you confirmed that it is not because getData() returns null? I can't see that from the code of the find() method.
 
Campbell Ritchie
Marshal
Posts: 63496
207
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The remove() method looks complicated. And complicated things can hide errors. I can see no need for the complicated changing of size. If you can write this.size++; in line 27, what is wrong with this.size--; in line 55?

You do not need to know whether a node is the root or the end node to add or to remove things. You can use line 18 whether root is null or not. All you need to do is pass null as the second argument to the constructor. In fact you may find you never use the one‑argument constructor and can delete it. Not certain, but I think you would only need a != null t‍est in line 53.
Where are you adding things in line 18? It isn't clear, but it would appear to be at the root. If you have a linked list, you would normally add elements at the end. In which case you would probably want to maintain a reference to the end node.
Why have you got a setSize() method at all?
 
Louis Müller
Ranch Hand
Posts: 53
3
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Have you confirmed that it is not because getData() returns null? I can't see that from the code of the find() method.


I think I did with this part:

when I run the code with the Input we are talking about it prints "hi" which is the data corresponding to the thisLink object. This is a proof that getData != null isnt it? I actually dont think I am right on this because the fact of the matter is that I get a NPE I just dont know why Im wrong thats why Im defending my position.

No, if you add nulls to your List, it should be the data field that points to null.


Hm I think I dont understand what entries we are talking about. So what I meant to say is that it should be possible for entries in my LinkedList to be zero. This is necessary because I use this to remove Links in my LinkedList.
 
Campbell Ritchie
Marshal
Posts: 63496
207
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Louis Müller wrote:. . . it should be possible for entries in my LinkedList to be zero. . . .

Not German null but Java® null. It means there is no object pointed to from that reference. It is completely different from 0..
 
Louis Müller
Ranch Hand
Posts: 53
3
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats what I meant to say. This was a slip.
I wanted to say that entries in my LinkedList can be null. For example in the code in my first post you can see in line 48 that prevLink is set to null because it is the Link before the root Link
 
Sheriff
Posts: 5750
149
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That doesn't sound right.  I believe you should only have a null in the data of the link at the tail (that very end).
 
Campbell Ritchie
Marshal
Posts: 63496
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that is permissible. It is next that is null in the end node, and previous is  null at the root node. Any node may have null for its data field.
 
Louis Müller
Ranch Hand
Posts: 53
3
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hm Im using it to travel through my LinkedList. I could just not initialize it and leave it as
Link prevLink;
But wouldnt that be the same result ? As long as there is no reference the Link variable will refer to null wont it?
 
Campbell Ritchie
Marshal
Posts: 63496
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Louis Müller wrote:. . . Hm Im using it to travel through my LinkedList. I could just not initialize it and leave it as
Link prevLink;

Using what? If you mean can you omit line 3, I think yes you can (not certain) and you can miss all reference to prevLink because you can use thisLink.prev instead.

But wouldnt that be the same result ? As long as there is no reference the Link variable will refer to null wont it?

No, prevLink is a local variable and it therefore has no default value and its value is undefined until you initialise or assign it. Don't worry; the compiler will tell you before you use an undefined value.
 
Louis Müller
Ranch Hand
Posts: 53
3
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I rewrote my entire code which was really neccessary for me to properly understand what happens here. Now I solved all my issues and got full points on my assignment. Fur future readers and for my own understanding I will sumarise what I did. I actually couldnt figure out what was wrong with my first version even tho I now have made the correct version. There has to be a mistake either in the remove/del or in the find/find methods. If somebody would care to point it out, I'd be delighted.

My task was to write 3 classes: Link, LinkedList, Hashtable
With these classes I should store Strings under a certain index which i had to calculate based on a hashfunction. Problem: for example "hi" and "ih" would end up beeing stored in the same position in my array. I had to handle these kinds of collisions with seperate chaining!

The Hashtable is supposed to store the Strings at the given index, but in a roundabout way to avoid collision. It should store a Linked List at the given index in which the different Strings with the same hashcode ("louis" , "oluis" , "sioul") should be stored. Since a LinkedList can not consist of simple String objects because its objects have to be linked with one another (Thats why its called LinkedList) I had to use the Link class in which the String got stored.
The store of the Hashtable is an Array of LinkedLists with the size 31. From what I've read, it'd be a better idea to make the size a variable and be able to dynamically change it (especially expand it) according to the fill-factor of the array to avoid too much collision. This wasnt part of my assignment thus the fixed value.
The LinkedList class had the following intance variables:
a Link root which should be the last Link that has been added to the LinkedList-object
a int size which represents the size of the LinkedList

both of these have no value assigned until one creates an LinkedList object with the constructor that takes a Link and a size as an argument.

The Link class. It stores the data (which are the Strings) and holds a reference to the Link that got added before this one got added. So if this would be the first Link to add to a LinkedList at a given index in my Hashtable store, this reference would be null.

The hastable needs to contain the methods
hash   -  to generate a hashcode. How you do this depends on you or on your assignment.
add  -  add a new String to your List, in order to do this you might need to create a LinkedList at the given index but you surely need to create a new Link that contains the String
del  - delete a Link object that contains the String this method gets passed as an argument
find - print out true if the String that gets passed as an argument is actually in the store
quit  -  simply exit the programm

Here is my code. The main metod looks like this because the input looks like this:
add hi
add ih
del ih
find ih


 
Campbell Ritchie
Marshal
Posts: 63496
207
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Louis Müller wrote:. . . Now I solved all my issues and got full points on my assignment. . . .

Well done
 
Louis Müller
Ranch Hand
Posts: 53
3
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:The remove() method looks complicated. And complicated things can hide errors. I can see no need for the complicated changing of size. If you can write this.size++; in line 27, what is wrong with this.size--; in line 55?

You do not need to know whether a node is the root or the end node to add or to remove things. You can use line 18 whether root is null or not. All you need to do is pass null as the second argument to the constructor. In fact you may find you never use the one‑argument constructor and can delete it. Not certain, but I think you would only need a != null t‍est in line 53.
Where are you adding things in line 18? It isn't clear, but it would appear to be at the root. If you have a linked list, you would normally add elements at the end. In which case you would probably want to maintain a reference to the end node.
Why have you got a setSize() method at all?



I actually missed out on this post of yours entirely. In retrospect, this would've been really useful.
As always, thank you for your time CR!
 
Campbell Ritchie
Marshal
Posts: 63496
207
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Louis Müller wrote:. . .  thank you . . .

That's a pleasure
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
ScroogeXHTML 8.2 - easy to use RTF to HTML converter library
https://coderanch.com/t/707504/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!