L One

Greenhorn
+ Follow
since Oct 09, 2005
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by L One

Oh I got it working now, turns out I made mistake in one of my other classes.

Thank you for your help.
18 years ago
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).
18 years ago
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?
18 years ago
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?
18 years ago
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.
18 years ago
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.
18 years ago