SSH框架中的有关数据库的问题

我其他没有问题,就是连接数据库可能有问题,但是程序也能初始化成功,但在执行具体sql的时候,不能执行

[b]java.lang.NullPointerException
at com.test.dao.BaseDaoImpl.loadObject(BaseDaoImpl.java:29)
at com.test.service.impl.UserServiceImpl.selectUserByNumber(UserServiceImpl.java:38)
at com.test.struts.action.UserAction.selectUserByNumber(UserAction.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.test.struts.interceptor.LoggerInterceptor.intercept(LoggerInterceptor.java:42)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.TimerInterceptor.invokeUnderTiming(TimerInterceptor.java:124)
at com.opensymphony.xwork2.interceptor.TimerInterceptor.intercept(TimerInterceptor.java:111)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)[/b]

这是我的其中spring和hibernate的配置文件
[code="java"]
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- 开启注解处理器 -->
<context:annotation-config/>

<!-- 定义使用C3P0连接池的数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <!-- 指定连接数据库的JDBC驱动 -->
    <property name="driverClass">
        <value>com.mysql.jdbc.Driver</value>
     </property>
    <!-- 连接数据库所用的URL -->
    <property name="jdbcUrl">
        <value>jdbc:mysql://localhost:3306/softtest?useUnicode=true&amp;characterEncoding=utf-8</value>
    </property>
    <!-- 连接数据库的用户名 -->
    <property name="user">
        <value>root</value>
    </property>
    <!-- 连接数据库的密码 -->
    <property name="password">
        <value>root</value>
    </property>
    <!-- 设置数据库连接池的最大连接数 -->
    <property name="maxPoolSize">
        <value>20</value>
    </property>
    <!-- 设置数据库连接池的最小连接数 -->
    <property name="minPoolSize">
        <value>2</value>
    </property>
    <!-- 设置数据库连接池的初始化连接数 -->
    <property name="initialPoolSize">
        <value>2</value>
    </property>
    <!-- 设置数据库连接池的连接的最大空闲时间,单位为秒 -->
    <property name="maxIdleTime">
        <value>20</value>
    </property>
</bean>

<!-- 定义Hibernate的SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <!-- 依赖注入上面定义的数据源dataSource -->
    <property name="dataSource" ref="dataSource"/>
    <!-- 注册Hibernate的ORM映射文件 -->
    <property name="mappingResources">
        <list>
            <value>com/test/orm/FaultInfo.hbm.xml</value>
            <value>com/test/orm/ModelInfo.hbm.xml</value>
            <value>com/test/orm/ProjectInfo.hbm.xml</value>
            <value>com/test/orm/UserInfo.hbm.xml</value>
            <value>com/test/orm/UserModel.hbm.xml</value>
            <value>com/test/orm/UserFault.hbm.xml</value>
            <value>com/test/orm/UserProject.hbm.xml</value>
            <value>com/test/orm/FaultType.hbm.xml</value>
            <value>com/test/orm/Logger.hbm.xml</value>   
        </list>
    </property>
    <!-- 设置Hibernate的相关属性 -->
    <property name="hibernateProperties">
        <props>
            <!-- 设置Hibernate的数据库方言 -->
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <!-- 设置Hibernate是否在控制台输出SQL语句,开发调试阶段通常设为true -->
            <prop key="show_sql">true</prop>
            <!-- 设置Hibernate一个提交批次中的最大SQL语句数 -->
            <prop key="hibernate.jdbc.batch_size">50</prop>
        </props>
    </property>
</bean>

<!--定义Hibernate的事务管理器HibernateTransactionManager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <!-- 依赖注入上面定义的sessionFactory -->
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- 装配HibernateTemplate实例 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <constructor-arg ref="sessionFactory"/>
</bean>


[/code]

这是我的数据库操作代码
[code="java"]
public Object loadObject(String hql) {
final String hql1 = hql;
Object obj = null;
try {
List list = hibernateTemplate.executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException {
Query query = session.createQuery(hql1);
return query.list();
}
});
if (list.size() > 0) {
obj = list.get(0);
}
} catch (Exception e) {
e.printStackTrace();
}

    return obj;
}

[/code]

herrychen90
herrychen90 我已经解决这个问题了,主要是hibernate中的dtd可能有点问题,我的配置文件的有个格式错了,但却不提示,然后去操作数据库,就失败了
7 年多之前 回复

4个回答

肯定是你的BaseDaoImpl对象的空的,看下你的BaseDaoImpl这个对象是否注入了或者注入是否正确。

java.lang.NullPointerException
at com.test.dao.BaseDaoImpl.loadObject(BaseDaoImpl.java:29)
BaseDaoImpl.java:29 行,看29行是什么?

herrychen90
herrychen90 从29行开始,即: List list = hibernateTemplate.executeFind(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { Query query = session.createQuery(hql1); return query.list(); } });
7 年多之前 回复

[code="java"]




改为这样试试:



[/code]

herrychen90
herrychen90 修改了下,仍然是原来的错误
7 年多之前 回复

看一下 BaseDaoImpl 中的 hibernateTemplate 是否注入过。

herrychen90
herrychen90 注入过了 <bean id="dao" class="com.test.dao.BaseDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"/> </bean>
7 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问