You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Bhuvan Gupta <bh...@gmail.com> on 2015/10/10 22:46:42 UTC

why adding a processor after unmarshaling in apache camel throw nullpointer

Hello,

I setup the camel routes shown below:
In stage folder i can see message getting marshal properly into a json.
but while un-marshaling on this line body.setA("modified A");
i get a null pointer exception basically body is null.. *Why* ?

StackOverflow question link:
http://stackoverflow.com/questions/33058563/adding-a-processor-after-unmarshaling-in-apache-camel

from("direct:stage").marshal().json(JsonLibrary.Gson).to("file://stage");

from("file://stage").unmarshal().json(JsonLibrary.Gson).process(new
Processor() {
    @Override
    public void process(Exchange exchange) throws Exception {
        MyTest body = exchange.getIn().getBody(MyTest.class);
        body.setA("modified A");
    }}).to("direct:b");

Re: why adding a processor after unmarshaling in apache camel throw nullpointer

Posted by Andrew Block <an...@gmail.com>.
The Gson library will unmarshal an object to specified type if it is provided, otherwise it will return a LinkedHashMap of the objects that it was able to unmarshal. The getBody() in the processor has no direct connection to the previously unmarshaled object the object type must be specified in the unmarshal component. 

- Andy

-- 
Andrew Block

On October 11, 2015 at 5:27:19 AM, Bhuvan Gupta (bhuvangu@gmail.com) wrote:

It worked once i changes to 
             unmarshal().json(JsonLibrary.Gson, MyTest.class)

So basically while unmarshalling we have to provide the Type to convert to, we just cant except it to change json string to a type specified in getBody(..) method..   this is the reason... . right ?

I will update the stackoverflow once i get the correct explaination.

Thanks
Bhuvan

On Sun, Oct 11, 2015 at 3:46 PM, Bhuvan Gupta <bh...@gmail.com> wrote:
I changed the processor code to 
                 Object body = exchange.getIn().getBody();
System.out.println("class: "+body.getClass()); 
System.out.println("as string:" + body);

and it printed :

class: class com.google.gson.internal.LinkedTreeMap
as string:{a=this is a, b=this is b, c={}}

What is happening... not able to create a mind map of what is happening.

Cheers
Bhuvan

