【问题标题】:NullPointerException when using Autowired in JSF [duplicate]在 JSF 中使用 Autowired 时出现 NullPointerException [重复]
【发布时间】:2017-11-08 01:32:36
【问题描述】:

我正在使用 Spring-boot、JSF 和 MyBatis。问题是当我尝试使用 MyBatis 映射器时收到 NullPointerException。 MyBatis 连接工作正常,我已经在 Autowired 的单元测试中对其进行了测试(与我在 Service 类中的操作方式相同)。

这是我的 JSF 控制器:

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

}

如果我使用 Autowired 注释,为什么 weatherMapper 为空?

【问题讨论】:

    标签: spring jsf


    【解决方案1】:

    你应该检查“weatherMapper”类,我认为查询在那里返回空值。

    【讨论】:

    • 感谢您的评论,但如果查询结果为 null,则当我在控制器上使用 getWheather 方法的返回值时会抛出异常。
    猜你喜欢
    • 2017-08-09
    • 2022-01-21
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    相关资源
    最近更新 更多