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

  • Home
  • SEARCH
  • 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 4346100
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:07:43+00:00 2026-05-21T12:07:43+00:00

I am getting the following exception: java.lang.IllegalArgumentException: object is not an instance of the

  • 0

I am getting the following exception:

java.lang.IllegalArgumentException: object is not an instance of the class

and I have a pretty good idea of why I am getting it – but I am stuck with finding a solution.

Here is the sample code to explain what I want to do:

public class Car {
  private Owner owner;

  //Constructor and getter for owner field
}

public class Owner {
  private String name;

  //Constructor and getter for name field
}

Now what I am attempting to do is: Starting from the Car class, I retrieve the field named “owner” – then I retrieve the field “name” of that object owner which is part of a car. Now I want to read (and later modify, but I already fail with reading) the value of the field “name”. The problem: I only have an instance of car, but not of its field “owner” . For clarification purposes: The described process shall work generically – so I wont know the names of the getters, setters, fields and so on.

Here is the code that fails (please let me know if more code is required to clarify the problem):

Car car = new Car();
//Set owner and name property of owner for car
...
Field nameField = resolveDatapath(datapathToFeature, car);
nameField.setAccessible(true);
//Here I fail with the afore mentioned IllegalArgumentException, because 
//indeed, I am passing car - not its sub-object "owner" (because as I can see it,
//there is no way for me to generically retrieve the owner object - or is there?)
String value = nameField.get(car).toString();

The above stuff should also work for any “depths” (say I want a field and manipulate it from Object c which is a field of object b, which in turn is a field of object a …)

Please let me know, if I can further clarify the question – here is the code which retrieves the field (in the above example its the nameField):

    private Field resolveDatapath(String path, Object parent) {
    String subString = path;
    if (!subString.contains("."))
    {
        //We haven reached the end of the path
        return getField(subString, parent.getClass());
    }       

    //We haven't reached the end of the 
    subString = path.substring(0, path.indexOf("."));   
    Field field = getField(subString, parent.getClass());

    return resolveDatapath(path.substring(path.indexOf(".")+1), field);
}

private Field getField(String name, Class<?> parent) {
    Field [] fields = parent.getDeclaredFields();

    for(Field f : fields)
    {           
        String current = f.getName();
        if(current.equalsIgnoreCase(name))
        {
            try {
                return parent.getDeclaredField(current);
            } catch (SecurityException e) {
                fail("Not allowed to access field - " + current);
            } catch (NoSuchFieldException e) {
                fail("No such field exists - " + current);
            }
        }
    }
    return null;
}

Thank you very much in advance,
best regards,
Ready4Android

  • 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-05-21T12:07:43+00:00Added an answer on May 21, 2026 at 12:07 pm

    Okay thanks to Peter for giving my brain the poke it needed 😉 I found the solution and here it is:

    I create a DatapathObject which contains the field and the parent object as an object. Before I was compaining, that I cant get an instance of the Owner object which is a field in the Car object. Of course thats utter nonsense as I now figured – my brain was kinda slow here 😛 .

    This code works and makes my unittest pass:

        public void testDatapathResolution() throws Exception{
        String[] path = datapathToFeature.split("\\.");
        assertEquals("Owner", path[0]);
        assertEquals("Name", path[1]);
        DatapathObject result = resolveDatapath(datapathToFeature, car);
        result.getField().setAccessible(true);
        Object value = result.getField().get(result.getParent());
    
        assertEquals(car.getOwner().getName(), value.toString());
    }
    

    And here is the DatapathObject class:

    public class DatapathObject {
      private Object parent;
      private Field field;
    
      public DatapathObject(Object parent, Field field) {
        this.parent = parent;
        this.field = field;
      }
    
      public Object getParent() {
        return parent;
      }
    
      public Field getField() {
        return field;
      } 
    }
    

    My resolveDatapath method chanded as follows (to accomodate the introduction of the DatapathObject:

        private DatapathObject resolveDatapath(String path, Object parent) throws 
        IllegalArgumentException, IllegalAccessException
    {
        String subString = path;
        if (!subString.contains("."))
        {
            //We haven reached the end of the path
            Field field = getField(subString, parent.getClass());
            return new DatapathObject(parent, field);
        }       
    
        //We haven't reached the end of the 
        subString = path.substring(0, path.indexOf("."));   
        Field field = getField(subString, parent.getClass());
        field.setAccessible(true);
        return resolveDatapath(path.substring(path.indexOf(".")+1),  
                field.get(parent));
    }
    

    Thanks again to Peter – lol I still dont understand your answer and I dont think it was what I was looking for but it provided my brain with the missing spark to find the answer 🙂 !

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

Sidebar

Related Questions

I am getting an exception saying : java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required When
I am getting following exception: java.lang.UnsupportedClassVersionError: net/sourceforge/barbecue/BarcodeException : **Unsupported major.minor version 0.0** at java.lang.ClassLoader.defineClass1(Native
I am getting following Exception on Android 2.2.1: java.lang.NoSuchMethodError: java.lang.String.isEmpty I am calling text.isEmpty
I'm new to using Hibernate with Java. I'm getting the following exception. The stuff
hey guys, i'm getting an exception on the following inner exception: {Value cannot be
I am getting the following error when I put class files in subfolders of
I am getting he following Exception while running my Quartz Schdular program. Below is
hi i am getting the following exception while running my application and my applicationContext.xml
I am getting following exception when a Local Interface is implemented by two Stateless
I am getting the following exception from a test case that ran successfully before

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.