勿忘勿念 2015-11-04 09:00 采纳率: 0%
浏览 4999
已结题

mybatis dao层空指针异常。

严重: Servlet.service() for servlet [springmvc] in context with path [/fourth] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at com.ssm.fourth.service.MapServiceImpl.selectAll(MapServiceImpl.java:20)
at com.ssm.fourth.controller.MapController.second(MapController.java:23)

下面是具体配置 和代码   跪求解决方法 谢谢了


web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>



springmvc
org.springframework.web.servlet.DispatcherServlet

contextConfigLocation
classpath:springmvc.xml

1


springmvc
*.action


org.springframework.web.context.ContextLoaderListener

applicationContext.xml
<context:component-scan 
base-package="com.ssm.fourth.service,com.ssm.fourth.dao"></context:component-scan>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:jdbc.properties"/>
</bean>

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> 

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    <property name="driverClassName" value="${driver}"/>
    <property name="url" value="${url}"/>
    <property name="username" value="${username}"/>
    <property name="password" value="${password}"/>

    <!-- 初始化连接池大小 -->
    <property name="initialSize" value="${initialSize}"></property>
    <!-- 连接池的最大数量 -->
    <property name="maxActive" value="${maxActive}"></property>
    <!-- 连接池的最大空闲 -->
    <property name="maxIdle" value="${maxIdle}"></property>
    <!-- 连接池的最小空闲 -->
    <property name="minIdle" value="${minIdle}"></property>
    <!-- 连接池的最大等待时间 -->
    <!-- <property name="maxWaite" value="${maxWaite}"></property> -->
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation" value="classpath:mybaties/SqlMapConfig.xml"/>
    <!-- 自动扫描mapping.xml文件 -->
    <property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>


springmvc.xml
<mvc:annotation-driven/>
    <context:component-scan base-package="com.ssm.fourth.controller"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsps/"/>
    <property name="suffix" value=".jsp"/>
</bean>


代码:
package com.ssm.fourth.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ssm.fourth.entity.Map;
import com.ssm.fourth.service.MapService;

@Controller
@RequestMapping(value="b")
public class MapController {

@Autowired
private MapService mapService;


@RequestMapping(value="second.action")
public String second(){
    System.out.println("second");
    List<Map> list = mapService.selectAll();
    for(int i=0;i < list.size();i++){
        System.out.println(list.get(i).getLatitude());
    }
    return "second";
}

}

package com.ssm.fourth.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.ssm.fourth.entity.Map;

@Repository
public interface MapDao {

public List<Map> selectAll();

}

package com.ssm.fourth.service;

import java.util.List;

import com.ssm.fourth.entity.Map;

public interface MapService {

public List<Map> selectAll();

}

package com.ssm.fourth.service;

import java.util.List;

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

import com.ssm.fourth.dao.MapDao;
import com.ssm.fourth.entity.Map;

@Service
public class MapServiceImpl implements MapService{

@Qualifier("mapDao")
private MapDao mapDao;

@Override
public List<Map> selectAll() {
    return this.mapDao.selectAll();
}

}

  • 写回答

4条回答 默认 最新

  • Xiuye_XY 2015-11-04 10:00
    关注
    I don't konw what it is! MyBatis's Exception: I encontered the problem of "NullPointerException" of "SqlSession" .Who caused this?
    The Main bad woman is:
    In The MyBatis Configuration's file(SqlMaoConfig.xml) :
    
    <environments default="environment">  
        <environment id="environment">  
    

    The "default" and "id" may be the same name.Or maybe NullPointerException.OK ,this is my problem and I solve that through my hands by
    searching the internet. Maybe it help you.

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决