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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:23:16+00:00 2026-06-14T20:23:16+00:00

I am trying to convert a java bean to hashmap and then later convert

  • 0

I am trying to convert a java bean to hashmap and then later convert the hashmap to java bean. For converting Java Object to hashmap this post helped me. Convert a JavaBean to key/value Map with nested name using commons-beans BeanUtils

Code below

    public class Ax {
    String axAttr;

    public String getAxAttr() {
        return axAttr;
    }

    public void setAxAttr(String axAttr) {
        this.axAttr = axAttr;
    }

    List<Bx> bxs;

    public List<Bx> getBxs() {
        return bxs;
    }

    public void setBxs(List<Bx> bxs) {
        this.bxs = bxs;
    }
}

public class Bx {
    String bxAttr;

    public String getBxAttr() {
        return bxAttr;
    }

    public void setBxAttr(String bxAttr) {
        this.bxAttr = bxAttr;
    }

    List<Cx> cxs = new ArrayList<Cx>();

    public List<Cx> getCxs() {
        return cxs;
    }

    public void setCxs(List<Cx> cxs) {
        this.cxs = cxs;
    }
}

public class Cx {
    String cxAttr;

    public String getCxAttr() {
        return cxAttr;
    }

    public void setCxAttr(String cxAttr) {
        this.cxAttr = cxAttr;
    }

    List<String> items;

    public List<String> getItems() {
        return items;
    }

    public void setItems(List<String> items) {
        this.items = items;
    }
}

below is the key value-pairs stored in the HashMap

axAttr –> axString
bxs[0].bxAttr –> bxString
bxs[0].cxs[0].cxAttr –> cxString
bxs[0].cxs[0].items[0] –> One
bxs[0].cxs[0].items[1] –> Two
bxs[0].cxs[0].items[2] –> Three

I stored these key values in DB and later retrieve them and want to convert to Java Bean again. But for converting the same HashMap to Java object with the help of propertyUtilsbean I am getting NullPointerException.

This is how I executed:

public static void main(String[] args) throws Exception {
        Ax ax = new Ax();
        ax.setAxAttr("axString");

        Bx bx = new Bx();
        bx.setBxAttr("bxString");

        Cx cx = new Cx();
        cx.setCxAttr("cxString");

        List<Bx> bxs = new ArrayList<Bx>();
        ax.setBxs(bxs);
        ax.getBxs().add(bx);

        List<Cx> cxs = new ArrayList<Cx>();
        bx.setCxs(cxs);
        bx.getCxs().add(cx);


        List<String> xs = new ArrayList<String>();
        cx.setAxs(xs);

        cx.getAxs().add(new String("One"));
        cx.getAxs().add(new String("Two"));
        cx.getAxs().add(new String("Three"));


        MyPropertyUtils myPropertyUtils = new MyPropertyUtils();

        Map map = new HashMap();
        for (String name :  myPropertyUtils.listNestedPropertyName(ax)) {
            map.put(name, PropertyUtils.getNestedProperty(ax, name));
        }

        Ax axNew = new Ax();

        Set<Entry> set = map.entrySet();


        for (Entry entry :set) {
            BeanUtils.setProperty(axNew, entry.getKey().toString(), entry.getValue().toString());
        }



    }

Exception

Exception in thread "main" java.lang.NullPointerException
    at org.apache.commons.beanutils.PropertyUtilsBean.getIndexedProperty(PropertyUtilsBean.java:507)
    at org.apache.commons.beanutils.PropertyUtilsBean.getIndexedProperty(PropertyUtilsBean.java:410)
    at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:768)
    at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:846)
    at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:903)
    at org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:456)
    at com.wavecrest.aspect.Test1.main(Test1.java:57)

Any suggestions are accepted:

  • 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-14T20:23:16+00:00Added an answer on June 14, 2026 at 8:23 pm

    BeanUtils don’t create objects instances for you inside axNew instance. So, calling

    BeanUtils.setProperty(axNew, "bxs[0].cxs[0].cxAttr", "Some value"); 
    

    Means “Get first element of ‘bxs’ collection, then get first element of it and then update the ‘cxAttr’ property of this object”. But bxs property of axNew is null at this moment, which causes NullPointerException.

    You need to initialize your objects with empty objects and only then you will be able to update their properties with setProperty method, e.g.:

    axNew.setBxs(new ArrayList<Bx>());
    axNew.getBsx().add(new Bx());
    // should works fine
    BeanUtils.setProperty(axNew, "bxs[0].bxAttr", "Some value"); 
    

    I would also add that it is not the best way to store objects with complex hierarchy inside DB. There are other more useful methods of serialization:

    • ORM
    • Conversion to XML/JSON
    • Java Serialization
    • etc.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert inputText to java.net.URL in JSF page: ... <h:form> <h:inputText value=${myBean.url}
I'm trying to do this (code) in old school xml. @Bean @Scope(value=request, proxyMode=ScopedProxyMode.INTERFACES) public
Hello i'm trying to convert my Java Object to a Json string to use
I am trying to convert a piece of Java code which uses a HashMap
I am trying to convert this dungeon algorithm from java into javascript, however, my
I am trying to convert this Java interpreter I wrote for my compiler to
I am trying to convert this Java (Android) code to c# (MonoDroid) but I
I'm trying to convert following java snippet to Xtend, but i couldn't able to
I have an application in C# that I'm trying to convert to java. The
I am trying to convert a python class into Java byte code with Jython

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.