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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:23:20+00:00 2026-05-29T10:23:20+00:00

Previously i wrote an app that used reflection to serialize data from json to

  • 0

Previously i wrote an app that used reflection to serialize data from json to objects and that solution worked fine. Now i am working on V2.0 of this app and i am now trying to isolate all the model logic in a service and make the activity layer separate from that. The purpose of this being that the service can live in the background and run periodical updates against the web service.
My problem is that as soon as i try to use reflection to instantiate an object that implements the parcelable interface it throws an InstantiationException. My guess is that the parcelable interface in some way interferes whit the default constructor of the object.

One solution is to make an intermediary object that i build and then send that object to the constructor of the correct parcelable object and send that object in the IPC. But i feel that there might be a simpler solution involving the use of parcelable in some way that i have not thought of yet.


public class ManagedObjectWrapper {

    private Object anObject;//the instance of the object itself
    private Class<?> theClass;//Metadata
    private Field[] declaredFields;//Metadata

    public ManagedObjectWrapper(Class<?> c){

        theClass=c;
        declaredFields = c.getDeclaredFields();
        try {
                           //this is where it crashes
            anObject =  c.newInstance();
        } catch (InstantiationException e) {

            e.printStackTrace();
        } catch (IllegalAccessException e){ 
            e.printStackTrace();
        }
    }

    public Object GetObject(){return this.anObject;}
    public Class<?> GetClass(){return this.theClass;}
    public Field[] GetDeclaredFields(){return this.declaredFields;}

}

This is how i use reflection to get my objects from the json

private ArrayList<ManagedObjectWrapper> getObjectsFromJSON(String pluralizedColumn,JSONArray array) throws Exception
    {
        ArrayList<ManagedObjectWrapper> returnList = new ArrayList<ManagedObjectWrapper>();
        String packageName = extContext.getPackageName();
        String singularObjectName = pluralizedColumn.substring(0, pluralizedColumn.length()-1);//remove the plural s 
        String canonicalClassName = packageName.concat(".").concat(singularObjectName);
        Class<?> theClass = Class.forName(canonicalClassName);

        for(int i = 0;i < array.length(); i++){

            ManagedObjectWrapper mow = new ManagedObjectWrapper(theClass);

            JSONObject obj = array.getJSONObject(i);
                for(Field field : mow.GetDeclaredFields()){
                        Class<?>[] params = {field.getType()
                            };
                    if(!field.getName().contentEquals("CREATOR")){
                        Method setterMethod = mow.GetClass().getDeclaredMethod(SET_METHOD_PREFIX.concat(field.getName()),params);
                        Object nullTest = obj.get(field.getName());

                    if(nullTest.equals(null)){

                        }else{
                            setterMethod.invoke(mow.GetObject(), obj.get(field.getName()));
                        }
                    }

                }

        returnList.add(mow);
    }

  • 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-29T10:23:21+00:00Added an answer on May 29, 2026 at 10:23 am

    Yes it can. Just implement a default constructor on the class , other more deeply hidden errors in the code made me think it was not that simple. But circling back to this issue made me try it again and voila now it works just like before

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

Sidebar

Related Questions

How to write an ant task that removes files from a previously compiled JAR?
I have a web app consisting of some JSPs that were previously running on
I am porting an app that I originally wrote using the accelerometer for IOS
So here's the scoop: I wrote a tiny C# app a while back that
I have an app that I am now developing multiplayer for. However I do
In a previous project, I built an iPhone app for 2.2.x that used SQLite.
I'm working on refining a database that I wrote for my app while ago.
I'm trying to create an Android client for a client/server app that has previously
I wrote a nice little app that gets Yahoo weather info and posts it
Previously I have asked to strip text from a field and convert it to

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.