0

I am using Spring-boot, JSF and MyBatis. The problem is that I am getting a NullPointerException when I try to use MyBatis mapper. The MyBatis connection is working OK, I have tested it on my unit tests with Autowired (the same way I am doing it in the Service class).

This is my JSF controller:

@ManagedBean
@ViewScoped
public class WeatherView implements Serializable {

    private List<Weather> weathers;

    @ManagedProperty(value = WeatherService.EL_NAME)
    private WeatherService weatherService;

    @PostConstruct
    public void init(){
        setWeathers(this.weatherService.getAll());
    }

    public List<Weather> getWeathers() {
        return this.weathers;
    }

    public void setWeathers(List<Weather> weathers) {
        this.weathers = weathers;
    }

    public void setWeatherService(WeatherService weatherService) {
        this.weatherService = weatherService;
    }

WeatherService.java

@Service(WeatherService.BEAN_NAME)
@ManagedBean(name = WeatherService.BEAN_NAME)
@ApplicationScoped
public class WeatherService {

    public static final String BEAN_NAME = "weatherService";

    public static final String EL_NAME = "#{weatherService}";

    @Autowired
    private WeatherMapper weatherMapper; // <-- This is the MyBatis Mapper

    // The NullPointerExceptions is thrown here
    public List<Weather> getAll(){
        return weatherMapper.getAll();
    }

}

Why weatherMapper is null if I am using Autowired annotation?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

1 Answers1

-1

you should check in "weatherMapper" class, I think the query is returning null there.

ovi
  • 30
  • 1
  • 6
  • Thanks for your comment, but if the query is resulting null, the Exception will throw when I use the return value of getWheather method at the controller. – Ismael Uribe Nov 08 '17 at 02:24