juneau-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From James Bognar <jamesbog...@apache.org>
Subject Re: ObjectMap parsing NullPointerException
Date Thu, 15 Jun 2017 12:53:31 GMT
Hi David,

Try using ObjectMap.cast(Class).  That should do what you're asking.


On Thu, Jun 15, 2017 at 5:20 AM, David Goddard <goddard@acm.org> wrote:

> Perfect - thanks for the quick fix, working now as expected.
>
> One related item... I want to parse an ObjectMap to an object (we
> initially parse some JSON to an ObjectMap because we don't know what object
> it represents at that stage (we discover that after examining the parsed
> map)).  I've found that Juneau can't implicitly parse an ObjectMap to a
> bean - when I try this, I get a ParseException thrown by
> JsonParserSession.getReader():
>
>   Cannot convert object of type org.apache.juneau.ObjectMap to a Reader
>
> The solution is pretty simple - first convert the ObjectMap back into JSON
> via toString():
>
>         Fails:
>         T parsedObject = (T) p.parse(objMap, objectClass);
>         Works:
>         T parsedObject = (T) p.parse(objMap.toString(), objectClass);
>
> Given that it appaears pretty trivial, I think it might be worth changing
> the parser to allow it to implicitly parse ObjectMap objects without the
> need for the developer to explicitly convert it to JSON. Although my
> knowledge of how the parser code works is only scratching at the surface so
> there may be something I'm missing.
>
> David
>
>
>
> On 14/06/2017 21:17, James Bognar wrote:
>
>> I just delivered a fix.  Try it now.
>>
>> On Wed, Jun 14, 2017 at 2:35 PM, James Bognar <
>> james.bognar@salesforce.com>
>> wrote:
>>
>> Looks like an existing bug.  Can you open a Jira ticket?
>>>
>>> On Wed, Jun 14, 2017 at 2:29 PM, James Bognar <
>>> james.bognar@salesforce.com
>>>
>>>> wrote:
>>>>
>>>
>>> Let me try it out.  I'll let you know.
>>>>
>>>> On Wed, Jun 14, 2017 at 2:03 PM, David Goddard <goddard@acm.org> wrote:
>>>>
>>>> Hi,
>>>>>
>>>>> I'm updating some code from Juneau 6.0.1 to 6.2.0-incubating and I'm
>>>>> finding that parsing some JSON to an ObjectMap that previously worked
>>>>> now
>>>>> fails with a NullPointerException:
>>>>>
>>>>>    java.lang.NullPointerException
>>>>>          at org.apache.juneau.ObjectMap.pa
>>>>> rseReader(ObjectMap.java:192)
>>>>>          at org.apache.juneau.ObjectMap.<init>(ObjectMap.java:186)
>>>>>
>>>>> This is the line of application code causing it:
>>>>>
>>>>>    ObjectMap map = new ObjectMap(new StringReader(json));
>>>>>
>>>>> The JSON in this case is:
>>>>>
>>>>> {
>>>>>    "weatherinsights": [
>>>>>          {
>>>>>              "credentials": {
>>>>>                  "username": "xx",
>>>>>                  "password": "xx",
>>>>>                  "host": "twcservice.au-syd.mybluemix.net",
>>>>>                  "port": 443,
>>>>>                  "url": "xx"
>>>>>              },
>>>>>              "syslog_drain_url": null,
>>>>>              "label": "weatherinsights",
>>>>>              "provider": null,
>>>>>              "plan": "Free-v2",
>>>>>              "name": "Weather Company Data xx",
>>>>>              "tags": [
>>>>>                  "big_data",
>>>>>                  "ibm_created",
>>>>>                  "ibm_dedicated_public"
>>>>>              ]
>>>>>          }
>>>>>      ],
>>>>>     "cloudantNoSQLDB": [
>>>>>        {
>>>>>           "name": "xx",
>>>>>           "label": "cloudantNoSQLDB",
>>>>>           "plan": "Shared",
>>>>>           "credentials": {
>>>>>              "username": "xx",
>>>>>              "password": "xx",
>>>>>              "host": "xx-bluemix.cloudant.com",
>>>>>              "port": 443,
>>>>>              "url": "xx"
>>>>>           }
>>>>>        }
>>>>>     ]
>>>>> }
>>>>>
>>>>> Looking in debug, I see that the failure in ObjectMap is here:
>>>>>
>>>>>    private void parseReader(Reader r, Parser p) throws ParseException
{
>>>>>          if (p == null)
>>>>>                  p = JsonParser.DEFAULT;
>>>>>          p.parseIntoMap(r, this, session.string(), session.object());
>>>>>    }
>>>>>
>>>>> This fails because the variable, session, is null.  I can't figure out
>>>>> why.  Stepping through it in debug, before it gets to this point, I
>>>>> see the
>>>>> ObjectMap being initialised here:
>>>>>
>>>>>          public ObjectMap(BeanSession session) {
>>>>>                  super();
>>>>>                  this.session = session;
>>>>>          }
>>>>>
>>>>> In this case, the passed value of session is *not* null:
>>>>>
>>>>>    {
>>>>>          BeanContext: {
>>>>>                  id: -981232688,
>>>>>                  beansRequireDefaultConstructor: false,
>>>>>                  beansRequireSerializable: false,
>>>>>                  beansRequireSettersForGetters: false,
>>>>>                  beansRequireSomeProperties: true,
>>>>>                  beanMapPutReturnsOldValue: false,
>>>>>                  beanConstructorVisibility: 'PUBLIC',
>>>>>                  beanClassVisibility: 'PUBLIC',
>>>>>                  beanMethodVisibility: 'PUBLIC',
>>>>>                  beanFieldVisibility: 'PUBLIC',
>>>>>                  useInterfaceProxies: true,
>>>>>                  ignoreUnknownBeanProperties: false,
>>>>>                  ignoreUnknownNullBeanProperties: true,
>>>>>                  ignorePropertiesWithoutSetters: true,
>>>>>                  ignoreInvocationExceptionsOnGetters: false,
>>>>>                  ignoreInvocationExceptionsOnSetters: false,
>>>>>                  useJavaBeanIntrospector: false,
>>>>>                  beanFilters: [
>>>>>                  ],
>>>>>                  pojoSwaps: [
>>>>>                  ],
>>>>>                  notBeanClasses: [
>>>>>                  ],
>>>>>                  implClasses: {
>>>>>                  },
>>>>>                  sortProperties: false,
>>>>>                  locale: 'en_GB',
>>>>>                  timeZone: null,
>>>>>                  mediaType: null
>>>>>          },
>>>>>          BeanSession: {
>>>>>                  locale: 'en_GB',
>>>>>                  timeZone: null
>>>>>          }
>>>>>    }
>>>>>
>>>>>
>>>>> However, by the time we get to parseReader, session *is* null.
>>>>>
>>>>> I'm obviously not ruling out something dumb on my part, but is there
>>>>> any
>>>>> obvious issue here, or somewhere for me to look further?
>>>>>
>>>>> Thanks,
>>>>>
>>>>> David
>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>> James Bognar
>>>>
>>>>
>>>
>>>
>>> --
>>> James Bognar
>>>
>>>
>>
>>
>>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message