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 Problems with ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problems with ArrayList" Watch "Problems with ArrayList" New topic
Author

Problems with ArrayList

Derrick Ritchie
Greenhorn

Joined: Jan 27, 2006
Posts: 6
Hi there

I think this is a beginners problem, if its not I apologize in advance. I`ve just created an array list using a class Point to add and get entries from the list.
The class for getting an object from the list looks like this:

public Object getDest(int Place)
{
if(DestinationList.isEmpty())
{
return null;
}

System.out.println("In class");
return DestinationList.get(Place);
}

This should return the Object to the calling code.

However when I call it in the main program using the line

Position NewPosition = (Position)DestinationList.getDest(Placement)

it comes up with a class not defined error. I assume this is because it cannot match the cast I`m using with the object it is bringing in.
However I have been through the code and the values being read in match the Position class that its being cast into.

Does anyone have any suggestions why the compiler is causing me the error?
If you want me to reprint the whole code I dont mind, I just did not want to clog up the whole thread with it. And if I`ve not explained it well enough then I`m also sorry, first time at trying this!

I appreciate the help anyone can provide, thanks in advance!
[ January 27, 2006: Message edited by: Derrick Ritchie ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Please post the full error message exactly as you see it. Paraphrasing loses much of the information that we need to help you to fix a compiler error. Also, it usually helps if you post a few lines of code that can give the context of the line that causes the error. In particular, you should post any declarations of variables that are used in this line of code.

Layne


Java API Documentation
The Java Tutorial
Derrick Ritchie
Greenhorn

Joined: Jan 27, 2006
Posts: 6
Hi there, thanks for the advice. Ok, here the error message in full, it occurs at runtime rather than compiler stage.

exception in thread main java.long.ClassCastException
at DestinationProg.ProcessOption<DestinationProg.java:52>
at DestinationProg.main<DestinationProg.java>

I`ll try to explain it a bit better now, Ive created a class called Positions which can read in data of the format int int char,int int char.

So I`ve created a class called PointList with an interface called Points which sets up an ArrayList like this

private List DestinationList;

public PointList()
{
DestinationList = new ArrayList();
}

the main program can set up the list no problem with a global variable

private static Points DestinationList;

and initilizer DestinationList = new PointList();
in the main program

It can populate and retrieve the list with no problem however when it comes to getting the first element of the list I get the above error.

The actual code to retrieve the list object is this:

Placement = 0;
Positions ThisPos = (Positions)DestinationList.getDest(Placement);
System.out.println(ThisPos);
Placement++;

The actual code in the class to retrieve an object is this:

public Object getDest(int Place)
{
if(DestinationList.isEmpty())
{
return null;
}
return DestinationList.get(Place);
}
Where Place is the argument passed by Placement in the main code.

It seems to have a problem casting the object being brought in to the Positions class even though the format of the object being passed back from the list is in the correct format to be accepted as a position.

Does anyone have any ideas why this may be happening?
[ January 28, 2006: Message edited by: Derrick Ritchie ]
Derrick Ritchie
Greenhorn

Joined: Jan 27, 2006
Posts: 6
Just a quick note to say that I`ve solved the problem, when I was putting in the objects in the list I had not set it as a position object which meant when I retrieved it it could not regognize it as a certain object.

Thanks for all who looked though!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems with ArrayList
 
Similar Threads
Problem with Database and Vectors
programming to the interface
Sun Cirtification
Parsing text
Why do values of collected HashMap have to be cast while values of parent Map don't? RESOLVED