0
[SOLVED] Why does my version of linked List doesn't work?
Hey I coded this recursive version of a linked list. As you can see, putting the first element works fine, the second element is being stored as well but when you try to access it, it is a null pointer. Where is my mistake? https://code.sololearn.com/cMhgkC9qix9f/?ref=app
7 Réponses
+ 5
Na dann gibt es hiermit ein kräftiges Moin, moin von einem Hamburger in Berlin... (okay ist jetzt nicht ganz so spektakulär).
(Switching back to English for the sake of others reading this post)
You can't do that because by calling "add2" with root.getRef() you were just passing a null value because ref was only declared but not initiated then. You could have already initiated it by "storing" an Element with a placeholder value in ref. That would actually work but then you would always have an element with a placeholder value in your list. That is why it is better to pass the current element.
+ 4
I „repaired“ it for you and wrote some comments to explain the changes that were needed. Just look into the codes you can find under my profile. I made it public.
+ 4
No problem. Let me know if you got any questions. If not, you can mark the questions as solved I guess. ;-)
0
Thank you very much!
0
I still don't get why I can't give the function the reference of root instead of the root and then getting the reference in the next function, since the storage for it exists and I fill it with something (a new Element) in that next function. Can't I give a function an empty object as a parameter an then declare that object in that same function?
0
Greetings from a German in Chile btw ;)
0
Thank you for your help and effort! Everything I needed to know ;)