• 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

really weird linked list error, please help!!

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am really confused. I have a linked list, which I have created from scratch, yup this is hw!

The problem I am having is the following:

I have a find method, which will take in an element as an argument, and search the list, and return the node to where the element is located.

Suppose I have the following code:



The above code works fine, and it prints out the value of tmp is "A".

But, if I try the following:



If I input A at the console, it throws back a NullPointerException. I am confused because, if I am inputting the SAME thing at the console, that I am manually putting in, why wont it find it?!?!?!?!



Any help would be greatly appreciated!

Thanks,
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The find() methods code would certainly help us help you find the answer, but in this case I'm guessing that you are using == to test for equality. For objects, you should use the equals(Object) method defined in the Object class to test equality as == only tests that two object references are equal -- it doesn't check the contents (fields) of those objects.

The reason this works for "A" is that the compiler uses the same instance to represent all "A" constants in that same class. So even though you have "A" in two places in your test class, the compiler uses a single String object for them both. This allows == to be true in this case.
 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David may very well be correct in his guess. but either way, it's also possible that your Console.readLine() method is returning an entire line of input -- including the terminating CR/LF characters from your pressing the return key! -- and that no such string is in your linked list. therefore, you might have to trim your input before searching for it.
 
Subhash L. Sriram
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks for the quick reply!

The following is my find() method:



Should I be changing it to using the equals method?

M Beck,

Could you explain how I would trim the input?

Thanks guys, I really appreciate it
[ February 20, 2005: Message edited by: Subhash L. Sriram ]
 
Subhash L. Sriram
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David,

You are a genius!

I changed it to this:



And it works fine! Man, that .equals(theItem) and == sure are tricky, huh?

Thanks so much! I have been working on this for hours.......
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Subhash L. Sriram:
Man, that .equals(theItem) and == sure are tricky, huh?

You said it! It nails every Java beginner as they're learning, and it continues to trip up Java experts to no end. One more trick to be aware of is that Object's definition of equals() is equivalent to ==.

Since you're using Strings and String overrides equals() to compare the characters in the String itself, you're okay. If, however, you create your own class to store in the LinkedList, you'll need to override equals() appropriately.

For example, if you created a Student class with a student ID (unique number), name, and age, you would override equals() to compare two Students' IDs since it uniquely identifies a Student.
 
Subhash L. Sriram
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David,

Thanks for the reply. Now, I am having another problem. There is also a remove() method for my LinkedList class, that finds a certain object, and then deletes it from the list.

It looks like this:



Now, if I have some input, and feed it to the remove method, it gives me another NullPointerException

The find() method is what I have posted earlier. This will not even work explicitly though?

Do you know what I am doing wrong?

Thanks again.
[ February 20, 2005: Message edited by: Subhash L. Sriram ]
 
Subhash L. Sriram
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never mind, stupid mistake, got it working....
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without the stack trace from the NullPointerException, again I must guess. In the future, please post the stack trace and point out the line in your code where it is thrown (since we can't see the same line numbers as you). Armed with this, we can help you more easily.

There are four places where an NPE could be thrown in that code. The first two are in popFront() and popBack(), but you didn't post them so I can't help there.

The other two cases are in these lines:First, if tmp is null, you'll get an NPE straight away. When would tmp be null?

Second, if either getPreviousNode() or getNextNode() return null, again you'll get an NPE. Ordinarily those will only be null for the first-node and last-node cases, which you've specifically checked already. It's possible that the next and previous references in ListNode are not being kept up-to-date in every method that modifies them, so double-check the cases where you need to set them.

Finally, what happens to dataSize when you remove either the first or last node in the list?
 
Subhash L. Sriram
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Like I said, it was a stupid mistake that I figured out, but thank you for the explanation, it is easier to understand it now.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Subhash L. Sriram:
Like I said, it was a stupid mistake that I figured out, but thank you for the explanation, it is easier to understand it now.

Oops, you found it while I was writing my post. Also, no mistake is stupid if you learn from it.
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh, and about the possible suggestion i had -- since, even though it turned out not to be necessary in this case, it might come in handy elsewhere:

basically, you want to look at the last character of the string, check to see if it's a character you might be interested in (like a letter, maybe), and go on looking towards the start of the string if it's not. when you find the first character of the string (counting from the end) that's one of the characters you care about, you ignore everything from that point on to the end, using (for example) a substring() method call. explore the String class and its methods for hints on how to do this sort of thing.

(i'm frankly surprised Java doesn't seem to have a configurable trim() method in String. you'd think people might want to strip off things other than whitespace often enough.)
 
Subhash L. Sriram
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by M Beck:
oh, and about the possible suggestion i had -- since, even though it turned out not to be necessary in this case, it might come in handy elsewhere:

basically, you want to look at the last character of the string, check to see if it's a character you might be interested in (like a letter, maybe), and go on looking towards the start of the string if it's not. when you find the first character of the string (counting from the end) that's one of the characters you care about, you ignore everything from that point on to the end, using (for example) a substring() method call. explore the String class and its methods for hints on how to do this sort of thing.

(i'm frankly surprised Java doesn't seem to have a configurable trim() method in String. you'd think people might want to strip off things other than whitespace often enough.)



Thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic