hemi9089 2017-08-18 02:51 采纳率: 100%
浏览 2029
已采纳

菜鸟做spring+springMVC+mybatis框架中的多表查询时遇到了空指针问题,求解答

严重: Servlet.service() for servlet [LOGSYS-manager] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
    at com.LOGSYS.service.impl.StuInfoServiceImpl.getStuInfo(StuInfoServiceImpl.java:54)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)

 package com.LOGSYS.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.LOGSYS.common.pojo.EUDataGridResult;
import com.LOGSYS.mapper.ClassInfoMapper;
import com.LOGSYS.mapper.StudentInfoMapper;
import com.LOGSYS.pojo.ClassInfo;
import com.LOGSYS.pojo.ClassInfoExample;
import com.LOGSYS.pojo.ClassInfoExample.Criteria;
import com.LOGSYS.pojo.StuLinkClsVO;
import com.LOGSYS.pojo.StudentInfo;
import com.LOGSYS.pojo.StudentInfoExample;
import com.LOGSYS.service.StuInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 学生管理Service
 * 
 * @ClassName StuInfoServiceImpl
 * @Description 学生管理Service
 * @author chenjunjie
 * @Date 2017年8月15日 下午3:16:22
 * @version 1.0.0
 */
@Service
public class StuInfoServiceImpl implements StuInfoService {

    @Autowired
    private StudentInfoMapper sinfoMapper;
    private ClassInfoMapper cinfoMapper;

    /**
     * 学生列表查询
     */
    @Override
    public EUDataGridResult getStuInfo(int page, int rows) {
        // 查询学生列表
        StudentInfoExample example = new StudentInfoExample();
        ClassInfoExample cExample = new ClassInfoExample();
        // 分页处理
        PageHelper.startPage(page, rows);
        List<StuLinkClsVO> zlist = new ArrayList<>();
        ClassInfo info = new ClassInfo();
        List<StudentInfo> list = sinfoMapper.selectByExample(example);
        for (StudentInfo studentInfo : list) {
            Criteria criteria = cExample.createCriteria();
            criteria.andClsIdEqualTo(studentInfo.getClsId());
            List<ClassInfo> list2 = cinfoMapper.selectByExample(cExample);
            if (list2 != null && list2.size() > 0) {
                info = list2.get(0);
            }
            StuLinkClsVO stuLinkClsVO = new StuLinkClsVO(info.getCname(), studentInfo.getStuId(), studentInfo.getSno(),studentInfo.getSname(), studentInfo.getsGender(), studentInfo.getClsId(), studentInfo.getStuTel(),studentInfo.getsAddress(), studentInfo.getCreated(), studentInfo.getUpdated(),studentInfo.getRole());
            zlist.add(stuLinkClsVO);
        }
        // 创建一个返回对象
        EUDataGridResult dataGridResult = new EUDataGridResult();
        dataGridResult.setRows(zlist);
        // 取记录总条数
        PageInfo<StuLinkClsVO> pinfo = new PageInfo<>(zlist);
        dataGridResult.setTotal(pinfo.getTotal());
        return dataGridResult;
    }

}

上面是错误信息

我在多表查询时候,是用了mybatis逆向工程生成的mapper和pojo代码。

我新建了一个pojo,里面包括学生pojo 和 班级pojo ,我从学生的Service层中调用班级的mapper,通过byexample查数据库,就会报空指针异常。

54行是 List list2 = cinfoMapper.selectByExample(cExample);

我大概想到,可能是在学生Service中不能去调用班级的mapper完成查询。

但是想解决却毫无头绪。

新学的知识很多地方很薄弱,请各位大神稍作解答,万分感谢了。

  • 写回答

2条回答 默认 最新

  • Rains_ 2017-08-18 02:58
    关注
        @Autowired
    private StudentInfoMapper sinfoMapper;
        @Autowired
    private ClassInfoMapper cinfoMapper;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?