Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: missing something so simple....help!

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default missing something so simple....help!

    i am trying to add nodes to a list (similiar to a linkedlist) but i keep getting errors at my add method and where i call the add method in my main.

    public class LLList<T> {
     
     
    		private Node firstNode;	// reference to first node
    		private int length;		// number of entries in list
     
    		public LLList(){
    			clear();
    		}
     
    		public final void clear() {
    			firstNode = null;
    			length = 0;
    		}
     
    		public boolean add(T newEntry) {
    			Node newNode = new Node(newEntry);//getting the error here
    			if (isEmpty()){
    				firstNode = newNode;
    			}else{
    				Node lastNode = getNodeAt(length);
    				lastNode.next = newNode;	// make last node reference new node
    			}
    			length++;
    			return true;
    		}
     
    		public void display() {
    			Node currentNode = firstNode;
    			while(currentNode != null){
    				System.out.println(currentNode.data);
    				currentNode = currentNode.next;
    			}
    		}
     
    		public boolean isEmpty() {
    			boolean result;
    			if (length == 0){
    				assert firstNode == null : "length = 0 but firstNode is not null";
    				result = true;
    			}else{
    				assert firstNode != null : "length not 0 but firstNode is null";
    				result = false;
    			}
    			return result;
    		}
     
    		/**	Task: Returns a reference to the node at a given position.
    		 * 	Precondition: List is not empty; 1 <= givenPosition <= length.
    		 * @param givenPosition
    		 * @return
    		 */
    		private Node getNodeAt(int givenPosition){
    			assert (!isEmpty() && 1 <= givenPosition && givenPosition <= length);
    			Node currentNode = firstNode;
    			for (int counter = 1; counter < givenPosition; counter++){
    				currentNode = currentNode.next;
    			}
    			assert currentNode != null;
    			return currentNode;
    		}
     
    		private class Node //private inner class
    		{	
    			private T data;
    			private Node next;
     
    			private Node(T dataPortion){
    				data = dataPortion;
    				next = null;
    			}
     
    			private Node(T dataPortion, Node nextNode){
    				data = dataPortion;
    				next = nextNode;
    			}
    		}// end Node
     
     
     
    	public static void main(String[] args) {
    		LLList<String> list = new LLList<String>();
    		list.add("bobby");//getting the error here
    		list.add("joey");
    		list.display();
    	}
     
    }


    this is the error i see when i try to compile:

    Exception in thread "main" java.lang.ClassFormatError: Duplicate method name&signature in class file LLList$Node
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknow n Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at LLList.add(LLList.java:18)
    at LLList.main(LLList.java:84)

    your help would be appreciated. thank you!


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: missing something so simple....help!

    I've never seen this exception with this particular error message before. A workaround is the change the access modifiers to the Node constructors (public or protected), or remove the second constructor. There shouldn't be a need to mark the constructors of an inner class as private, but confess I can't explain this Exception
    Last edited by copeg; October 14th, 2011 at 08:39 PM.

  3. The Following User Says Thank You to copeg For This Useful Post:

    b094mph (October 17th, 2011)

  4. #3
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: missing something so simple....help!

    Hi b094mph,

    I just tested your code and it compiled and ran with no errors (via command line). Maybe this is an issue with your IDE or JDK version.

  5. The Following User Says Thank You to Tsarin For This Useful Post:

    b094mph (October 17th, 2011)

  6. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: missing something so simple....help!

    how do i find out which version of JDK i am using? i just tried my code in netbeans IDE and it works. so there is something wrong with my version of eclipse or the jdk.

  7. #5
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: missing something so simple....help!

    'javac -version' in the command line. For what its worth, I did receive the same exception when I ran your code...and like I said above, changing the access modifiers of the constructors solved the issue.

  8. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: missing something so simple....help!

    hmmm... curious. It should be accessible even if it's declared private, unless I'm misinterpreting the JLS:

    6.6.1 Determining Accessibility
    ...
    Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (7.6) that encloses the declaration of the member or constructor.
    The top level class should be the enclosing LLList class.

    What JDK compiler are you using (OS and version)?

    FYI, my results match Tsarin's (I'm using the 1.7.0 JDK developed by Sun Oracle for Windows x64).

    p.s. Changing the constructor access modifiers of Node to public is perfectly legal, too.
    Last edited by helloworld922; October 16th, 2011 at 07:55 PM.

  9. The Following User Says Thank You to helloworld922 For This Useful Post:

    b094mph (October 17th, 2011)

  10. #7
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: missing something so simple....help!

    I re installed eclipse and jdk 1.7. it works now! thanks for all your help guys. I appreciated it!

Similar Threads

  1. I'm either doing this entirely wrong or I'm missing something simple.
    By xionyus in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 14th, 2011, 08:56 AM
  2. Missing drivers?
    By mjpam in forum JDBC & Databases
    Replies: 5
    Last Post: September 6th, 2010, 08:00 PM
  3. Prime number generator program missing return statement
    By 03EVOAWD in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 10th, 2009, 09:17 AM
  4. Java error in password generator program
    By Lizard in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 16th, 2009, 07:49 PM
  5. [SOLVED] Java program to generate 10 random integers and then sum computed
    By Lizard in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 14th, 2009, 12:33 PM