0

Spring + JPA @Wire variable is not working giving null pointer [closed]

asked 2013-01-25 13:59:55 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2621 3 8
http://emrpms.blogspot.in...

updated 2013-01-25 14:05:26 +0800

Please see all my code here

In my VM, I have declared like this

@WireVariable private examplesListService examplesListService =null;

@Command public void showrecordcount() { List<exampleslist> result = examplesListService.findAll(); Messagebox.show(" Count is " + result.size()); }

Now the findall giving null pointer exception.

delete flag offensive retag edit

The question has been closed for the following reason "the question is answered, right answer was accepted" by Senthilchettyin
close date 2013-01-30 05:08:37

Comments

Any help please ?

Senthilchettyin ( 2013-01-25 18:31:00 +0800 )edit

4 Answers

Sort by ยป oldest newest most voted
4

answered 2013-01-26 19:32:59 +0800

gekkio gravatar image gekkio flag of Finland
899 1
http://gekkio.fi/blog

Your Spring ApplicationContext doesn't contain the bean "examplesListService", so the wiring will not work. One of the reasons why I dislike @WireVariable is that it doesn't throw an exception if the wiring fails, it will just leave the field as null. This makes it difficult to debug stuff like this.

You are using component-scan element in the XML configuration, but that only works for beans that are annotated with something that component-scan supports (e.g. @Component, @service, @Repository). For example, your DAO will be managed by Spring correctly, because it includes the @Repository annotation.

Just add @service to examplesListService, and the wiring should work fine. Please note that you must also add @Autowired to the

private examplesListDao examplesListDao;

field in the service class, because otherwise it will be null, and your code will throw a NullPointerException in the service class.

BTW, the Java convention is to always use CamelCase class names and lowercase package names, so it's a good idea to rename the classes to start with an uppercase letter, and convert the package names to lowercase. Also, usually @Transactional is added to the service class methods, not to DAOs.

link publish delete flag offensive edit

Comments

You are 100 % correct. I have added the bean reference as follows <bean id="examplesListDao" class="com.org.zkexamples.examplesListDaoImpl" /> <bean id="examplesListService" class="com.org.zkexamples.examplesListServiceImp"> <property name="examplesListDao" ref="examplesListDao" /> </bean>

Senthilchettyin ( 2013-01-27 09:19:36 +0800 )edit

I will try your suggestion also.

Senthilchettyin ( 2013-01-27 09:20:13 +0800 )edit
1

answered 2013-01-29 16:11:47 +0800

gekkio gravatar image gekkio flag of Finland
899 1
http://gekkio.fi/blog

So i just want to understand why service interface is not identified. I am new to Spring and ZK, so please help me.

That happens because component scanning will assign names for the Spring beans automatically. So, "zkSpringJpaExampleServiceImpl" is the "default name" when you use @service annotation on the class ZkSpringJpaExampleServiceImpl.

ZK @WireVariable injects by name, not by type, so the variable name must be an exact match to the bean name. This is why it works if you change the variable name "zkSpringJpaExampleServiceImpl", because then it matches with the default name given by component scanning. Injection by name and not by type is another reason why I dislike @WireVariable. Note that Spring @Autowired by default injects by type, so the @Autowired variable name can be anything and it will work. This is why the service implementation can see the DAO even though the DAO has the bean name "zkSpringJpaExampleDaoImpl", which is not the same as the variable name.

You can customize names by adding them to the annotations. For example:

@Service("myBean")
public class ZkSpringJpaExampleServiceImpl ...

Now the bean gets registered as "myBean", so that should be the variable name:

@WireVariable
private ZkSpringJpaExampleService myBean;

By forcing your own names to Spring beans, you can use more meaningful names than what component scanning gives them by default.

PS. Don't add the service annotation to the interface, just the implementation class. Otherwise the code looks fine to me.

link publish delete flag offensive edit
0

answered 2013-01-29 07:53:52 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2621 3 8
http://emrpms.blogspot.in...

Hi gekkio

Thank you very much. As you said, i have added the XML Configuration for the Bean and everything works fine.

Just for the new comers, i have created step by step tutorial here

Now, i am trying to convert to Annotation as you suggest.

Here is my complete code.

Now again null pointer is coming. I just observe the output and found that the following bean has been identified. zkSpringJpaExampleDaoImpl,zkSpringJpaExampleServiceImpl

But there is no bean for my interface ZkSpringJpaExampleService even though i annotate as @service.

And also just a work around, i changed the variable in the VM

@WireVariable
private ZkSpringJpaExampleService ZkSpringJpaExampleService;

TO @WireVariable private ZkSpringJpaExampleService zkSpringJpaExampleServiceImpl;

Now, it is works fine.

So i just want to understand why service interface is not identified. I am new to Spring and ZK, so please help me.

link publish delete flag offensive edit
0

answered 2013-01-30 05:07:54 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2621 3 8
http://emrpms.blogspot.in...

Great. Thank you very much for explaining me the concepts. I also started dislike the @wirevariable,because in some places it working fine, but in some places still it gives null pointer, so i manage by callling zk Springutility.getbean method.

As you said, i have changed the code accordingly. And also i found another method to resolve this by declaring the bean <bean id="zkSpringJpaExampleService" class="zkCommon.zkSpringJPA.ZkSpringJpaExampleServiceImpl"/>

Anyway, thank you once again. This help me to complete first ZK Small application. I will update in this post, once i hosted the application.

link publish delete flag offensive edit

Comments

Can you please give your email id ?

Senthilchettyin ( 2013-01-30 07:28:09 +0800 )edit

Question tools

Follow
1 follower

RSS

Stats

Asked: 2013-01-25 13:59:55 +0800

Seen: 198 times

Last updated: Jan 30 '13

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More