SpringMVC+Mybatis调用后台报空指针异常,代码没有报错,页面也有参数传进来

首先测试接口是不是有错误测试后台接口函数  测试目录放在和application.xml同一级

import com.wfbql.club.dao.IUserDao;
import com.wfbql.club.daomain.IUserLogin;
import com.wfbql.club.service.IUservice;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

//spring整合了junit,同时 只有spring引入外部文件时需要classpath
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class TestDemo1 {

        @Autowired
        private com.wfbql.club.service.IUservice IUservice;

        @Test
        public void test1() throws Exception {
            IUserLogin  t1 = new IUserLogin();
            IUservice.test(t1);
        }


}

发现接口正常查询使用 那么肯定是controller层写错了,定位错误位置

import com.wfbql.club.daomain.IUserLogin;
import com.wfbql.club.service.IUservice;
import com.wfbql.club.service.impl.IUserviceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/user")
public class IUserController {

    
    @Autowired
    private IUservice iUservice;

    @ResponseBody
    @RequestMapping(value ="/login.do",method = RequestMethod.POST)
    public Map<String, Object> Login(HttpServletRequest request,@RequestParam("username") String username,
                                     @RequestParam("password") String password) throws Exception {
        Map<String,Object> map = new HashMap<String, Object>();
        System.out.println(username+"===="+password);
        IUserLogin iuser = new IUserLogin();

        iuser.setUsername(username);
        iuser.setPassword(password);

        
         iUservice =new IUserviceImpl();
         iUservice.test(iuser);
        map.put("json",iuser);
        return map;
    }

}

发现自己写错了   iUservice =new IUserviceImpl();  没有使用@Autowired来生成实现类 ,去掉iUservice =new IUserviceImpl(); 即可正常运行.但是为什么是报的空指针还是不知道,错误的位置反复查看了也没有错.很难去发现.

主要还是需要测试函数来进行排查,确定报错位置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值