You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Julien Martin <ba...@gmail.com> on 2011/10/16 21:46:17 UTC

Tapestry failing to inject a service into a Translator with @Inject?

Hello,
I have the following Translator implementation:

package com.bignibou.web.services;

import com.bignibou.domain.Postcode;
import com.bignibou.service.BignibouService;
import org.apache.tapestry5.Field;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.Translator;
import org.apache.tapestry5.ValidationException;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.FormSupport;




public class PostcodeTranslator implements Translator<Postcode> {

    @Inject
    BignibouService service;

    @Override
    public String getName() {
        return "postcodeTranslator";
    }

    @Override
    public String toClient(Postcode value) {
        return value.getPostcode();
    }

    @Override
    public Class<Postcode> getType() {
        return Postcode.class;
    }

    @Override
    public String getMessageKey() {
        return "postcodeTranslator-parse-exception";
    }

    @Override
    public Postcode parseClient(Field field, String clientValue, String
message) throws ValidationException {
        try {
            String[] postcodesIds = clientValue.split(",");
            int postcodeId = Integer.parseInt(postcodesIds[0]);
            return service.loadPostcodeById(postcodeId);*//NULL POINTER
EXCEPTION HERE!!!*
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new ValidationException(message);
        }
    }

    @Override
    public void render(Field field, String message, MarkupWriter writer,
FormSupport formSupport) {

    }
}

Unfortunately it seems that my service is not properly injected by @Inject
and I get a NPE when I tried to use my service variable.

Note that this does not occur with a ValueEncoder implementation that I use
elsewhere in the application...

Can anyone help please?

Regards,

Julien.

Re: Tapestry failing to inject a service into a Translator with @Inject?

Posted by Julien Martin <ba...@gmail.com>.
Thanks a lot Martin!!
It works much better now. [?]
Julien.

2011/10/16 Martin Strand <do...@gmail.com>

> On Sun, 16 Oct 2011 22:09:14 +0200, Julien Martin <ba...@gmail.com>
> wrote:
>
>  It might have to do with the way I contribute my Translator i.e.:
>>
>> @Contribute(TranslatorSource.**class)
>> public static void provideTranslators(**MappedConfiguration<Class,
>> Translator> configuration) {
>>  configuration.add(Postcode.**class, new PostcodeTranslator());
>> }
>>
>> See how I instanciate the PostcodeTranslator...
>> I don't know how to get round this...
>>
>
>
> If PostcodeTranslator is a defined service, you can use addInstance and
> Tapestry will autobuild the service for you:
>
> configuration.addInstance(**Postcode.class, PostcodeTranslator.class);
>
>
>
> otherwise you can inject its dependencies into the contribution method, and
> pass to a constructor:
>
> @Contribute(TranslatorSource.**class)
> public static void provideTranslators(**MappedConfiguration<Class,
> Translator> configuration, BignibouService service) {
>  configuration.add(Postcode.**class, new PostcodeTranslator(service));
> }
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.**apache.org<us...@tapestry.apache.org>
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: Tapestry failing to inject a service into a Translator with @Inject?

Posted by Martin Strand <do...@gmail.com>.
On Sun, 16 Oct 2011 22:09:14 +0200, Julien Martin <ba...@gmail.com> wrote:

> It might have to do with the way I contribute my Translator i.e.:
>
> @Contribute(TranslatorSource.class)
> public static void provideTranslators(MappedConfiguration<Class,  
> Translator> configuration) {
>   configuration.add(Postcode.class, new PostcodeTranslator());
> }
>
> See how I instanciate the PostcodeTranslator...
> I don't know how to get round this...


If PostcodeTranslator is a defined service, you can use addInstance and  
Tapestry will autobuild the service for you:

configuration.addInstance(Postcode.class, PostcodeTranslator.class);



otherwise you can inject its dependencies into the contribution method,  
and pass to a constructor:

@Contribute(TranslatorSource.class)
public static void provideTranslators(MappedConfiguration<Class,  
Translator> configuration, BignibouService service) {
   configuration.add(Postcode.class, new PostcodeTranslator(service));
}

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Tapestry failing to inject a service into a Translator with @Inject?

Posted by Julien Martin <ba...@gmail.com>.
It might have to do with the way I contribute my Translator i.e.:

*    @Contribute(TranslatorSource.class)*
*    public static void provideTranslators(MappedConfiguration<Class,
Translator> configuration){*
*        configuration.add(Postcode.class, new PostcodeTranslator());*
*    }*

See how I instanciate the PostcodeTranslator...
I don't know how to get round this...
Can someone please help?
Regards,
Julien.


2011/10/16 Julien Martin <ba...@gmail.com>

> Hello,
> I have the following Translator implementation:
>
> package com.bignibou.web.services;
>
> import com.bignibou.domain.Postcode;
> import com.bignibou.service.BignibouService;
> import org.apache.tapestry5.Field;
> import org.apache.tapestry5.MarkupWriter;
> import org.apache.tapestry5.Translator;
> import org.apache.tapestry5.ValidationException;
> import org.apache.tapestry5.ioc.annotations.Inject;
> import org.apache.tapestry5.services.FormSupport;
>
>
>
>
> public class PostcodeTranslator implements Translator<Postcode> {
>
>     @Inject
>     BignibouService service;
>
>     @Override
>     public String getName() {
>         return "postcodeTranslator";
>     }
>
>     @Override
>     public String toClient(Postcode value) {
>         return value.getPostcode();
>     }
>
>     @Override
>     public Class<Postcode> getType() {
>         return Postcode.class;
>     }
>
>     @Override
>     public String getMessageKey() {
>         return "postcodeTranslator-parse-exception";
>     }
>
>     @Override
>     public Postcode parseClient(Field field, String clientValue, String
> message) throws ValidationException {
>         try {
>             String[] postcodesIds = clientValue.split(",");
>             int postcodeId = Integer.parseInt(postcodesIds[0]);
>             return service.loadPostcodeById(postcodeId);*//NULL POINTER
> EXCEPTION HERE!!!*
>         } catch (RuntimeException e) {
>             e.printStackTrace();
>             throw new ValidationException(message);
>         }
>     }
>
>     @Override
>     public void render(Field field, String message, MarkupWriter writer,
> FormSupport formSupport) {
>
>     }
> }
>
> Unfortunately it seems that my service is not properly injected by @Inject
> and I get a NPE when I tried to use my service variable.
>
> Note that this does not occur with a ValueEncoder implementation that I use
> elsewhere in the application...
>
> Can anyone help please?
>
> Regards,
>
> Julien.
>