0

I am working on a spring webapp and am getting a null pointer exception for a variable that is defined in the Applications class but autowired into other classes. It works in some classes but not others. Here is where the error occurs

public class pullDataFromNightlyTest {

testers tempTester;

@Autowired
JdbcTemplate jdbcTemplate;



public List<String> findAllTests() 
{
    System.out.println("hello");
    return jdbcTemplate.query("select TEST_NAME from LastNightsTests", new      RowMapper<String>() 
    {
        @Override
        public String mapRow(ResultSet rs, int rowNum) throws SQLException 
        {
            System.out.println("hello");
        System.out.println(rs.getString("TEST_NAME"));
        return rs.getString("TEST_NAME");
        }
    });
    }

}



Here is the applications java class

@ComponentScan
@EnableTransactionManagement
@EnableAutoConfiguration
public class Application {

 @Bean
    pullDataFromNightlyTest bookingService() {
        return new pullDataFromNightlyTest();
    }

@Bean
DataSource dataSource() {
    return new SimpleDriverDataSource() {{
        setDriverClass(org.h2.Driver.class);
        setUsername("sa");
        setUrl("jdbc:h2:tests");
        setPassword("");
    }};
}

@Bean
JdbcTemplate jdbcTemplate(DataSource dataSource) {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    System.out.println("Creating tables");
    jdbcTemplate.execute("drop table LastNightsTest if exists");
    jdbcTemplate.execute("create table LastNightsTest(" +
            "ID serial, TEST_NAME varchar(40) NOT NULL)");
    return jdbcTemplate;
}

public static void main(String[] args) {
    Object[] sources = {Application.class, ScheduledTasks.class};
    ApplicationContext ctx = SpringApplication.run(sources, args);
    pullDataFromNightlyTest bookingService = ctx.getBean(pullDataFromNightlyTest.class);
}
}

and here is where the method within the first class is being called

@EnableScheduling
@EnableAutoConfiguration
public class ScheduledTasks {



private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

private pullDataFromNightlyTest dataPull = new pullDataFromNightlyTest();

@Autowired
JdbcTemplate jdbcTemplate;

@Scheduled(cron = "38 57 15 * * *")
public void reportCurrentTime() throws IOException, InterruptedException, SQLException {
    System.out.println("The time is now " + dateFormat.format(new Date()));

    testers tester = new testers();

    testers[] testerArray = tester.getSmokeTestersWithTests();


    String[] names = new String[testerArray.length];
    String[] DEV = new String[testerArray.length];
    String[] OBI01 = new String[testerArray.length];
    for(int i = 0; i < testerArray.length; i++)
    {
        names[i] = testerArray[i].getName();

    }
    for (int i = 0; i < testerArray.length; i++) 
    {
        System.out.printf("Inserting tests record for " + names[i]);
        jdbcTemplate.update(
                "INSERT INTO LastNightsTest(test_name) values(?)",
                names[i]);
    }
    List<String> x = dataPull.findAllTests();

}
}
user3073234
  • 771
  • 5
  • 11
  • 24

1 Answers1

2

I've had similar problem with this type of issue and only resolved it when i advised @ComponentScan where to conduct the scan.

e.g. @ComponentScan("com.myproject.app")

Give that a go and see if it still returns null.

Also most IDEs will do a IDE level component scan to identify Beans, see if your IDE is able to pick up the bean as well.

Happy to help further if this doesn't work but please attach the error stack trace.

Aeseir
  • 7,754
  • 10
  • 58
  • 107
  • Thanks for your answer. Where in the code would the @ComponentScan go? – user3073234 Jul 03 '14 at 01:26
  • Should go in your Web App configuration class (e.g. i have it in the class that declares my viewresolver). Great example of where is showing on Spring website http://spring.io/guides/tutorials/web/3/. Glad to help otherwise, please accept answer when you get a chance. – Aeseir Jul 03 '14 at 01:38
  • Is it necessary to write an event handler for my pullDataFromNightlyTest class? – user3073234 Jul 03 '14 at 13:26
  • I can't advise one that as that is matter of opinion, personally no. – Aeseir Jul 03 '14 at 22:53