File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes ClassCastException - ArrayList binarySearch 1.4 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ClassCastException - ArrayList binarySearch 1.4" Watch "ClassCastException - ArrayList binarySearch 1.4" New topic

ClassCastException - ArrayList binarySearch 1.4

David Wooddall-Gainey

Joined: Dec 13, 2007
Posts: 16

Below please find my problem!
I have been diligently working through Kathy and Bert's Cert book, 1.5, after diligently working through 1.4!

Current environment is 1.4; IDE is Eclipse with WOLips, a WebObjects (Apple) based environment.

When the code below runs, it blows at the search, the stackTrace points to the compareTo method in the SMU Class: ClassCastException on the line I starred.

The compareTo method resides in SMU. The ArrayList contains LocationSoilSeries objects. LocationSoilSeries extends SMU. Both classes listed below.

Clues? Please?


David WG

1 Another method calls GetData after Validate has been instantiated.
2 The Sort seems to work. I have other test code to print the list before and after the sort.
3 Originally I was using Comparators. I would like to sort and search by several properties. But the problem first appeared then, so I simplified.

public class Validate {
protected BufferedReader in;
protected File smuFile;
protected ArrayList LSS;

public Validate(String f) {
smuFile = new File(f);

public boolean GetData() {
boolean haveError = false;

try {
String line;
String unitName = "";
String smu = "";
String soilSeriesName = "";
double percentage = 0;

int lineCount = 0;
int tokenCount = 0;

LSS = new ArrayList();
LocationSoilSeries lss;

in = new BufferedReader(new FileReader(smuFile));
while ((line = in.readLine()) != null) {
tokenCount = 0;
line = line.trim();
StringTokenizer t = new StringTokenizer(line, ",");
while (t.hasMoreTokens()) {
String aToken = t.nextToken();
switch (tokenCount) {
case 1:
unitName = aToken;
case 2:
smu = aToken;
case 3:
soilSeriesName = aToken;
case 4:
percentage = Double.parseDouble(aToken);
} // switch
} // while has more tokens
lss = new LocationSoilSeries(lineCount,unitName,smu,soilSeriesName,percentage);
} // while more to read
} // try
catch (IOException e) {
} // catch

System.out.println("Sort by SOIL Map Unit");
int s = 0;

// blows in the search - get ClassCastException - referring to
// the compareTo in SMU

s = Collections.binarySearch(LSS, "12C");
if (s > 0 ){
LocationSoilSeries lss = (LocationSoilSeries)LSS.get(s);
System.out.println("LSS " + s + ";" + lss.getLineNumber()+ ";" + lss.getSoilMapUnit()+ ";")
else {
System.out.println("No cigar");
return true;
} // getdata

} // Validate

public class LocationSoilSeries extends SMU {

protected String soilSeriesName;
protected double percentage;

public LocationSoilSeries (int lN, String name, String smu, String ssName, double percent) {
this.soilSeriesName = ssName;
this.percentage = percent;

public double getPercentage() {
return percentage;

public void setPercentage(double percentage) {
this.percentage = percentage;

public String getSoilSeriesName() {
return soilSeriesName;

public void setSoilSeriesName(String soilSeriesName) {
this.soilSeriesName = soilSeriesName;

public class SMU implements Comparable{

protected int lineNumber;
protected String unitName;
protected String soilMapUnit;

public SMU (int lN, String n, String soilmapunit){
this.lineNumber = lN;
this.unitName = n;
this.soilMapUnit = soilmapunit;

public int compareTo(Object o) {
SMU smu = (SMU)o; // **************************************
return soilMapUnit.compareTo(smu.getSoilMapUnit());

public String getSoilMapUnit() {
return soilMapUnit;

public void setSoilMapUnit(String soilMapUnit) {
this.soilMapUnit = soilMapUnit;

public String getUnitName() {
return unitName;

public void setUnitName(String unitName) {
this.unitName = unitName;

public int getLineNumber() {
return lineNumber;

public void setLineNumber(int lineNumber) {
this.lineNumber = lineNumber;
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46378
Please use code tags round your quoted code; they make it easier to read.
Please tell us which line the class cast exception occurs on. You should be able to work that out by reading the stack trace which prints out on screen.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

This call is going to try to look for the String "12C" in the list, by calling the "compareTo()" method of the items in the list.

// blows in the search - get ClassCastException - referring to
// the compareTo in SMU

s = Collections.binarySearch(LSS, "12C");

And your compareTo() method does this:

public int compareTo(Object o) {
SMU smu = (SMU)o; // **************************************

You're casting the argument to an SMU, without checking if, indeed, it is one. But binarySearch() is going to be passing a String -- and hence the exception.

You would need to do something like

s = Collections.binarySearch(LSS, new SMU(...,"12C",...));

Alternatively, your compareTo() could know how to compare SMUs to Strings, but honestly, that's really ugly, and a really bad practice.

[Jess in Action][AskingGoodQuestions]
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Maybe the search key in not mutually comparable with the elements of the LSS list.

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
David Wooddall-Gainey

Joined: Dec 13, 2007
Posts: 16
Thanks all. Yes, the problem was my misunderstanding that if the compareTo was comparing strings, I could search on a string. But the ArrayList is full of objects!

I'm adding some code below just to figure out how to format it for a post. Sorry about the mess yesterday. I actually formatted it quite nicely before posting.

No replies needed unless the code isn't formatted!

I agree. Here's the link:
subject: ClassCastException - ArrayList binarySearch 1.4
It's not a secret anymore!