/** * 查询,判断是否已登录 */ public List<Course> query() { if (teaAuthority == null) { System.out.println("对不起,您还没有登录,请先登录再去操作数据"); } else { return teaAuthority.getAllCourse(); } return null; } /** * 判断是否已登录,如果没有登录,请先登录 * * @param courseType * @return 两个方法:叫做方法重载 */ public List<Course> query(String courseType) { if (teaAuthority == null) { System.out.println("对不起,您还没有登录,请先登录再去操作数据"); } else { return teaAuthority.getCourseByType(courseType); } return null; }
时间: 2024-02-14 16:26:02 浏览: 29
根据你提供的代码,空指针异常可能是由于 teaAuthority 对象为空引起的。在 showList 方法中调用了 role.query() 方法,而在 query 方法中,如果 teaAuthority 为空,会打印一条错误信息,但没有返回任何值。因此,在 showList 方法中,如果 query 方法返回 null,就会出现空指针异常。
你可以在 query 方法中,将错误信息改为返回一个空的课程列表,而不是 null。这样,在 showList 方法中就不会出现空指针异常。修改后的代码如下所示:
```java
public List<Course> query() {
if (teaAuthority == null) {
System.out.println("对不起,您还没有登录,请先登录再去操作数据");
return new ArrayList<>(); // 返回一个空的课程列表
} else {
return teaAuthority.getAllCourse();
}
}
public List<Course> query(String courseType) {
if (teaAuthority == null) {
System.out.println("对不起,您还没有登录,请先登录再去操作数据");
return new ArrayList<>(); // 返回一个空的课程列表
} else {
return teaAuthority.getCourseByType(courseType);
}
}
```
通过以上修改,你应该能够解决空指针异常问题。如果问题仍然存在,请提供更多相关代码以便我更好地帮助你。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![text/x-java](https://img-home.csdnimg.cn/images/20210720083646.png)
![unitypackage](https://img-home.csdnimg.cn/images/20210720083646.png)