SaveOfficeAction.java出错:java.lang.NullPointerExceptionpackage we.app.action;import we.app.data.*;import com.opensymphony.xwork2.ActionSupport;public class SaveOfficeAction extends ActionSupport {
    private IOffice B_Office;
    private IOfficeDAO B_OfficeDAO;
    private String Tips;    public IOffice getB_Office() {
        return B_Office;
    }    public void setB_Office(IOffice b_Office) {
        B_Office = b_Office;
    }    public IOfficeDAO getB_OfficeDAO() {
        return B_OfficeDAO;
    }    public void setB_OfficeDAO(IOfficeDAO b_OfficeDAO) {
        B_OfficeDAO = b_OfficeDAO;
    }    public String getTips() {
        return Tips;
    }    public void setTips(String tips) {
        Tips = tips;
    }    public String execute() throws Exception
    {
        this.Tips=this.getB_OfficeDAO().findById(1).getOfficename().toString();
        //this.Trans_OfficeDAO.save(this.B_Office);
        return SUCCESS;
    }
}
1、index.jsp:   <%@taglib uri="/struts-tags" prefix="s"%><%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>  <body>
    <s:form action="SaveOffice">
        <s:textfield name="B_Office.officename" label="officename"></s:textfield>
        <s:submit></s:submit>
    </s:form>
    <br>
  </body>
</html>
2、hibernate.cfg.xml:   <?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>    <session-factory>
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
        <property name="connection.url">
            jdbc:jtds:sqlserver://192.168.0.100:4628/tnew
        </property>
        <property name="connection.username">sa</property>
        <property name="connection.password">111</property>
        <property name="connection.driver_class">
            net.sourceforge.jtds.jdbc.Driver
        </property>
        <property name="myeclipse.connection.profile">
            sqlserverjtds
        </property>
        <mapping resource="we/app/data/Peop.hbm.xml" />
        <mapping resource="we/app/data/Office.hbm.xml" />    </session-factory></hibernate-configuration>
3、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:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"
    >
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation"
            value="classpath:hibernate.cfg.xml">
        </property>
    </bean>    <bean id="PeopDAO" class="we.app.data.PeopDAO">
            <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <bean id="B_OfficeDAO" class="we.app.data.OfficeDAO">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>    <bean id="B_Office" class="we.app.data.Office"></bean>
    <bean id="B_Peop" class="we.app.data.Peop"></bean>
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
       <property name="sessionFactory">
         <ref bean="sessionFactory"/>
       </property>
    </bean><bean id="Trans_OfficeDAO"
  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager">
   <ref bean="transactionManager" />
  </property>
  <property name="target">
   <ref local="B_OfficeDAO" />
  </property>
  <property name="transactionAttributes">
   <props>
    <prop key="*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
 </bean>
</beans>
4、package we.app.data;import java.util.List;public interface IOfficeDAO {    // property constants
    public static final String OFFICENAME = "officename";    public abstract void save(IOffice transientInstance);    public abstract void delete(IOffice persistentInstance);    public abstract IOffice findById(java.lang.Integer id);    public abstract List findByExample(IOffice instance);    public abstract List findByProperty(String propertyName, Object value);    public abstract List findByOfficename(Object officename);    public abstract List findAll();    public abstract IOffice merge(Office detachedInstance);    public abstract void attachDirty(IOffice instance);    public abstract void attachClean(IOffice instance);}

解决方案 »

  1.   

    public class SaveOfficeAction extends ActionSupport {
        private IOffice B_Office;
        private IOfficeDAO B_OfficeDAO=new IOfficeDAO();
        private String Tips;
    这样试试!!
      

  2.   

    你一定得看看是什么异常。
    在那一行,找出那一行的为NULL对象问题就解决了一半,然后,你再想想,你可能没有给它注入值,或注入值,可能没有注入对。
      

  3.   

    private IOffice B_Office;
    private IOfficeDAO B_OfficeDAO;
    private String Tips;spring注入的属性格式有要求。 B_Office首字母要小写,写成b_office比较好。其他的一样