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();
}
}