java - hibernate/jpa Autowire annotation producing Nullpointer exception errors -
new java on here.
i using spring boot , working eclipse kepler make program.
is possible define autowired in regular class - or - must use in controller class?
i trying make group of functions/methods in class (valfuncsauth). 1 of methods (validateauthinfo), getting error:
java.lang.nullpointerexception
listed below.
i created "helper" function because - in different controllers - executing same code. kind of check/validation. trying put (the check/validation code) in 1 place (like function). wanted make call particular function check/validation (and not repeating same code each controller).
why getting error? there way fix this?
tia
java.lang.nullpointerexception: null @ ccinfw.helpfulfunctions.valfuncsauth.validateauthinfo(valfuncsauth.java:121) @ ccinfw.controller.work.workprojectcontroller.createproject(workprojectcontroller.java:97) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(unknown source) @ sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source) @ java.lang.reflect.method.invoke(unknown source) @ org.springframework.web.method.support.invocablehandlermethod.doinvoke(invocablehandlermethod.java:205) @ org.springframework.web.method.support.invocablehandlermethod.invokeforrequest(invocablehandlermethod.java:133) @ org.springframework.web.servlet.mvc.method.annotation.servletinvocablehandlermethod.invokeandhandle(servletinvocablehandlermethod.java:97) @ org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.invokehandlermethod(requestmappinghandleradapter.java:827) @ org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.handleinternal(requestmappinghandleradapter.java:738) @ org.springframework.web.servlet.mvc.method.abstracthandlermethodadapter.handle(abstracthandlermethodadapter.java:85) @ org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:967)
i defined following "helper" class
public class valfuncsauth { private static final logger logger = loggerfactory .getlogger(customercontroller.class); private securerandom random = new securerandom(); [... snip ...] @autowired private mstrstoreheaddao companydao; [... snip ...] public void validateauthinfo(bigdecimal tenantid, bigdecimal authid, string authtype, string timezone) throws exception { [... snip ...] try { // company info - make sure exists companyitem = companydao.findbytenantid(tenantid); <<<< --- error here if (companyitem.size() != 1) { throw new datainputexception( " => emsg-25590 - invalid tenantid passed in : ->" + tenantid); } [... snip ...] }
the dao
@transactional @repository public interface mstrstoreheaddao extends crudrepository<mstrstorehead, bigdecimal> { public list<mstrstorehead> findall(); public list<mstrstorehead> findbytenantid(bigdecimal id); }
iniitaliztion
@requestmapping(value = "/create/{tenantid}/{jobid}", method = requestmethod.post, produces = "application/json") public responseentity<void> createjob ( @pathvariable bigdecimal tenantid, @pathvariable bigdecimal jobid, @requestbody workjobchargesparamiopojo input) throws exception { valfuncsauth valfuncs = new valfuncsauth(); try { [... snip ...] /** * make sure person creating record (the worker) * authorized - convert his/her timezone */ valfuncs.validateauthinfo(tenantid, input.getloggedinreclockid(), input.getloggedinreclocktype(), input.getloggedintimezone()); [... snip ...]
the problem way initializing bean
valfuncsauth valfuncs = new valfuncsauth();
when initialize bean spring container not know autowired beans, need let spring container initialize object ( mark bean) annotate class component
@component public class valfuncsauth {
and autowired controller
@autowired private valfuncsauth valfuncsauth; @requestmapping(value = "/create/{tenantid}/{jobid}", method = requestmethod.post, produces = "application/json") public responseentity<void> createjob ( @pathvariable bigdecimal tenantid, @pathvariable bigdecimal jobid, @requestbody workjobchargesparamiopojo input) throws exception {
now if want bean use different object on each call of api need change scope of bean prototype
@component @scope("prototype") public class valfuncsauth {
Comments
Post a Comment