2 sunrealonetwo sunrealonetwo 于 2013.05.11 18:14 提问

学习用MyEclipse 2013开发SSH项目遇到java.lang.NullPointerException问题,请帮助!

SaveOfficeAction.java出错:java.lang.NullPointerException

package 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);

}

2个回答

ht314495132
ht314495132   2013.05.11 21:52

你好歹把出错信息贴出来啊

suannai0314
suannai0314 你这个可以直接在问题下边添加评论
4 年多之前 回复
u010122579
u010122579   2013.05.12 13:49

this.Tips=this.getB_OfficeDAO().findById(1).getOfficename().toString();中的B_officeDao是不是没有在你的action里面注入,也木有申明;要不你干脆new 一个B_officeDao在试试,要不你再你的Application.xml配置文件中加上一条

Csdn user default icon
上传中...
上传图片
插入图片