0

I am trying to use the jdbcTemplate to query a MySQL database to get the SYSDATE. So the query is as simple as SELECT SYSDATE()

However, I get the following error:

Exception in thread "main" java.lang.NullPointerException
at com.trade.xml.modifier.Test.<init>(Test.java:18)
at com.trade.xml.modifier.TradeModifierApplication.main(TradeModifierApplication.java:17)

Here is a bit of what I do:

  1. I created a DatabaseConfiguration.class which should take all the connection details from the application.properties

The application.properties looks like this:

spring.datasource.url=jdbc:mysql://localhost:3306/xmlconverter
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect

The DatabaseConfiguration.class looks like this

@Configuration
public class DatabaseConfiguration {

        @Value("${spring.datasource.driver-class-name}")
        private String driverName;

        @Value("${spring.datasource.url}")
        private String url;

        @Value("${spring.datasource.username}")
        private String userName;

        @Value("${spring.datasource.password}")
        private String password;

        @Bean(name = "dataSource")
        public DataSource dataSource() {
            final DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName(driverName);
            dataSource.setUrl(url);
            dataSource.setUsername(userName);
            dataSource.setPassword(password);
            return dataSource;
        }
    
    @Bean
    public JdbcTemplate dbjdbcTemplate() {
        return new JdbcTemplate(dataSource());
    }

}

2.I also created a Test.class where I wrote my method for querying

@Service
public class Test {
    
    //Since a bean is created in DatabaseConfiguration, 
    //"template = new JdbcTemplate()" is omitted (should be recognized automatically, right?)
    @Autowired
    private JdbcTemplate template;
    
    
    public String getDate() {
        String sql = "SELECT SYSDATE()";
        
        return template.queryForObject(sql, String.class);
    }

}
  1. The main class looks as follows

     @SpringBootApplication
     public class TradeModifierApplication {
    
     public static void main(String[] args) {
         SpringApplication.run(TradeModifierApplication.class, args);
    
         Test test = new Test();
         System.out.println(test.getDate());
    

At this point I have read the whole Internet and I am not sure what is the root cause of this NPE. Any help/hints will be appreciated.

Ne7WoRK
  • 161
  • 1
  • 1
  • 15

1 Answers1

0

The problem should be this line in TradeModifierApplication:

Test test = new Test();

You should let the spring container to instance Test class.

One way would be to inject by:

public static void main(String[] args) {
    ApplicationContext applicationContext = SpringApplication.run(TradeModifierApplication.class, args);
    Test service = applicationContext.getBean(Test.class);
    service.getDate());
}

}

devwebcl
  • 2,866
  • 3
  • 27
  • 46