Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9209793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:55:45+00:00 2026-06-18T00:55:45+00:00

I am trying to modify a specific object which is returned as an Object

  • 0

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;
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:55:46+00:00Added an answer on June 18, 2026 at 12:55 am

    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){}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to modify the System.Drawing.Printing.PrinterSettings object that I get from the System.Windows.Forms.PrintDialog
I am trying to modify an Integer field on existing table from nullable to
I am trying to modify the code below to check the input from a
I'm trying to modify the following query to find the rank of a specific
I am trying to secure an application to only run from a specific USB
I am trying to use the php DateTime objects modify function to modify he
I am trying to modify the CSS of a specific div within my CSS
I'm trying to modify some code that returns results from the database. Currently the
I am trying to acces a specific bit and modify it. I have moved
I'm trying to read some text from an html file, modify it in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.