Menu

getting exception while invoking JSONObject.f

Help
RaghuNath
2007-12-25
2013-04-29
  • RaghuNath

    RaghuNath - 2007-12-25

    Hi I'm getting exception while invoking JSONObject.fromObject( new MyBean() )
    I'm using following set of libs..
    \commons-beanutils.jar
    \commons-collections-3.2.jar
    \commons-lang-2.3.jar
    \commons-logging-1.1.1.jar
    \ezmorph-1.0.4.jar
    \json-lib-2.2-jdk15.zip
    \commons-beanutils-bean-collections.jar
    \commons-beanutils-core.jar
    \junit4.4\junit-4.4.jar
    =============================================
    package com.visu.binbaz.server.json;
    import static net.sf.json.test.JSONAssert.*;
    import net.sf.json.JSONObject;
    import net.sf.json.JSONSerializer;

    public class JsonTest {
    public static void main(String[] args) {
    MyJavaBean bean = new MyJavaBean(); 
    bean.setString( "JSON" ); 
    bean.setInteger( 1 ); 
    bean.setDooble( 2.0d ); 
    bean.setBool( true ); 
    JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( bean ); 
    assertEquals( "JSON", jsonObject.getString("string") ); 
    assertEquals( 1, jsonObject.getInt("integer") ); 
    assertEquals( 2.0d, jsonObject.getDouble("dooble"), 0d ); 
    assertTrue( jsonObject.getBoolean("bool") ); 
    }
    }
    =====================================================================
    class MyJavaBean { 
    private String string; 
    private int integer; 
    private double dooble; 
    private boolean bool;
    public String getString() {
    return string;
    }
    public void setString(String string) {
    this.string = string;
    }
    public int getInteger() {
    return integer;
    }
    public void setInteger(int integer) {
    this.integer = integer;
    }
    public double getDooble() {
    return dooble;
    }
    public void setDooble(double dooble) {
    this.dooble = dooble;
    }
    public boolean isBool() {
    return bool;
    }
    public void setBool(boolean bool) {
    this.bool = bool;

    // getters & setters 

    =============================================
    Exception in thread "main" net.sf.json.JSONException: java.lang.NoSuchMethodException: Property 'bool' has no getter method
    at net.sf.json.JSONObject._fromBean(JSONObject.java:947)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:187)
    at net.sf.json.JSONArray.fromObject(JSONArray.java:192)
    at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:113)
    at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:84)
    at com.visu.binbaz.server.json.JsonTest.main(JsonTest.java:13)
    Caused by: java.lang.NoSuchMethodException: Property 'bool' has no getter method
    at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1127)
    at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:686)
    at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:715)
    at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:290)
    at net.sf.json.JSONObject._fromBean(JSONObject.java:922)
    ... 5 more

     
    • RaghuNath

      RaghuNath - 2007-12-25

      I tried with
          public boolean getBool(){..}
          public boolean isBool(){..}
      But same exception..

       
    • aalmiray

      aalmiray - 2007-12-25

      This behavior is weird, per the exception the problem lies in a PropertyUtils.getProperty() call.
      Which version of beanutils are you using?
      do you have multiple beanutils jars in your classpath by any chance?
      does it fails just for a boolean property or for any property?
      is your bean class public?

       
    • RaghuNath

      RaghuNath - 2007-12-25

      I got it..
      My bean class was not public..now it's working fine .
      I'm wondering about the exception,if it's able to pick the class def. it should be able to pick the public property in the class.
      Any how it's working great

      Thanks aalmiray ..

      raghunath nandyala

       
      • aalmiray

        aalmiray - 2007-12-25

        Well not exactly, even though the class def was available the properties were not, it has something to do with how PropertyUtils works, try the same with a package protected inner bean and you'll see that properties are not available.

        If this problem because more common then I'll look for another solution for accessing properties.

        Cheers,
        Andres

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.