java - @Autowired results in NullPointerException -


i have facelets/ jsf managed beans/ hibernate application. right trying implement di beans via annotations. have 2 of them: loginbean, registrationbean (each responsible appropriate page). problem whenever try autowire property inside registrationbean, end npe. if place same class fields loginbean - autowired no problem! below managedbeans, applicationcontext.xml (db settings ok, won't post db.properties) , class, trying autowire, stacktrace.

with best regards.

nazar

applicationcontext.xml:

<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans"        xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:tx="http://www.springframework.org/schema/tx"        xmlns:context="http://www.springframework.org/schema/context"        xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">      <context:component-scan base-package="com.dataart.mediaportal"/>      <!-- data source , persistence settings-->     <bean id="propertiesplaceholderconfigurer" class="org.springframework.beans.factory.config.propertyplaceholderconfigurer">         <property name="locations">             <list>                 <value>classpath:db.properties</value>             </list>         </property>     </bean>      <bean id="sessionfactory" class="org.springframework.orm.hibernate4.localsessionfactorybean">         <property name="datasource" ref="dmdatasource"/>         <property name="packagestoscan" value="com.dataart.mediaportal"/>         <property name="hibernateproperties">             <props>                 <prop key="hibernate.dialect">${db.dialect}</prop>                 <prop key="hibernate.show_sql">${db.show_sql}</prop>                 <prop key="hibernate.hbm2ddl.import_files">${db.import_files}</prop>                 <prop key="hibernate.hbm2ddl.auto">${db.hbm2ddl_auto}</prop>                 <prop key="connection.pool_size">${db.pool_size}</prop>                 <prop key="current_session_context_class">${db.current_session_context_class}</prop>                 <!--<prop key="hibernate.cache.provider_class">${db.provider_class}</prop>-->                 <!--<prop key="hibernate.cache.use_second_level_cache">${db.use_second_level_cache}</prop>-->                 <!--<prop key="hibernate.cache.use_query_cache">${db.use_query_cache}</prop>-->             </props>         </property>     </bean>      <bean id="transactionmanager"         class="org.springframework.orm.hibernate3.hibernatetransactionmanager">         <property name="datasource" ref="dmdatasource" />         <property name="sessionfactory" ref="sessionfactory" />     </bean>      <bean id="dmdatasource" class="org.apache.commons.dbcp.basicdatasource" destroy-method="close">         <property name="driverclassname" value="${db.driver}" />         <property name="url" value="${db.url}" />         <property name="username" value="${db.username}" />         <property name="password" value="${db.password}" />         <property name="maxwait" value="5000" />         <property name="maxactive" value="5" />     </bean>  </beans> 

loginbean:

package com.dataart.mediaportal.controller.bean;  import com.dataart.mediaportal.dao.impl.userdaoimpl; import com.dataart.mediaportal.model.user; import java.io.ioexception; import javax.faces.bean.managedbean; import javax.faces.bean.requestscoped; import javax.faces.context.externalcontext; import javax.faces.context.facescontext; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.component;  @component @requestscoped @managedbean(name = "loginbean") public class loginbean extends basebean {      private static final logger logger = loggerfactory.getlogger(loginbean.class);     private user user;     private string login;     private string password;     @autowired     private userdaoimpl userdao;      public string getlogin() {         return login;     }      public void setlogin(string login) {         this.login = login;     }      public string getpassword() {         return password;     }      public void setpassword(string password) {         this.password = password;     }       public string authorize() {         logger.debug("searching user in database...");         user = userdao.login(login, password);         if (user != null) {             logger.debug("user found.");             getsession(false).setattribute("user", user);             return "login";         }         logger.warn("user has not been found. login: " + login + ", password: " + password);         return null;     }      public void logout() throws ioexception {         logger.debug("loging out.");         externalcontext ec = facescontext.getcurrentinstance().getexternalcontext();         logger.debug("invalidating session...");         ec.invalidatesession();         logger.debug("done. forwarding home page.");         ec.redirect("login.xhtml");     }  } 

registrationbean:

package com.dataart.mediaportal.controller.bean;  import com.dataart.mediaportal.dao.impl.userdaoimpl; import com.dataart.mediaportal.model.user; import java.math.biginteger; import java.security.messagedigest; import java.security.nosuchalgorithmexception; import javax.faces.application.facesmessage; import javax.faces.bean.managedbean; import javax.faces.bean.requestscoped; import javax.faces.context.facescontext; import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.annotation.scope; import org.springframework.stereotype.component;  @component @requestscoped @managedbean(name = "registration") @scope("request") public class registrationbean extends basebean {      @autowired(required = false)     private userdaoimpl userdao;     private string reglogin;     private string regpassword;     private string name;     private string surname;     @autowired(required = true)     private user user;       public string getreglogin() {         return reglogin;     }      public void setreglogin(string reglogin) {         this.reglogin = reglogin;     }      public string getregpassword() {         return regpassword;     }      public void setregpassword(string regpassword) {         this.regpassword = regpassword;     }      public string getname() {         return name;     }      public void setname(string name) {         this.name = name;     }      public string getsurname() {         return surname;     }      public void setsurname(string surname) {         this.surname = surname;     }      public string register() throws nosuchalgorithmexception {          messagedigest md = messagedigest.getinstance("md5");         md.update(regpassword.getbytes());         string hash = new biginteger(1, md.digest()).tostring(16);         user.setuserlogin(reglogin);         user.setuserpassword(hash);         user.setusername(name);         user.setuserlastname(surname);         user.setroleid(0);          if (userdao.insertuser(user)) {             getsession(false).setattribute("user", user);             return "home";         } else {             facescontext.getcurrentinstance().                     addmessage(null, new facesmessage("registration failed!"));             return null;         }     }  } 

user:

package com.dataart.mediaportal.model;  import java.io.serializable; import java.util.list; import javax.persistence.basic; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.fetchtype; import javax.persistence.generatedvalue; import javax.persistence.generationtype; import javax.persistence.id; import javax.persistence.namedqueries; import javax.persistence.namedquery; import javax.persistence.onetomany; import javax.persistence.table; import javax.validation.constraints.notnull; import javax.validation.constraints.size; import org.springframework.stereotype.component;  @entity @table(name = "users") @namedqueries({     @namedquery(name = "user.findall", query = "select u user u")}) @component public class user implements serializable {      private static final long serialversionuid = 1l;      @basic(optional = false)     @notnull     @size(min = 1, max = 2147483647)     @column(name = "user_login")     private string userlogin;      @basic(optional = false)     @notnull     @size(min = 1, max = 32)     @column(name = "user_password")     private string userpassword;      @size(max = 2147483647)     @column(name = "user_name")     private string username;      @size(max = 2147483647)     @column(name = "user_lastname")     private string userlastname;      @column(name = "role_id")     private integer roleid;      @id     @generatedvalue(strategy = generationtype.identity)     @basic(optional = false)     @column(name = "user_id")     private integer userid;      @onetomany(mappedby = "userid", fetch = fetchtype.lazy)     private list<album> albumlist;      public user() {     }      public user(integer userid) {         this.userid = userid;     }      public user(integer userid, string userlogin, string userpassword) {         this.userid = userid;         this.userlogin = userlogin;         this.userpassword = userpassword;     }      public string getuserlogin() {         return userlogin;     }      public void setuserlogin(string userlogin) {         this.userlogin = userlogin;     }      public string getuserpassword() {         return userpassword;     }      public void setuserpassword(string userpassword) {         this.userpassword = userpassword;     }      public string getusername() {         return username;     }      public void setusername(string username) {         this.username = username;     }      public string getuserlastname() {         return userlastname;     }      public void setuserlastname(string userlastname) {         this.userlastname = userlastname;     }      public integer getroleid() {         return roleid;     }      public void setroleid(integer roleid) {         this.roleid = roleid;     }      public integer getuserid() {         return userid;     }      public void setuserid(integer userid) {         this.userid = userid;     }      public list<album> getalbumlist() {         return albumlist;     }      public void setalbumlist(list<album> albumlist) {         this.albumlist = albumlist;     } } 

stacktrace:

javax.faces.facesexception: #{registration.register()}: java.lang.nullpointerexception     @ com.sun.faces.lifecycle.invokeapplicationphase.execute(invokeapplicationphase.java:89)     @ com.sun.faces.lifecycle.phase.dophase(phase.java:101)     @ com.sun.faces.lifecycle.lifecycleimpl.execute(lifecycleimpl.java:198)     @ javax.faces.webapp.facesservlet.service(facesservlet.java:646)     @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:305)     @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210)     @ org.apache.myfaces.webapp.filter.extensionsfilter.dofilter(extensionsfilter.java:374)     @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:243)     @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210)     @ org.netbeans.modules.web.monitor.server.monitorfilter.dofilter(monitorfilter.java:393)     @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:243)     @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210)     @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:222)     @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:123)     @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:472)     @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:171)     @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:99)     @ org.apache.catalina.valves.accesslogvalve.invoke(accesslogvalve.java:936)     @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:118)     @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:407)     @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1004)     @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:589)     @ org.apache.tomcat.util.net.jioendpoint$socketprocessor.run(jioendpoint.java:312)     @ java.util.concurrent.threadpoolexecutor$worker.runtask(threadpoolexecutor.java:886)     @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:908)     @ java.lang.thread.run(thread.java:662) caused by: javax.faces.facesexception: #{registration.register()}: java.lang.nullpointerexception     @ com.sun.faces.application.actionlistenerimpl.processaction(actionlistenerimpl.java:118)     @ javax.faces.component.uicommand.broadcast(uicommand.java:315)     @ javax.faces.component.uiviewroot.broadcastevents(uiviewroot.java:790)     @ javax.faces.component.uiviewroot.processapplication(uiviewroot.java:1282)     @ com.sun.faces.lifecycle.invokeapplicationphase.execute(invokeapplicationphase.java:81)     ... 25 more caused by: javax.faces.el.evaluationexception: java.lang.nullpointerexception     @ javax.faces.component.methodbindingmethodexpressionadapter.invoke(methodbindingmethodexpressionadapter.java:101)     @ com.sun.faces.application.actionlistenerimpl.processaction(actionlistenerimpl.java:102)     ... 29 more caused by: java.lang.nullpointerexception     @ com.dataart.mediaportal.controller.bean.registrationbean.register(registrationbean.java:69)     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)     @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:39)     @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:25)     @ java.lang.reflect.method.invoke(method.java:597)     @ org.apache.el.parser.astvalue.invoke(astvalue.java:278)     @ org.apache.el.methodexpressionimpl.invoke(methodexpressionimpl.java:274)     @ com.sun.faces.facelets.el.tagmethodexpression.invoke(tagmethodexpression.java:105)     @ javax.faces.component.methodbindingmethodexpressionadapter.invoke(methodbindingmethodexpressionadapter.java:87)     ... 30 more 

try removing 'required = false' attribute 'userdao', registrationbean.

thanks.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -