'What can cause a null pointer exception in my class listener using an autowired dao? [duplicate]

While trying to execute a delete query on registers of the class Alumno, on a many to one relationship with the class Calificacion I get null pointer exception on the autowired repository.

public class AlumnoListener {

    @Autowired
    CalificacionRepositorio calificacionDAO;
    
    @PreRemove
    public void borradoAlumno(Alumno alumno) {
        
        List<Calificacion> list = (List<Calificacion>) calificacionDAO.findAll();

        list.stream().filter(c -> c.getAlumno().equals(alumno)).forEach(calificacionDAO::delete);
    }
}



Solution 1:[1]

You probably have to create a bean in your applicationContext.xml

<bean id="CalificacionRepositorio" class="com.example.CalificacionRepositorio" autowire="byName"></bean>

Or programmatically in a @Configuration annotated class within your ComponentScan package

@Bean
@Lazy
public CalificacionRepositorio configuraCalificacionRepositorio() {
    return new CalificacionRepositorio();
}

If you have already registered your bean in the application context make sure that the variable name matches the class name. Try replacing calificacionDAO with calificacionRepositorio

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RicardoE