struts+spring+jpa NullPointerException 竟然找不出

一个很简单的问题,我却被困住了,各位高手,看看是什么问题,检查了很多遍,但问题还是没有解决
先谢谢大家了..
------------NullPointerException description------------
root cause

java.lang.NullPointerException
com.edshop.web.action.product.BrandManagerAction.add(BrandManagerAction.java:49)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:269)
org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:170)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
com.edshop.web.filter.SetCodeFilter.doFilter(SetCodeFilter.java:22)
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:112)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
-------------formbean--------------------
public class BrandForm extends BaseForm {

private String name;
private FormFile formFile;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public FormFile getFormFile() {
    return formFile;
}

public void setFormFile(FormFile formFile) {
    this.formFile = formFile;
}

}
------------entity----------------
@Entity
public class Brand implements Serializable{
/** 品牌ID(UUID) /
@Id @Column(length=36)
private String code;
/
品牌名称 /
@Column(length=40,nullable=false)
private String name;
/
是否可见 /
@Column(nullable=false)
private Boolean visible=true;
/
logo路径 **/
@Column(length=80)
private String logopath;

public Brand(){}

public Brand(String logopath, String name) {
    super();
    this.logopath = logopath;
    this.name = name;
}

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Boolean getVisible() {
    return visible;
}

public void setVisible(Boolean visible) {
    this.visible = visible;
}

public String getLogopath() {
    return logopath;
}

public void setLogopath(String logopath) {
    this.logopath = logopath;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((code == null) ? 0 : code.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Brand other = (Brand) obj;
    if (code == null) {
        if (other.code != null)
            return false;
    } else if (!code.equals(other.code))
        return false;
    return true;
}

}
---------------action--------------

public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
BrandForm formbean = (BrandForm) form;
Brand brand = new Brand();
brand.setName(formbean.getName());
//上传文件
if (formbean.getFormFile() != null && formbean.getFormFile().getFileSize() > 0) {
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy/MM/dd/HH");//格式化日期
//logo保存文件路径的格式如:/images/brand/yyyy/MM/dd/HH/1234566.jpg
String logopathdir = "/images/brand/" + dateformat.format(new Date());
//得到图片保存的目录的真实路径
String logorealpathdir = request.getSession().getServletContext().getRealPath(logopathdir);
File logosavedir = new File(logopathdir);

        if(!logosavedir.exists())//如果目录不存在就创建
            logosavedir.mkdirs();
        String ext = formbean.getFormFile().getFileName().substring(formbean.getFormFile().getFileName().lastIndexOf('.'));
        String imagemane = UUID.randomUUID().toString()+ext;//构建文件名称
        FileOutputStream out = new FileOutputStream(new File(logorealpathdir,imagemane));
        out.write(formbean.getFormFile().getFileData());
        out.close();
        String logopath =logopathdir+imagemane;
        brand.setLogopath(logopath);
    }
    brandService.save(brand);

}
----------------web.xml的部分配置----------------

org.springframework.web.context.ContextLoaderListener

<filter>
    <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

1个回答

估计问题就处在你保存文件的路径上:
[code="java"]
//logo保存文件路径的格式如:/images/brand/yyyy/MM/dd/HH/1234566.jpg
String logopathdir = "/images/brand/" + dateformat.format(new Date());
//得到图片保存的目录的真实路径
String logorealpathdir = request.getSession().getServletContext().getRealPath(logopathdir);
[/code]
在地址字符串中 关于这些特殊的转义字符的输入方式 相信不用我再提醒你了吧

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问