Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

class cast error

Status
Not open for further replies.

OhioSteve

MIS
Mar 12, 2002
1,352
US
I am writing a method that accepts three vectors. They each hold just one type of object. One holds type a, one holds type b, and one holds type c. Each type is a homemade class that is working properly. I have been extremely careful to keep track of which vector is which. In spite of ALL of these precautions I get the "classCastError" when I try to take an object out of the first vector and cast it as type a. The problem only occurs if there are elements in the second vector.

So the presence of records in the second vector makes casting fail in the FIRST vector. I know that it sounds unbelievable but it is true.

I have worked twelve hours on this one issue. Please advise.
 
It is unbelievable ! There will be a coding error somewhere :)

In any case, there is no need to maintain two or more vectors - a Vector will hold any class type.

Take a look at this code, and maybe rewrite yours to use something similar ?

Code:
public class Test  {
	class A {
		public char a = 'a';
	}
	class B {
		public char b = 'b';
	}

	public static void main(String args[]) throws Exception {
		new Test().test();

	}

	public void test() throws Exception {

		Vector v = new Vector();
		v.add(new A());
		v.add(new B());
		v.add(new A());
		v.add(new B());

		for (int i = 0; i < v.size(); i++) {
			Object o = v.elementAt(i);
			if (o instanceof A) {
				A aa = (A)o;
				System.out.println("Class is " +o.getClass().getName() +" " +aa.a);
			} else if (o instanceof B) {
				B bb = (B)o;
				System.out.println("Class is " +o.getClass().getName() +" " +bb.b);
			} else {
				System.out.println("Not sure what it is ! - " +o.getClass().getName());
			}
		}
	}
}

--------------------------------------------------
Free Database Connection Pooling Software
 
This issue had a humorous resolution :)

I had literally been rereading this code for more than twelve hours. I had become convinced that I had some exotic problem involving memory addresses. I had hypothesized that somehow, someway the compiler was switching the memory addresses of the vectors! Then when I tried to cast them, I did not know which vector contained which type of thing.

Five minutes ago, I found this near the start of the main function:

employeeVector=addFileDialogue(menuChoice,log,codeVector,stdin);

I WAS DOING IT TO MYSELF! That call should accept menuChoice, log, and employeeVector. I was smooshing the vectors together!
 
If possible, you may switch to java-1.5, giving you typesafe collections:
Code:
Vector <Code> codeVector = new Vector <Code> ();
// compiletime-error:
codeVector.add (new Employee ("James Gosling"));

If not possible, you may write a Wrapper, which isn't fun, if you need to implement nearly every functionality of Vector, but fine, if you only need two or three:
Code:
class CodeVector // NOT! extending Vector - 
{
      private Vector v = new Vector ();

      // add (employee) impossible:
      public void add (Code c) {v.add (c);}

      public Code remove () {return (Code) v.remove ();}
      //... probably more...
}
which prevents you from misusing 'v' for unwanted mixed data.
Drawback: If you try to use a method, which expects a Vector as argument, you may not pass the CodeVector.
Of course you could write a method, which retruns the Vector for that task:
Code:
      public Vector getTheVector () {return v;}
but then the recipient of the v may insert his employees, so this is inconsequent, and the 1.5 feature is a very useful extension of the typesafety of java.

I really enjoy it.

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top