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 7828553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:23:48+00:00 2026-06-02T10:23:48+00:00

Using Java, I have a method which I’m calling with method.invoke that returns a

  • 0

Using Java, I have a method which I’m calling with method.invoke that returns a string.
Method.invoke returns an object though which can’t be cast to String.
How am I supposed to use the Object as a string??

In the docs on reflection it shows the following:

Object o = m.invoke(t, new Locale(args[1], args[2], args[3]));
out.format("%s() returned %b%n", mname, (Boolean) o);

But my code does this and I get an exception: java.lang.Class cannot be cast to java.lang.String

This is an abstract class – there will then be several implementations of beans.

            Method[] methods = this.getClass().getMethods();
    for (Method method : methods) {
        if (isMethodGetter(method)) {
            
            try {

                Object message = method.invoke(this); // expect a string    
                       Object message = method.invoke(this); // expect a string
                
                if (message == null) {
                    // no messeage
                } else {
                    logger.debug("Calling listAnswers: got an answer: "
                            + message);

                    // create an answer object from the reflected
                    Answer answer = new Answer();
                    answer.setText((String)message);//cast as string
                    answerList.add(answer);
        }

edit: as per below answers I had a problem with the validation.

  • 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-02T10:23:49+00:00Added an answer on June 2, 2026 at 10:23 am

    It’s simple: you didn’t get an instance of java.lang.String, but of java.lang.Class. Your clarification in the comment makes it all crystal-clear: the method java.lang.Object.getClass passes your isMethodGetter test, but it’s not want you wanted. Just enhance that checking code to not let the exact name getClass pass.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java method which takes a String as parameter and returns a
I am using java language,I have a method that is supposed to return an
I'm using Java. I want to have a setter method of one class that
I have a java class ( Test ) with a public method which returns
I have a property text=000.01 but using java.util.Properties 's method getProperty("text"); returns "0.01" (must
I just discovered that when calling Java from Matlab object.method(arg1,...,argn) is equivalent to method(object,
I am using java with spring3 i have following controller's service method @Override public
I am using java Runtime.exec() method to execute bat file,in bat file i have
I want to have a java app running, using a function/method (with as little
I am using java and have to deal with numbers larger than long (which

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.