cancel
Showing results for 
Search instead for 
Did you mean: 

ClassCastException on CommandBean

Former Member
0 Kudos

Hello all,

I have Entity and Stateless Session Beans working properly once tested via web services. And I created a command bean to call the methods.

The SessionBean returns a wrapper class array.

ClassCastException's here: temp_record = (BusinessUnitLocal) itr.next(); // CLASS CAST EXCEPTION

What are the possible issues that I need to look into to fix this?

Please help. Thank you.

Here's my commandbean declaration:

private BusinessUnitSessionLocal localBUnit = null;
private BusinessUnitSessionLocalHome localHomeBUnit = null;

try{
localHomeBUnit = (BusinessUnitSessionLocalHome) icontext.lookup(
	"localejbs/BusinessUnitSessionBean");
localBUnit = localHomeBUnit.create();


}catch (NamingException e) {
//		   TODO Auto-generated catch block
			e.printStackTrace();
		} catch (CreateException e) {
			//		   TODO Auto-generated catch block
			e.printStackTrace();
		}


//method call

public void findAllBusinessUnit() {
		Collection records = new ArrayList();
		BusinessUnitWrapper record = null;
		BusinessUnitLocal temp_record = null;

		records = new ArrayList(Arrays.asList(localBUnit.findAll()));
		Iterator itr = records.iterator();

		while (itr.hasNext()) {
			
			*temp_record = (BusinessUnitLocal) itr.next(); // CLASS CAST EXCEPTION*
			record = new BusinessUnitWrapper();
			record.setBusinessUnitID(temp_record.getBusinessUnitID());
			record.setBusinessUnitName(temp_record.getBusinessUnitName());
			record.setBusinessUnitStatus(temp_record.getBusinessUnitStatus());
			outputBUnit.add(record);
		}
}

Accepted Solutions (1)

Accepted Solutions (1)

siarhei_pisarenka3
Active Contributor
0 Kudos

Hi JMJ

Check implementation of BusinessUnitSession bean, method findAll(). What does the method actually return? It should be array of entity beans.

BR, Sergei

Former Member
0 Kudos

Hello Sergei,

Thank you for the prompt response.

Here's my code for the findAll method:

public BusinessUnitWrapper[] findAll() {
		ArrayList buList = new ArrayList();
		Collection val = null;
		BusinessUnitWrapper buWrapper = new BusinessUnitWrapper();

		try {
			val = localHome.findAll();

			for (Iterator iterator = val.iterator(); iterator.hasNext();) {
				BusinessUnitLocal data = (BusinessUnitLocal) iterator.next();
				buWrapper = convertToBUWrapper(data);
				buList.add(buWrapper);
			}

			//TicketWrapper[] result = new TicketWrapper[tickets.size()];
			//tickets.toArray(result);

			BusinessUnitWrapper[] result =
				new BusinessUnitWrapper[buList.size()];
			buList.toArray(result);

			return result;

		} catch (FinderException ex) {
			ex.printStackTrace();
		}
		return null;
	}

siarhei_pisarenka3
Active Contributor
0 Kudos

Hi JMJ

findAll() returns array of BusinessUnitWrapper, and you are iterating on the array. But you are trying to cast the BusinessUnitWrapper to BusinessUnitLocal. That's the reason of the error.

Cast the iterator to BusinessUnitWrapper, not to BusinessUnitLocal.

BR, Sergei

Former Member
0 Kudos

Okay, that was embarassing. haha. Quite possibly the most embarassing post to date here.

I can't understand why I didn't realize that I was actually casting it to an entity local interface.

I got to solve the problem before I got to your reply.

Thank you Sergei.

Answers (0)