Autowired变量上的Spring MVC空指针

时间:2014-07-02 20:03:47

标签: spring nullpointerexception autowired

我正在使用spring webapp并且正在为Applications类中定义的变量获取空指针异常,但是已自动连接到其他类。它适用于某些类,但不适用于其他类。这是发生错误的地方

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

这里是第一个类中的方法被调用的地方

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

}
}

1 个答案:

答案 0 :(得分:2)

我遇到类似问题的类似问题,只有当我建议@ComponentScan进行扫描时才解决它。

e.g。 @ComponentScan( “com.myproject.app”)

给它一个去看它是否仍然返回null。

此外,大多数IDE都会进行IDE级别组件扫描以识别Beans,看看您的IDE是否也能够获取该bean。

如果这不起作用,请继续提供帮助,但请附上错误堆栈跟踪。