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 Trouble with subclassing?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trouble with subclassing??" Watch "Trouble with subclassing??" New topic
Author

Trouble with subclassing??

Akane Tanaban
Greenhorn

Joined: May 17, 2004
Posts: 4
Hello,

I am doing a homework project, and we are doing subclassing for the first time. I'm supposed to create a program using a linked list to store strings in alphabetical order. Our teacher gave use the base class (called ListNode) to use to create a subclass, NameList.

The problem is that it seems to compile all right, but when I run it, it gives me the following message:

java.lang.NoClassDefFoundError: Empty
Exception in thread "main"

Is this a subclassing problem? Is it having trouble finding the base class? I'm wondering if someone could point me in the right direction in solving this problem.

I know I still have some problems with my algorithms, but I can't even test them without being able to see the output.

I'd appreciate any help anyone can lend on this matter. Thanks!

Oh, here is what I have written so far, in case someone wants to try running it:

//------------------------------------------------------
// ListNode class --------------------------------------
public class ListNode
{
// no constructor needed since this is only initialization
ListNode next = null;

// ---- ListNode: InsertAfter method ----
// the calling node will "point to" new_node after this call
void InsertAfter( ListNode new_node)
{
new_node.next = next;
next = new_node;
}

// ---- ListNode: DeleteAfter method ----
// the node AFTER the calling node will be deleted
// return it to caller if needed.
ListNode DeleteAfter()
{
ListNode node_to_delete;

node_to_delete = next;
if (node_to_delete != null)
next = node_to_delete.next; // i.e., point around next node
return node_to_delete;
}

// ---- ListNode: DisplayNode method ----
void DisplayNode()
{ }

} // end of class ListNode -----------------------------
//------------------------------------------------------


import java.io.*;
import java.lang.*;

class NameNode extends ListNode
{
public String name;

// default constructor
public NameNode()
{
name = "default name";
}

// constructor that takes a string
public NameNode(String s)
{
if(s.length() > 0 && s.length() < 20)
name = s;
}

public void DisplayNode()
{
System.out.println("Nodes are: " + name + "/n");
}

/*
public static NameNode GetNext(NameNode nn)
{
NameNode next_nn = new NameNode();
nn.next = next_nn;
return next_nn;
}
*/

public NameNode GetNext(ListNode current)
{
NameNode next_name_node = new NameNode();
if (current != null)
{
current = current.next;
next_name_node.next = current;
}
return next_name_node;
}
}




//-------------------------------------------------------
// NameList class -----------------------------------------
public class NameList
{
private ListNode head, current;

// ---- MyList: constructor method ----
// create a "header" node which won't be used for data
// this makes all methods easier to write - no special cases
public NameList()
{
head = new ListNode();
current = null;
}

// Inserts the new node alphabetically into the list.
// It compensates for case.
// It does not store the passed objects, but rather a COPY of the passed objects.
public void Insert( String s )
{
NameNode new_node = new NameNode();
s = s.toLowerCase();
new_node.name = s;

NameNode name_node_p = new NameNode();
ListNode p;
p = name_node_p.next;



for (ResetIterator(); (p = Iterate())!= null; )
{
if( s.compareTo(name_node_p.name) <= 0 )
;
else if ( s.compareTo(name_node_p.name) > 0 )
{
p.InsertAfter(name_node_p.next);
}
}
AddToFront(name_node_p.next);

}

public void Delete(String s)
{
NameNode delete_node = new NameNode();
ListNode p;
p = delete_node.next;
for (ResetIterator(); (p = Iterate()) != null; )
{
if ( s.equals(delete_node.name))
p.DeleteAfter();
}


}
// ---- MyList: AddToFront (push) method ----
void AddToFront( ListNode new_node)
{
head.InsertAfter(new_node);
}

// ---- MyList: RemoveFromFront (pop) method ----
ListNode RemoveFromFront()
{
ListNode removed_node;

removed_node = head.DeleteAfter();
return removed_node;
}

// ---- NameList: IsEmpty method ----
boolean IsEmpty()
{
return (head.next == null);
}

// ---- NameList: DisplayList method ----
void DisplayList()
{
ListNode p;
for (ResetIterator(); (p = Iterate()) != null; )
p.DisplayNode();
}

// ---- NameList: ResetIterator method ----
void ResetIterator()
{
current = head;
}

// ---- NameList: Iterate method ----
ListNode Iterate()
{
if (current != null)
current = current.next;
return current;
}
public static void main(String[] args)
{
NameList list1, list2;

list1 = new NameList();
list2 = new NameList();




list1.Insert("Bob");
list1.Insert("carol");
list1.Insert("Ted");
list1.Insert("alice");
list1.Insert("michael");
list1.Insert("birkoff");
list1.Insert("MADELINE");

System.out.println("\n-----");
list1.DisplayList();

list1.Delete("irving");
list1.Delete("michael");

System.out.println("\n-----");
list1.DisplayList();

System.out.println("\n-----");
list2.DisplayList();

list2.Insert("alice");
list2.Insert("michael");

System.out.println("\n-----");
list2.DisplayList();

}


}
// end of class NameList --------------------------------
//-------------------------------------------------------
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
I saved those as three separate files in a directory, compiled them, and ran them. I get a NullPointerException instead. Did you put them into separate files? I'm using JDK 1.5.0_01-b08, though it shouldn't matter.
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
Are all your files in the same directory? If so, are you typing "java NameList" on the command line in that directory? Here's what I typed:



Because name_node_p.next gets passed to AddToFront and causes a NullPointerException (meaning it's null), I noticed that the following line might be causing the problem I saw:



I'm wondering if you meant that to be the other way around...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trouble with subclassing??
 
Similar Threads
cannot insert into linkedlist after delete
Implementing Comparable, crafting compareTo with Set class
Queue in Java
sorting linked list using merge sort
How to insert an object to a linkedlist ADT?