jQuery in Action, 2nd edition
The moose likes Java in General and the fly likes linkedList getting null element Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "linkedList getting null element" Watch "linkedList getting null element" New topic
Author

linkedList getting null element

Tony Fabeets
Ranch Hand

Joined: Dec 22, 2003
Posts: 38
Debugger show that stuObj was constructed correctly, but when added to DeansList, the DeansList gets an Object NULL.
David Peterson
author
Ranch Hand

Joined: Oct 14, 2001
Posts: 154
You've obviously cut down the code to try and show the relevant bit, but to be able to work out what your bug is, we need to see how you are accessing the linked-list when you get the "null" reference.
As an aside (but it is important): There are standard naming-conventions in the Java world. Classes should always begin with a capital letter, and objects should always begin with lower-case letters. It makes things very confusing to other people if you don't follow this convention. E.g. Your object "DeansList" should be named "deansList". Also, try and choose descriptive names for your variables (e.g. crHrVal is hard to understand).
Anyway, what you've written looks correct. Post a bit more code and we'll see if we can work out what the problem is.
Regards,
David Peterson
Tony Fabeets
Ranch Hand

Joined: Dec 22, 2003
Posts: 38

[ February 16, 2004: Message edited by: Tony Fabeets ]
Tony Fabeets
Ranch Hand

Joined: Dec 22, 2003
Posts: 38
I figured out why null was being added. Was a problem with the calling method, as you suggested.
As of now, i simplified this method...

what i'm trying to make it do is
*Creates Student obj and adds it to DeansList if student has gpa >= 3.5
*return true if add is successful else reutnrs false
*If stuObj is added, add in ascending order via ssn within name
*use overriding compareTo iin Student class
*/
when i try to implement an iterator via

when i use that code, nothing is ever added to LinkedList DeansList. the condition if(stuObj.compareTo(listItr.next() >=0)
is never true.
my compareTo(Object) is as follows.

thank you
[ February 16, 2004: Message edited by: Tony Fabeets ]
[ February 16, 2004: Message edited by: Tony Fabeets ]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8265

Originally posted by Tony Fabeets:

when i use that code, nothing is ever added to LinkedList DeansList.

There's a typo in that code so it will not compile. I hope you left something out when you transcribed it or you've been testing an old version of the class.

would someone be kind enough to draw up a compareTo(Object) that will compare stuObj.name properly?

Nope, we pretty much insist that students do their own work. You can feel free to post what you have and we will be glad to critique it.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Tony Fabeets
Ranch Hand

Joined: Dec 22, 2003
Posts: 38
above post edited accordingly
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8265


Thanks for fixing that. Don't want to waste time on red herrings.
Comparing String objects using "==" actually evaluates if the String handles are pointing to the same instance of a String object. It does not evaluate the contents of the String instance. Consider the following code:

You can see that even though str1 and str2 both contain the string "blah", they will not be "==" to each other. One should always use String.equals() when testing for equivalence.
IMPORTANT NOTE: if str2 were declared:

the test "==" would evaluate to true. This behavior fools a lot of people into thinking they can use "==" to evaluate String objects. The reason that "==" works in this case is because the compiler sets the literal references to "blah" to the same String instance in the literal pool.
Perhaps you should look at using String.compare() in your case. . .
Tony Fabeets
Ranch Hand

Joined: Dec 22, 2003
Posts: 38
beginners mistake :-/
I now see how to compare if strings are equals, but would would i find whether to return -1 or 1?

the above code will not compile and the code below never lets the condition to add student be true. With all the code provided, nothing is added to list, but when i strip the add method down, as in the first post, everything works fine. So, we've narrowed the problem down, but not seeing where.
[ February 16, 2004: Message edited by: Tony Fabeets ]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8265

I'm curious. If we have established that "==" evaluates the object handle and not the contents of the object, why would you assume that greater than or less than works differently?
HINT:
Perhaps you should look at using String.compare() in your case. . .
/HINT
[ February 16, 2004: Message edited by: Joe Ess ]
Tony Fabeets
Ranch Hand

Joined: Dec 22, 2003
Posts: 38
Here's what i've come up with and still, nothing is true in the condition
if(stuObj.compare(i.next()) >=0)

[ February 16, 2004: Message edited by: Tony Fabeets ]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8265

Originally posted by Tony Fabeets:
Here's what i've come up with and still, nothing is true in the condition
if(stuObj.compare(i.next()) >=0)

What names are in your list and what are you adding?
[ February 16, 2004: Message edited by: Joe Ess ]
Tony Fabeets
Ranch Hand

Joined: Dec 22, 2003
Posts: 38
not getting all the proper results, but have a good start. thanks for the help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: linkedList getting null element
 
Similar Threads
adding obj to LinkedList via string value
getting java.lang.NoSuchMethodError: main error! thanks =)
alpabetical insertaion into linkedList
hashing problem
Compile error