views:

44

answers:

1

In my code, I have a nestted class that holds an object and an integer. This is to mark the beginning and end locations for a sublist method.

The code executes to the point that the object and integer have established values. I've verified this in the debugger.

The next line then calls ListLoc<E> startNode= new ListLoc<E>(start, startElement); and that is what trips the exception.

The class is already defined as

 private class ListLoc<E>{
    public Chunk<E> node;
    public int index;

    /* This object is created to hold a chunk and index location.  Two
     * objects will be created for the start and end locations for 
     * creating a sublist
     */
    public ListLoc(Chunk<E> node, int index){
        this.node= node;
        this.index= index;
    }
}

The strange thing is this portion of the code was executing fine before I replaced the local startNode with a global variable. That idea didn't work out, so I changed the variable back, and this exception cropped up.

I have made no changes to the class path or executions. The only time this crops up is in the debugger

EDIT: added stack trace

ClassNotFoundException(Throwable).(String, Throwable) line: 217
ClassNotFoundException(Exception).(String, Throwable) line: not available ClassNotFoundException.(String) line: not available
URLClassLoader$1.run() line: not available
AccessController.doPrivileged(PrivilegedExceptionAction, AccessControlContext) line: not available [native method]
Launcher$ExtClassLoader(URLClassLoader).findClass(String) line: not available
Launcher$ExtClassLoader.findClass(String) line: not available
Launcher$ExtClassLoader(ClassLoader).loadClass(String, boolean) line: not available Launcher$AppClassLoader(ClassLoader).loadClass(String, boolean) line: not available Launcher$AppClassLoader.loadClass(String, boolean) line: not available
Launcher$AppClassLoader(ClassLoader).loadClass(String) line: not available

From what it looks to me, its the classLoader that triggers the exception.

+2  A: 

It would help if you could clarify what the exception is, however, I am guessing that you are getting a ClassNotFound for E.

It looks like your professor has not clarified the concept of Generics yet. I would recommend looking over what a generic data structure is. THe advice I would give is to look for why E is not defined and why you are trying to create a list of them.

I think that the following link might help to clarify the syntax and the concepts behind them: http://download.oracle.com/javase/tutorial/extra/generics/index.html

Particularly the following section: http://download.oracle.com/javase/tutorial/extra/generics/simple.html

bogertron
As a co-worker would say, Java doesn't have "real generics" the way .NET does. So you can't say "new E()" or anything like that.
StriplingWarrior
Added the stack trace. Thing is, I'm not trying to make a list of E, it is just a start and end pointer. The object will be sent to a loop that goes through the list and copies the values between the start and end points to the new list.
Jason
@Jason: You are correct in that the ClassLoader is triggering the exception. However, that is intentional behavior. In your example, you are creating a ListLoc object which works with a generic type (E).
bogertron
Alright, I'll see if i can clarify this in 550 characters. When you declare [private class ListLoc<E>] you are telling the jvm that this class can replace the element (E) with another Object derivative (say Integer or String). When you actually declare an instance of ListLoc, you need to specify what Object type you plan to use for the element. So when the object is created, it will know to check for the specified object type. If anything is unclear please ask add comments
bogertron
ok, so what would the solution be? change `ListLoc<E> to ListLoc(Chunk<?> node, int index)`?
Jason
I don't know, what datatype is Chunk going to hold?
bogertron
The Chunk is a doubly linked node that contains an array of N size, to hold any item. Therefore, the generic typing is a requirement.
Jason
So the definition of the Chunk is correct. However, when you use it for your implementation, what type is it going to hold? Chunk and ListLoc are generically defined, but you need to state what object type they will hold when the runtime executes the line of code.
bogertron
ok, I think I get what you're saying. The ListLoc will hold a chunk node and an integer. However, the contents of the node can vary from primitive types to whatever object is needed at the moment. So I need to find some way to tell the ListLoc that a Chunk is going to be passed when it is instanciated.
Jason
That is absolutely correct. The one problem is that you cannot use primitive types when declaring generics. You have to use their object counterparts, ie java.lang.Integer for int, java.lang.Double for double.
bogertron
I've tried a few different variations ranging from `ListLoc<Chunk, Integer>` to `ListLoc<?,?>` and the same error is still being thrown. The strange thing is I have this portion of the code structured about 75% similar to another student and she has her class as `ListLoc<E>` and no exceptions are thrown.
Jason
When you say that 'she has her class as ListLoc<E>' is that in the declaration of the class, as in private class ListLoc<E>? That is correct. However, when you create an instance, you need to specify the type. With the variations you have provided, you have two generic types, for example 'public class ListLoc<K, V>.' Your class definition was correct in the sample code provided. However, your creation of an instance of it was incorrect. The following should be the only thing you need to change: ListLoc<E> startNode= new ListLoc<E>(start, startElement);. Think of the above comments and apply
bogertron