用struts2,页面的数据由pojo来收集用在相应的action类中
问题:
    pojo的属性有一个引用类型的对象,这是在pojo在做收集页面数据的时候,就会有nullPointException,
    这是怎么回事,如果说把那个引用类型的去掉,就不会有问题,百思不得其解....

解决方案 »

  1.   

    你自己在bean的构造中处理下其中的引用为空的情况
      

  2.   

    public class FundAccount implements java.io.Serializable { // Fields private Integer fundAccNo;
    private Integer financialAccNo;
    private Integer fundNo;
    private Integer quantity;
    private Double price;
    private Fund funds; public FundAccount() 
    {
    } /** full constructor */
    public FundAccount(Integer financialAccNo,
    Integer quantity, Double price,Integer fundNo) {
    this.financialAccNo = financialAccNo;
    this.fundNo = fundNo;
    this.quantity = quantity;
    this.price = price;
    } // Property accessors public Integer getFundAccNo() {
    return this.fundAccNo;
    } public void setFundAccNo(Integer fundAccNo) {
    this.fundAccNo = fundAccNo;
    } public Integer getFinancialAccNo() {
    return this.financialAccNo;
    } public void setFinancialAccNo(Integer financialAccNo) {
    this.financialAccNo = financialAccNo;
    } public Integer getFundNo() {
    return this.fundNo;
    } public void setFundNo(Integer fundNo) {
    this.fundNo = fundNo;
    } public Integer getQuantity() {
    return this.quantity;
    } public void setQuantity(Integer quantity) {
    this.quantity = quantity;
    } public Double getPrice() {
    return this.price;
    } public void setPrice(Double price) {
    this.price = price;
    } public Fund getFunds() {
    return funds;
    } public void setFunds(Fund funds) {
    this.funds = funds;
    }去掉public void setFunds(Fund funds) {
    this.funds = funds;
    }
    就不会有问题,但是这个不能去掉,其它代码还有用到这个
      

  3.   

    那要看你表单中的字段是什么啦,fundAccount.funds.id// 表单字段是类似这样吗.
    如果不是,你可以试下
      

  4.   

    肯定是这样的啊,去掉public void setFunds(Fund funds) { 
    this.funds = funds; 

    就不会有问题,但是这个不能去掉,其它代码还有用到这个 
      

  5.   

    发现了只要是继承java.io.Serializable的JAVABEAN都不能在POJO里有set方法,要不然就会nullpoint,还是不能理解啊
      

  6.   

    <s:form action="fundAccount!insert" method="post">
    <table width="407" height="227" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#F2F2F2" bordercolor="#E6E6E6">
        <s:select name="fundAccount.fundNo" list="#request.listFund" listKey="fundNo" listValue="fundName" label="上市基金"/>
        <s:textfield name="fundAccount.quantity" label="购买数量"/>
        <s:textfield name="fundAccount.price"  label="当前价格"/>
        <s:textfield name="financialAccount.accountNo" label="资金帐户"/>
        <s:textfield name="financialAccount.password" label="资金帐户密码"/>
        <s:submit value="购买" /><s:reset value="重置"/>
     
    </table>
    </s:form>
      

  7.   

    你既然单独写了POJO,为什么我觉得你不需要使用模型驱动,struts2中推荐使用属性驱动的方式
    以我的想法,你的POJO不用改变,二Form和Action写成如下姓石应该没有问题package org.spring.hibernate;public class FundAccountAction extends ActionSupport {
    private Integer fundAccNo; 
    private Integer financialAccNo; 
    private Integer fundNo; 
    private Integer quantity; 
    private Double price;

    public String insert()throws Exception{
    FundAccount fa = new FundAccount();
    fa.setFundAccNo = fundAccNo;
    .......
    }

    public Integer getFundAccNo() {
    return fundAccNo;
    }
    public void setFundAccNo(Integer fundAccNo) {
    this.fundAccNo = fundAccNo;
    }
    public Integer getFinancialAccNo() {
    return financialAccNo;
    }
    public void setFinancialAccNo(Integer financialAccNo) {
    this.financialAccNo = financialAccNo;
    }
    public Integer getFundNo() {
    return fundNo;
    }
    public void setFundNo(Integer fundNo) {
    this.fundNo = fundNo;
    }
    public Integer getQuantity() {
    return quantity;
    }
    public void setQuantity(Integer quantity) {
    this.quantity = quantity;
    }
    public Double getPrice() {
    return price;
    }
    public void setPrice(Double price) {
    this.price = price;

    }<s:form action="fundAccount!insert" method="post"> 
    <table width="407" height="227" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#F2F2F2" bordercolor="#E6E6E6"> 
        <s:select name="fundNo" list="#request.listFund" listKey="fundNo" listValue="fundName" label="上市基金"/> 
        <s:textfield name="quantity" label="购买数量"/> 
        <s:textfield name="price"  label="当前价格"/> 
        <s:textfield name="accountNo" label="资金帐户"/> 
        <s:textfield name="password" label="资金帐户密码"/> 
        <s:submit value="购买" /> <s:reset value="重置"/> </table> 
    </s:form>