On Sun, Oct 11, 2015 at 8:21 AM, Andrew Block <an...@gmail.com> wrote:
In your processor, since you are requesting the object contained in the message body as a particular type, it is possible for the value to be null if that is not the type that is actually on the body. Would you be able to verify the object type after the unmarshall.? You can do this in your processor by getting the Object on the message without a type (message.getIn().getBody() and checking the type. 

- Andy


-- 
Andrew Block

On October 10, 2015 at 3:46:52 PM, Bhuvan Gupta (bhuvangu@gmail.com) wrote:

Hello,

I setup the camel routes shown below:
In stage folder i can see message getting marshal properly into a json.
but while un-marshaling on this line body.setA("modified A");
i get a null pointer exception basically body is null.. *Why* ?

StackOverflow question link:
http://stackoverflow.com/questions/33058563/adding-a-processor-after-unmarshaling-in-apache-camel

from("direct:stage").marshal().json(JsonLibrary.Gson).to("file://stage");

from("file://stage").unmarshal().json(JsonLibrary.Gson).process(new
Processor() {
@Override
public void process(Exchange exchange) throws Exception {
MyTest body = exchange.getIn().getBody(MyTest.class);
body.setA("modified A");
}}).to("direct:b");



Re: why adding a processor after unmarshaling in apache camel throw nullpointer

Posted by Bhuvan Gupta <bh...@gmail.com>.
It worked once i changes to
             unmarshal().json(JsonLibrary.Gson, MyTest.class)

So basically while unmarshalling we have to provide the Type to convert to,
we just cant except it to change json string to a type specified in
getBody(..) method..   this is the reason... . right ?

I will update the stackoverflow once i get the correct explaination.

Thanks
Bhuvan

On Sun, Oct 11, 2015 at 3:46 PM, Bhuvan Gupta <bh...@gmail.com> wrote:

> I changed the processor code to
>                  Object body = exchange.getIn().getBody();
> System.out.println("class: "+body.getClass());
> System.out.println("as string:" + body);
>
> and it printed :
>
> class: class com.google.gson.internal.LinkedTreeMap
> as string:{a=this is a, b=this is b, c={}}
>
> What is happening... not able to create a mind map of what is happening.
>
> Cheers
> Bhuvan
>
> On Sun, Oct 11, 2015 at 8:21 AM, Andrew Block <an...@gmail.com>
> wrote:
>
>> In your processor, since you are requesting the object contained in the
>> message body as a particular type, it is possible for the value to be null
>> if that is not the type that is actually on the body. Would you be able to
>> verify the object type after the unmarshall.? You can do this in your
>> processor by getting the Object on the message without a type
>> (message.getIn().getBody() and checking the type.
>>
>> - Andy
>>
>>
>> --
>> Andrew Block
>>
>> On October 10, 2015 at 3:46:52 PM, Bhuvan Gupta (bhuvangu@gmail.com)
>> wrote:
>>
>> Hello,
>>
>> I setup the camel routes shown below:
>> In stage folder i can see message getting marshal properly into a json.
>> but while un-marshaling on this line body.setA("modified A");
>> i get a null pointer exception basically body is null.. *Why* ?
>>
>> StackOverflow question link:
>>
>> http://stackoverflow.com/questions/33058563/adding-a-processor-after-unmarshaling-in-apache-camel
>>
>> from("direct:stage").marshal().json(JsonLibrary.Gson).to("file://stage");
>>
>> from("file://stage").unmarshal().json(JsonLibrary.Gson).process(new
>> Processor() {
>> @Override
>> public void process(Exchange exchange) throws Exception {
>> MyTest body = exchange.getIn().getBody(MyTest.class);
>> body.setA("modified A");
>> }}).to("direct:b");
>>
>>
>

Re: why adding a processor after unmarshaling in apache camel throw nullpointer

Posted by Bhuvan Gupta <bh...@gmail.com>.
I changed the processor code to
                 Object body = exchange.getIn().getBody();
System.out.println("class: "+body.getClass());
System.out.println("as string:" + body);

and it printed :

class: class com.google.gson.internal.LinkedTreeMap
as string:{a=this is a, b=this is b, c={}}

What is happening... not able to create a mind map of what is happening.

Cheers
Bhuvan

On Sun, Oct 11, 2015 at 8:21 AM, Andrew Block <an...@gmail.com> wrote:

> In your processor, since you are requesting the object contained in the
> message body as a particular type, it is possible for the value to be null
> if that is not the type that is actually on the body. Would you be able to
> verify the object type after the unmarshall.? You can do this in your
> processor by getting the Object on the message without a type
> (message.getIn().getBody() and checking the type.
>
> - Andy
>
>
> --
> Andrew Block
>
> On October 10, 2015 at 3:46:52 PM, Bhuvan Gupta (bhuvangu@gmail.com)
> wrote:
>
> Hello,
>
> I setup the camel routes shown below:
> In stage folder i can see message getting marshal properly into a json.
> but while un-marshaling on this line body.setA("modified A");
> i get a null pointer exception basically body is null.. *Why* ?
>
> StackOverflow question link:
>
> http://stackoverflow.com/questions/33058563/adding-a-processor-after-unmarshaling-in-apache-camel
>
> from("direct:stage").marshal().json(JsonLibrary.Gson).to("file://stage");
>
> from("file://stage").unmarshal().json(JsonLibrary.Gson).process(new
> Processor() {
> @Override
> public void process(Exchange exchange) throws Exception {
> MyTest body = exchange.getIn().getBody(MyTest.class);
> body.setA("modified A");
> }}).to("direct:b");
>
>

Re: why adding a processor after unmarshaling in apache camel throw nullpointer

Posted by Andrew Block <an...@gmail.com>.
In your processor, since you are requesting the object contained in the message body as a particular type, it is possible for the value to be null if that is not the type that is actually on the body. Would you be able to verify the object type after the unmarshall.? You can do this in your processor by getting the Object on the message without a type (message.getIn().getBody() and checking the type. 

- Andy


-- 
Andrew Block

On October 10, 2015 at 3:46:52 PM, Bhuvan Gupta (bhuvangu@gmail.com) wrote:

Hello,  

I setup the camel routes shown below:  
In stage folder i can see message getting marshal properly into a json.  
but while un-marshaling on this line body.setA("modified A");  
i get a null pointer exception basically body is null.. *Why* ?  

StackOverflow question link:  
http://stackoverflow.com/questions/33058563/adding-a-processor-after-unmarshaling-in-apache-camel  

from("direct:stage").marshal().json(JsonLibrary.Gson).to("file://stage");  

from("file://stage").unmarshal().json(JsonLibrary.Gson).process(new  
Processor() {  
@Override  
public void process(Exchange exchange) throws Exception {  
MyTest body = exchange.getIn().getBody(MyTest.class);  
body.setA("modified A");  
}}).to("direct:b");