日期:2014-05-19  浏览次数:20909 次

DTO传值问题
大家帮忙看一下System.out.println(regvo.getUsername());能够打印出来jsp页面传的值
但是当把值传给dto的时候出现空指针异常,为何?
Java code
package com.action;

import com.dto.RegDTO;
import com.opensymphony.xwork2.ActionSupport;
import com.service.RegService;
import com.vo.RegVO;

import freemarker.template.utility.Execute;

public class RegAction extends ActionSupport {

    private RegVO regvo;
    private RegDTO regdto;
    private RegService regservice;

    public String execute() throws Exception {
        // 首先将VO的值传递给DTO
        System.out.println(regvo.getUsername());
        this.populateDTO();
        regservice.RegUser();
        return SUCCESS;
    }

    public void populateDTO() {
        regdto.setUsername(regvo.getUsername());
        regdto.setPassword(regvo.getPassword());
    }

    public RegVO getRegvo() {
        return regvo;
    }

    public void setRegvo(RegVO regvo) {
        this.regvo = regvo;
    }

    public RegDTO getRegdto() {
        return regdto;
    }

    public void setRegdto(RegDTO regdto) {
        this.regdto = regdto;
    }

    public RegService getRegservice() {
        return regservice;
    }

    public void setRegservice(RegService regservice) {
        this.regservice = regservice;
    }

    public static void main(String[] args) throws Exception {
        RegAction sa = new RegAction();
        sa.execute();
    }

}


异常语句this.populateDTO();
异常代码
Java code
java.lang.NullPointerException
    com.action.RegAction.populateDTO(RegAction.java:25)
    com.action.RegAction.execute(RegAction.java:19)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)



------解决方案--------------------
DTO对象在拿来用之前需要初始化不?