0

I have a problem, I created a connection to an Oracle database using the JDBC driver.

All the examples I find on internet use like:

@SpringBootApplication
public class CanalCorSdqsApplication implements CommandLineRunner{

    public static void main(String[] args) throws Exception {         
        SpringApplication.run(CanalCorSdqsApplication.class, args);
    }
        
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void run(String... args) throws Exception {
       
        String sql = "SELECT * FROM USUARIO_WEBSERVICES";
        List<UsuarioWebService> students = jdbcTemplate.query(sql,BeanPropertyRowMapper.newInstance(UsuarioWebService.class));            
        students.forEach(System.out :: println);
        System.out.println(students.get(0).getLoginUsuario());   
    }

That works for me too, but when I try to do the same in another class, it throws

java.lang.NullPointerException:

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

public class ProcesoPrincipal implements CommandLineRunner {
    
    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    public void consulta(){
        String sql = "SELECT * FROM USUARIO_WEBSERVICES";
        List<UsuarioWebService> students = jdbcTemplate.query(sql,BeanPropertyRowMapper.newInstance(UsuarioWebService.class));            
        students.forEach(System.out :: println);
    }

    @Override
    public void run(String... args) throws Exception {
        String sql = "SELECT * FROM USUARIO_WEBSERVICES";
        List<UsuarioWebService> students = jdbcTemplate.query(sql,BeanPropertyRowMapper.newInstance(UsuarioWebService.class));            
        students.forEach(System.out :: println);
    }

In both methods it throws java.lang.NullPointerException.

Do you know what it is happening and how to fix?

this is the project:

enter image description here

  • Is this your entire application? Or in other words, is this a [mre]? Could you also provide the entire stacktrace? – Mark Rotteveel Jun 30 '21 at 17:01
  • @MarkRotteveel its entire cus it very small project for now. just a object (usuario), main class, and other class. – Diego Izquierdo Dussan Jun 30 '21 at 17:08
  • 1
    Does this answer your question? [Why is my Spring @Autowired field null?](https://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null) – Denis Zavedeev Jun 30 '21 at 17:22
  • @caco3 I dont get that at all. but I tried some of those and don´t works :S. still trowing null. – Diego Izquierdo Dussan Jun 30 '21 at 20:31
  • 1
    Do you have `new ProcesoPrincipal()` in your code? If yes, then you can remove the `new`, mark `ProcesoPrincipal` as a `@Component` and `@Autowired` it into the place where you need it – Denis Zavedeev Jun 30 '21 at 21:01
  • @caco3 Yes I did. In the main class I change it for `@Autowired private ProcesoPrincipal pp;` and in the ProcesoPrincipal class still `@Autowired private JdbcTemplate jdbcTemplate;` and it works. I dont get at all the tag Autowired but you solution works. Thank you so much! – Diego Izquierdo Dussan Jul 01 '21 at 15:11

2 Answers2

0

You need to add @Service or @Component to the ProcesoPrincipal class.

dukethrash
  • 1,449
  • 4
  • 15
  • 25
0

Your new class implements CommandLineRunner. Why? Take off implements CommandLineRunner.

dukethrash
  • 1,449
  • 4
  • 15
  • 25