NullPointerException trying to Autowire Service in Java Application (Spring) -


i writing java application going using spring, hibernate , more going packaged in jar , run command like.

my main class looks following right now:

public class app {      private static final logger logger = loggerfactory.getlogger(app.class);       @autowired     private static memberinquiryservice memberinquiryservice;       public static void main(string[] args )     {         logger.info("starting inquiry batch process");          int pendingrecords = memberinquiryservice.getpendingrecordcount();          logger.info("current number of pendinig records (" + pendingrecords + ")");          logger.info("ending inquiry batch process");     } } 

and in getpendingrecordcount return "10" testing:

public int getpendingrecordcount()     {           return 10;     }; 

why getting following error:

exception in thread "main" java.lang.nullpointerexception     @ org.xxxx.inquirybatch.app.app.main(app.java:33)     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)     @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57)     @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43)     @ java.lang.reflect.method.invoke(method.java:601)     @ com.intellij.rt.execution.application.appmain.main(appmain.java:120) 

also here databaseconfig.class

@configuration @enabletransactionmanagement @componentscan(basepackages= { "org.xxxx.inquirybatch", "org.xxxx.core" }) @propertysource("classpath:application.properties") public class databaseconfig  {       private static final logger logger = loggerfactory.getlogger(databaseconfig.class);      @autowired     environment env;      @bean      public datasource datasource()  {          string servertype = env.getproperty("server.type");          try {              if(servertype.equalsignorecase("tomcat"))             {                 com.mchange.v2.c3p0.combopooleddatasource ds = new com.mchange.v2.c3p0.combopooleddatasource();                  ds.setdriverclass(env.getproperty("database.driver"));                 ds.setuser(env.getproperty("database.user"));                 ds.setpassword(env.getproperty("database.password"));                 ds.setjdbcurl(env.getproperty("database.url"));                  return ds;             }             else             {                 context ctx = new initialcontext();                 return (datasource) ctx.lookup("java:jboss/datasources/mysqldb");             }         }         catch (exception e)         {                   logger.error(e.getmessage());         }          return null;      }      @bean     public sessionfactory sessionfactory()     {          localsessionfactorybean factorybean = new localsessionfactorybean();         factorybean.setdatasource(datasource());         factorybean.sethibernateproperties(gethibernateproperties());         factorybean.setpackagestoscan(new string[] { "org.xxxx.inquirybatch.model", "org.xxxx.core.model" } );          try {             factorybean.afterpropertiesset();         } catch (ioexception e) {             logger.error(e.getmessage());             e.printstacktrace();  //to change body of catch statement use file | settings | file templates.         }           return factorybean.getobject();     }      @bean     public properties gethibernateproperties()     {         properties hibernateproperties = new properties();          hibernateproperties.setproperty("hibernate.dialect", env.getproperty("hibernate.dialect"));         hibernateproperties.setproperty("hibernate.show_sql", env.getproperty("hibernate.show_sql"));         hibernateproperties.setproperty("hibernate.use_sql_comments", env.getproperty("hibernate.use_sql_comments"));         hibernateproperties.setproperty("hibernate.format_sql", env.getproperty("hibernate.format_sql"));         hibernateproperties.setproperty("hibernate.hbm2ddl.auto", env.getproperty("hibernate.hbm2ddl.auto"));          hibernateproperties.setproperty("hibernate.generate_statistics", env.getproperty("hibernate.generate_statistics"));             hibernateproperties.setproperty("javax.persistence.validation.mode", env.getproperty("javax.persistence.validation.mode"));          //audit history flags         hibernateproperties.setproperty("org.hibernate.envers.store_data_at_delete", env.getproperty("org.hibernate.envers.store_data_at_delete"));         hibernateproperties.setproperty("org.hibernate.envers.global_with_modified_flag", env.getproperty("org.hibernate.envers.global_with_modified_flag"));          return hibernateproperties;     }      @bean     public hibernatetransactionmanager hibernatetransactionmanager()     {         hibernatetransactionmanager htm = new hibernatetransactionmanager();         htm.setsessionfactory(sessionfactory());         htm.afterpropertiesset();         return htm;     }  } 

spring never injects static fields. , inject objects retrieved application context, or injected other objects.

you're not creating application context in program, spring doesn't play role in program.

i suggest read the documentation.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -