xjx123
2011-01-25 18:31
浏览 212
已采纳

空指针异常



请各位帮我看看问题到底出在哪里
2011-01-25 10:25:00,296 WARN [org.apache.struts.action.RequestProcessor] - Unhandled Exception thrown: class java.lang.NullPointerException
2011-1-25 10:25:00 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet action threw exception
java.lang.NullPointerException
    at main.com.rpc.servlet.TestLogonAction.doCheckUser(TestLogonAction.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
    at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    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:128)
    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:849)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    at java.lang.Thread.run(Thread.java:595)

Action类

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import main.com.rpc.ajaxservice.AjaxServiceCheckUser;
import main.com.rpc.servlet.pojo.Userconfiginfo;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class TestLogonAction extends DispatchAction {

    private AjaxServiceCheckUser checkUserService;

    public ActionForward doCheckUser(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        boolean flag = false;
        String name = request.getParameter("name");
        Userconfiginfo userConfig = new Userconfiginfo();
        userConfig.setLOGINACCOUNT(name);
        if (!"".equals(name) && name != null) {
            flag = checkUserService.doCheckUser(userConfig);
        }
        return null;
    }

    public AjaxServiceCheckUser getCheckUserService() {
        return checkUserService;
    }

    public void setCheckUserService(AjaxServiceCheckUser checkUserService) {
        this.checkUserService = checkUserService;
    }
}

Service类
package main.com.rpc.ajaxservice;


import main.com.rpc.ajaxdao.AjaxDaoCheckUser;
import main.com.rpc.servlet.pojo.Userconfiginfo;

public class AjaxServiceCheckUser {

    private AjaxDaoCheckUser ajaxdaoCheckUse;

    public boolean doCheckUser(Userconfiginfo config) {
        boolean flag = false;
        try{
            flag = ajaxdaoCheckUse.doCheckUser(config);
        }catch(Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    public AjaxDaoCheckUser getAjaxdaoCheckUse() {
        return ajaxdaoCheckUse;
    }

    public void setAjaxdaoCheckUse(AjaxDaoCheckUser ajaxdaoCheckUse) {
        this.ajaxdaoCheckUse = ajaxdaoCheckUse;
    }
}
Dao类
package main.com.rpc.ajaxdao;

import java.sql.SQLException;
import java.util.List;

import main.com.rpc.servlet.pojo.Userconfiginfo;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class AjaxDaoCheckUser extends HibernateDaoSupport {

    private static final Log log = LogFactory.getLog(AjaxDaoCheckUser.class);
    private Session session = null;
    private Query qr = null;
    private List<Userconfiginfo> list = null;

    public boolean doCheckUser(Userconfiginfo config) throws SQLException {

        boolean flagResult = false;
        String hql = " From Userconfiginfo Where LOGINACCOUNT=?";
        try {
//                      String[] userInfo = new String[2];
            userInfo[0] = config.getLOGINACCOUNT();
            System.out.println(this.getHibernateTemplate());
            List userList = this.getHibernateTemplate().find(hql, userInfo);
            if (userList.size() > 0) {
                flagResult = true;
            }
        } catch (Exception e) {
            log.error("查询名字出错", e);
            throw new SQLException("查询名字出错");
        } finally {
            try {
                if (session != null) {
                    session.close();
                }
            } catch (Exception e) {
                throw new SQLException("查询名字出错");
            }

        }

        return flagResult;
    }

}

struts配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  <form-beans>
    <form-bean name="StuInfoForm" type="main.com.rpc.web.StuInfoForm"></form-bean>
    <form-bean name="userForm" type="main.com.rpc.servlet.UserForm"></form-bean>
  </form-beans>
  <action-mappings>
    <action path="/login" parameter="method" name="StuInfoForm" type="org.springframework.web.struts.DelegatingActionProxy">
        <forward name="query" path="/query.jsp"></forward>
        <forward name="toAdd" path="/add.jsp"></forward>
        <forward name="toModify" path="/modify.jsp"></forward>
    </action>
    
    
    <action
      attribute="userForm"
      input="/ajax.jsp"
      name="userForm"
      parameter="operate"
      path="/user"
      scope="request"
      type="main.com.rpc.servlet.TestLogonAction">

      <forward name="login" path="/login.jsp" />

    </action>
  </action-mappings>
  <controller processorClass="main.com.rpc.web.RequestAction"></controller>
  <message-resources parameter="main.com.rpc.web.ApplicationResources" />
</struts-config>

spring 配置文件
<?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: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-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
    
    <bean id="mydb" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName">
            <value>oracle.jdbc.pool.OracleConnectionPoolDataSource</value>
        </property>
        <property name="url">
            <value>jdbc:oracle:thin:@localhost:1521:orcl</value>
        </property>
        <property name="username">
            <value>leadermis</value>
        </property>
        <property name="password">
            <value>leadermis</value>
        </property>
    </bean>
    <!-- 事务AOP -->
    <!-- 配置sessionFactory 
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation"
            value="/WEB-INF/classes/main/hibernate.cfg.xml">
        </property>
    </bean>
    -->
    <bean id="mySessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="mydb" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>main/com/rpc/persist/user.hbm.xml</value>
                <value>main/com/rpc/servlet/pojo/userconfiginfo.hbm.xml</value>
            </list>
        </property>
    </bean>
    
    <bean id="StuInfoDAO" class="main.com.rpc.persist.StuInfoDAO">
        <property name="sessionFactory">
            <ref bean="mySessionFactory" />
        </property>
    </bean>
    <!-- 实现类 -->
    <bean id="StuInfoHandler" class="main.com.rpc.biz.StuInfoHandlerImpl">
        <property name="stuinfoDao" ref="StuInfoDAO"></property>
    </bean>
    <!-- action -->
    <bean name="/login" class="main.com.rpc.web.StuInfoAction">
        <property name="stuInfoHandler" ref="StuInfoHandler"></property>
    </bean>
    <bean id="ajaxDao" class="main.com.rpc.ajaxdao.AjaxDaoCheckUser">
        <property name="sessionFactory" >
            <ref bean="mySessionFactory"/>
        </property>
    </bean>
    <bean id="ajaxService" class="main.com.rpc.ajaxservice.AjaxServiceCheckUser">
        <property name="ajaxdaoCheckUse" ref="ajaxDao"></property>
    </bean>
    <bean name="/user" class="main.com.rpc.servlet.TestLogonAction">
        <property name="checkUserService" ref="ajaxService"></property>
    </bean> 
    
    
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="mySessionFactory"></property>
    </bean>
    <!-- 配置事务的传播特性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="del*" propagation="REQUIRED"/>
            <tx:method name="modify*" propagation="REQUIRED"/>
            <tx:method name="*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <!-- 哪些方法参与事务 -->
    <aop:config>
        <aop:pointcut id="method" expression="execution(* com.rpc.biz.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="method"/>
    </aop:config>
    
    
</beans>
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • piaoyao1203 2011-01-26 11:55
    已采纳

    可能原因:
    TestLogonAction实例不是从Spring IOC容器中获得的,而是Struts自己创建的。
    配置错误:
    在Struts中配置TestLogonAction的时候type属性应该是org.springframework.web.struts.DelegatingActionProxy

    已采纳该答案
    打赏 评论
  • 王启军 2011-01-26 08:53

    checkUserService

    显然这个没初始化

    打赏 评论
  • piaoyao1203 2011-01-26 11:22

    TestLogonAction 的实例是从Spring IOC容器中获得的吗?

    打赏 评论
  • chenxiang105 2011-01-26 11:46

    问题就在这两个是实例化上面吧。 checkUserService ajaxService

    自己写个测试的,针对这两个service 做实例化, 看问题出在哪里

    有时候遇到这样的问题,一般可以先改下配置文件里面的名字, 名称冲突貌似很常见 看起来傻,有时候太管用了

    打赏 评论

相关推荐 更多相似问题