File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Help with Linked Lists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with Linked Lists" Watch "Help with Linked Lists" New topic
Author

Help with Linked Lists

L One
Greenhorn

Joined: Oct 09, 2005
Posts: 6
I have an array of linked lists
LinkedList[] mylist = new LinkedList[10];

and I added an object called Entry that has 2 parameters to the list

The problem I'm having is that when I try to compare name with one in my linked list, it appears to be null, even when I added something to it.

here's a little snippet

String name = "Bob";
int age = 13;

if(mylist[0] == null)
{
//add the entry right away since there is nothing in the list.
mylist[0] = new LinkedList();
mylist[0].add(new Entry(name, age))
}
else //there are entries in the list
{
ListIterator it = mylist[0].listIterator();
Entry p = (Entry) it.next();

//checking if it exists
while(it.hasNext())
{
if(p.getName().equals(name))
{
System.out.println("Already exists");
}
else
{
//add the entry
mylist[0].add(new Entry(name,age));
}
}

this code will always add since the p.getName() is null, can someone please help me fix this?

Thank you.
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
I'm not sure to really understand your problem, but with the code you've posted, I can say that :
- you'll never enter the "while" loop OR you'll enter in an infinite loop OR you'll have an error :

If there's 1 item in you're list, it.next() will give you this item, and it.hasNext() will be false.

If there's more than 1 item in your list, it.next() will give you the first item, and it.hasNext() will be always true since you don't call it.next() in the loop.

If ther's no item in your list, (possible even if your list is not null), it.next() will throw an Exception.


Regards,<br />Seb<br /> <br />SCJP 1.4
L One
Greenhorn

Joined: Oct 09, 2005
Posts: 6
thanks for the info.
but the thing is I don't know why, my value is null, right after I have added it to the list.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061

to

I don't know if this will fix the problem you are asking about, but it will fix other problems in your code.

With that said, please provide some more detail as I asked for above. It will help us to understand what your problem is so that we can help you further.

Layne
[ October 09, 2005: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
L One
Greenhorn

Joined: Oct 09, 2005
Posts: 6
This method I'm trying to code is adding entries to linked lists and if the user enters the same entry it will not add it.
After I added one entry into the list, I checked the value and it was null.

if(mylist[0] == null)
{
//add the entry right away since there is nothing in the list.
mylist[0] = new LinkedList();
mylist[0].add(new Entry(name, age))
}
else //there are entries in the list
{
ListIterator it = mylist[0].listIterator();

//checking if it exists
while(it.hasNext())
{
Entry p = (Entry) it.next();

System.out.println("The name is "+ p.getName()); --> outputs null

if(p.getName().equals(name)) <-- I get a null pointer exception here too
{
System.out.println("Already exists");
}
else
{
//add the entry
mylist[0].add(new Entry(name,age));
}
}

Does this mean that the values are not adding to the list since the p.getName() returned null?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Does this mean that the values are not adding to the list since the p.getName() returned null?

Not at all. If the values were not being added to the list, then p would be null and this line would cause a NullPointerException. The fact that the SOP is printing out "null" means that the name used to initialize the Entry is null. So where does the name come from? Where is name declared and initialized? If you can show us this part of the code, we can help you further.

I also have a question about your design: Why are you using an array of Lists? Does each list hold different elements? From the code that you have posted so far, it looks like you are only using the first list in the array anyway, so why do you need an array rather than just a single list?

Besides that, there is an easier way to avoid duplications. It's called Set. Any class that implements the Set interface, such as HashSet or TreeSet, will guarantee that the set does not contain any duplicates. To get this to work, you will need to override the equals() and hashCode() methods in your Entry class, but it will probably result in less code.

If you have to use ArrayList, you should look at the contains() method. This will do all the work of your while loop with a single method call.

These suggestions won't fix your problem, but they will make your code a little more manageable so that we can continue to track down the problem. If you don't want to make these changes to this code, just be sure keep these suggestions in mind for future projects.

Layne
[ October 09, 2005: Message edited by: Layne Lund ]
L One
Greenhorn

Joined: Oct 09, 2005
Posts: 6
The name is from the main method, like a user inputs the name and age and then an addEntry(name,age) method is called to another class which has this method.
Its a requirement not to use hashtables and hashcode, so I am using an array of linked lists for the hashtable and I'm designing my own function to position the Entry objects in list.

the main method:
theTable tb = new theTable();
try
{
tb.addEntry("Bob","13");
System.out.println("Test Successful");
}
catch(Exception e)
{
System.out.println("Test Unsuccessful");
}

In theTable class:
I declared the LinkedList[] list; and then in the constructor I created the list. (list = new LinkedList[10]

In the addEntry(String name, int age) method:
-have made a function to determine where position of the entry is put.
this is called pos.

//If there are no nodes in the linked link, it adds it right away
if(list[pos] == null)
{
list[pos] = new LinkedList();
list[pos].add(new Entry(name, age));
}
else //there are nodes in the list
{
ListIterator it = list[pos].listIterator();
//find if it exists...

while(it.hasNext())
{
Entry p = (Entry) it.next();
System.out.println("The entry key is: "+ p.key());
if (p.getName().equals(name))
{
System.out.println("Already exists");
}
else
{
list[pos].add(new Entry(name, age));
}
}//end while
}//end else

I have another class for Entry and all it has are the get methods of name and age.

What can I do to fix this?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Ah, so you are writing your own hashtable class. That makes sense why you have an array of ArrayLists!

The example code you have shown so far shouldn't cause this error, so I suspect the problem is somewhere else. To help you track down the problem, I think you should make the following changes:
  • Add some code at the beginning of addEntry() to make sure that its parameters are not null. For example, you can do something like this:


  • You should do this for ALL parameters.
  • In main() you have a try...catch to detect whether the test succeeds or fails. While the message "Test Unsuccessful" is somewhat helpful, you are suppressing information that goes along with the exception that you catch. You should add the following line in the catch clause:




  • These two changes will help you see what is happening in your code. Once you make these changes, please post the output and I will help you from there.

    Layne

    [ October 09, 2005: Message edited by: Layne Lund ]
    [ October 09, 2005: Message edited by: Layne Lund ]
    L One
    Greenhorn

    Joined: Oct 09, 2005
    Posts: 6
    the values that are sent through are not null, and I couldn't put the e.printStackTrace() because it wouldn't allow me to, it says it needs to be void. So I still get the null pointer exception at this line : p.getName().equals(name).
    Layne Lund
    Ranch Hand

    Joined: Dec 06, 2001
    Posts: 3061
    Originally posted by L One:
    the values that are sent through are not null, and I couldn't put the e.printStackTrace() because it wouldn't allow me to, it says it needs to be void. So I still get the null pointer exception at this line : p.getName().equals(name).


    What said what needs to be void? Where did you put e.printStackTrace()?

    Perhaps I wasn't clear about the change I was suggesting. You should do this:



    However, as I stated above, I don't see anything in this code that will cause a NullPointerException (NPE). In fact, the orignal code won't tell you that you have an NPE if one occurs. So how do you know you are getting one? Please copy and paste the complete error message here so I can help you further.

    Layne
    L One
    Greenhorn

    Joined: Oct 09, 2005
    Posts: 6
    Oh I got it working now, turns out I made mistake in one of my other classes.

    Thank you for your help.
     
    jQuery in Action, 2nd edition
     
    subject: Help with Linked Lists