• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problems with ArrayList

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Derrick Ritchie
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic