Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Trouble with subclassing??

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 --------------------------------
//-------------------------------------------------------
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic