Srihari
Srihari

Reputation: 766

get objects back from collection

I am trying to modify a specific object which is returned as an Object collection from a method. Now lots of other objects are also added to this collection. Hence finding the specific object gets difficult. I tried to solve it using "instanceof", but am getting the following exception:

java.lang.ClassCastException: com.sheesoft.MockBean cannot be cast to com.sheesoft.TestBean
at com.sheesoft.Main.main(Main.java:30)

I have been able to reproduce the same exception with some test classes. Please let me know where I am going wrong.

Main:

package com.sheesoft;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Main {

/**
 * @param args
 */
public static void main(String[] args) {
    Collection<Object> beanCollection = new ArrayList<Object>();
    TestBean tb1 = new TestBean();
    MockBean mb1 = new MockBean();

    beanCollection.add(tb1);
    beanCollection.add(mb1);

    try{
    Iterator<Object> itr = beanCollection.iterator();
    while(itr.hasNext()){
        Object object = itr.next();
        if(object instanceof TestBean){
            TestBean tb2 = (TestBean) itr.next();
            tb2 = modifyTestBody(tb2);
            System.out.println(tb2.getBody());
        }
        if(object instanceof MockBean){
            MockBean mb2 = (MockBean) itr.next();
            System.out.println(mb2.getMockPayload());
        }
    }
    }catch(Exception e){
        e.printStackTrace();
    }
}

    private static TestBean modifyTestBody(TestBean tb) {
    tb.setBody("NewBody");
            return tb;
    }

}

Bean1:

package com.sheesoft;

public class TestBean {
private String header;
private String body;

public TestBean(){
    setHeader("header");
    setBody("body");
}

public String getHeader(){
    return header;
}
public String getBody(){
    return body;
}
public void setHeader(String header){
    this.header=header;
}
public void setBody(String body){
    this.body = body;
}
public String toString(){
    StringBuilder sb = new StringBuilder();
    sb.append("header = "+header+"\n");
    sb.append("body = "+body+"\n");
    String returns = sb.toString();
    return returns;
}
}

Bean 2:

package com.sheesoft;

public class MockBean {
private String mockHeader;
private String mockBody;
private String mockPayload; 

public MockBean(){
    setMockHeader("mockHeader");
    setMockBody("mockBody");
    setMockPayload("mockPayload");
}



public String getMockHeader(){
    return mockHeader;
}
public String getMockBody(){
    return mockBody;
}
public String getMockPayload(){
    return mockPayload;
}
public void setMockHeader(String header){
    this.mockHeader=header;
}
public void setMockBody(String body){
    this.mockBody = body;
}
public void setMockPayload(String payload) {
    this.mockPayload = payload;
}

public String toString(){
    StringBuilder sb = new StringBuilder();
    sb.append("mockHeader = "+mockHeader+"\n");
    sb.append("mockBody = "+mockBody+"\n");
    sb.append("mockPayload"+mockPayload+"\n");
    String returns = sb.toString();
    return returns;
}
}

Upvotes: 0

Views: 169

Answers (3)

Vishal K
Vishal K

Reputation: 13066

In your code:

System.out.println(itr.next().toString());
        if(itr.next() instanceof TestBean){
            TestBean tb2 = (TestBean) itr.next();
                            tb2=modifyTestBody(tb2);
            System.out.println(tb2.getBody());

You have already called itr.next() three times . So by the time u wrote third itr.next() You already have jumped to third element . So in each loop you are iterating to three elements instead.So your Iterator is exhausted prematurely and throwing java.util.NoSuchElementException

EDIT Use this code instead:

while(itr.hasNext()){
        Object obj = itr.next();
        System.out.println(obj.toString());
        if(obj instanceof TestBean){
            TestBean tb2 = (TestBean) obj;
                            tb2=modifyTestBody(tb2);
            System.out.println(tb2.getBody());
        }
        if(obj instanceof MockBean){
            MockBean mb2 = (MockBean) obj;
            System.out.println(mb2.getMockPayload());
        }
    }
    }catch(Exception e){}

Upvotes: 1

zibi
zibi

Reputation: 3313

In the code you have

    System.out.println(itr.next().toString());
    if(itr.next() instanceof TestBean){
        TestBean tb2 = (TestBean) itr.next();
....

the problem is that every time you call itr.next() you get the next object from a collection, so in that case you don't take one, but several objects.

You should change this code to:

    Object object = itr.next();
    System.out.println(object.toString());
    if(object instanceof TestBean){
        TestBean tb2 = (TestBean) object;
....

Upvotes: 0

Louis Wasserman
Louis Wasserman

Reputation: 198014

In

    System.out.println(itr.next().toString());
    if(itr.next() instanceof TestBean){
        TestBean tb2 = (TestBean) itr.next();
                        tb2=modifyTestBody(tb2);
        System.out.println(tb2.getBody());
    }
    if(itr.next() instanceof MockBean){
        MockBean mb2 = (MockBean) itr.next();
        System.out.println(mb2.getMockPayload());
    }

you'll call itr.next() three separate times. That moves through three different elements, when you've only checked if there's one element available. Instead, store itr.next() into a variable and do all the operations on that variable.

Upvotes: 1

Related Questions