Announcement Announcement Module
Collapse
No announcement yet.
Autowired services are null when not debugging Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Autowired services are null when not debugging

    I'm attempting to get my application which consists of a couple of beans up and running but I feel like I'm chasing my tail when it comes to getting these services available (for whatever reason when I check in debugging I'm able to get them ... but get null pointer exceptions when I run the methods without a breakpoint!!!?)

    The goal is to have this statistics manager run in the background of the bot collecting and updating data...

    The classes instantiate themselves on their own once defined in the application-context...which I guess is what's supposed to happened.

    Update

    I can't figure out how to delete my post! So I'm just going to update this with what I think is the appopriate way to do this with my app. I got it working and no more null pointer exceptions

    Here is the configuration:

    application-context.xml
    Code:
    	
             <context:component-scan base-package="java.main.data" />
    
             <bean id='tradingBot' class='java.main.example.Bot'>
    		<constructor-arg index="0">
    			<ref bean="simpleTaskExecutor" />
    		</constructor-arg>
    		<constructor-arg index="1">
    			<ref bean="mtgoxUSD" />
    		</constructor-arg>
    		<constructor-arg index="2">
    			<ref bean="taskScheduler" />
    		</constructor-arg>
    	</bean>
    I included the bean for Bot because it's where the main application context is loaded.

    Bot.java:

    Code:
    public class Bot{
    
    	public static void main(String[] args) throws Exception {
    		ApplicationContext context = new ClassPathXmlApplicationContext(
    				"TradingBot.xml");
                    StatisticsManager manager = new ((StatisticsManager) context.getBean("statisticsManager", StatisticsManager.class));
                    manager.run( // pass any bean instantiations that aren't autowired .. );
            }
    }
    and the class that's giving me trouble:

    Code:
    @Component
    class StatisticsManager{
          @Autowired 
          public TradesService tradeService;
    
          public void run(// nonauto wired args){
                 taskExecutor.execute((Runnable) new StatisticsManagerRunner(...));
          }
    
          private class StatisticsManagerRunner implements Runnable(){
                 @Override
                 public void run(){
                         // autowired services should now work
                 }
          }
    }
    Last edited by jordanbaucke; Mar 25th, 2013, 11:15 AM.
Working...
X