调用HibernateTemplate.save()等方法报空指针

时间:2023-01-02 22:17:25
测试main方法
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
GoodsService goodsService = ac.getBean(GoodsServiceImpl.class);
Goods goods = goodsService.findGoodsById(2);
System.out.println(goods.getGoodname());
}

applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/aop 
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  http://www.springframework.org/schema/tx 
  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
 
 <import resource="applicationContext-user.xml"/>
 
  <!-- 加载资源文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:dbconfig.properties</value>
</list>
</property>
</bean>
<!-- 不使用连接池时, DriverManagerDataSoruce -->
<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driverclass}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="ds"></property><!-- 配置hibernate数据库信息 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.OracleDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>

<!-- 1配置所有的映射文件 -->
<!-- 
<property name="mappingResources">
<list>
<value>com/zrgk/model/User.hbm.xml</value>
</list>
</property> -->

<!-- 2只需要配置映射文件的路径,spring会自动加载
    路径下的所有映射文件 -->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:com/zrgk/model</value>
</list>
</property>
</bean>
<!-- 给模板注入数据库类型,方言,及其他 -->
<bean id="ht" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<bean id="baseDao" class="com.zrgk.dao.impl.BaseDaoImpl" abstract="true">
<property name="ht" ref="ht"></property>
</bean>
</beans>

错误信息
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.NullPointerException
at com.zrgk.order.dao.impl.BaseDaoImpl.findById(BaseDaoImpl.java:29)
at com.zrgk.order.service.impl.GoodsServiceImpl.findGoodsById(GoodsServiceImpl.java:21)
at util.Test1.main(Test1.java:15)

1 个解决方案

#1


通过断点debug下BaseDaoImpl.java文件29行,看下哪个引用为空了

#1


通过断点debug下BaseDaoImpl.java文件29行,看下哪个引用为空了