Struts检测到一个未处理的异常:java.lang.NullPointerException

时间:2014-02-14 17:02:39

标签: java struts2 struts2-interceptors struts-action interceptorstack

您好我的代码存在此问题,我尝试将文件上传到服务器并保留数据库。

第一个版本是没​​有fileupload的相同代码,并且都运行良好。

struts.xml中

<action name="Crear" class="actions.AccionAnadirCliente">
    <interceptor-ref name="fileUpload">
        <param name="maximunSize">1048576</param>
        <param name="allowedTypes">image/png,image/gif,image/jpg,image/jpeg</param>
    </interceptor-ref>  
    <result name="input">/paginaBienvenida.jsp</result>
    <result name="success">/nuevoCliente.jsp</result>
</action>

现在,执行accionAnadirCliente

public String execute() throws Exception {
    String filePath = "C:\\Users\\alumno\\Desktop\\DWES_JAVA\\imagenes";
    //crea un fichero
    File fileToCreate = new File(filePath, this.fileFileName);
    String rutaCompleta=filePath+ this.fileFileName;
    //volcado de datos.
    FileUtils.copyFile(this.file, fileToCreate);


    Cliente cliente = new Cliente();
    System.out.println(getNombre());

    cliente.setNombre(getNombre()); 
    cliente.setEdad(getEdad());
    cliente.setEmail(getEmail());
    cliente.setTelefono(getTelefono()); 
    cliente.setRuta(rutaCompleta);

    InterfaceClienteDAO clienteDAO = FactoriaDAO.getAlumnoDAO("MySQL");

    clienteDAO.insertarCliente(cliente);    

    return SUCCESS;

}

bean Cliente

private String nombre;
private String email;
private String telefono;
private int edad;
private String ruta;

和getter and setters。

ClienteDAO

public void insertarCliente(Cliente c){

    try {
        db.abrirConexion();
        String query = "insert into cliente values('" + c.getNombre() + "','"
                + c.getEmail() + "','" + c.getTelefono() + "',"
                + c.getEdad() + ","+c.getRuta()+" )";
        db.ejecutarUpdate(query);
        db.commit();
        db.cerrarConexion();

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
}

nuevoCliente.jsp只包含信息。

现在例外。

Struts Problem Report

Struts has detected an unhandled exception:

Messages:   
Stacktraces

java.lang.NullPointerException
    java.io.File.(Unknown Source)
    actions.AccionAnadirCliente.execute(AccionAnadirCliente.java:38)
    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)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252)
    org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:325)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:562)
    org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    java.lang.Thread.run(Unknown Source)
谢谢,伊万。

编辑: 这个例外行是

File fileToCreate = new File(filePath, this.fileFileName);

1 个答案:

答案 0 :(得分:1)

您只包含文件上传拦截器,而您需要包含整个堆栈(默认堆栈或您创建的自定义堆栈):

<action name="Crear" class="actions.AccionAnadirCliente">

    <interceptor-ref name="defaultStack">
        <param name="fileUpload.maximumSize">1048576</param>
        <param name="fileUpload.allowedTypes">
            image/png,image/gif,image/jpg,image/jpeg
        </param>
    </interceptor-ref>  

    <result name="input">/paginaBienvenida.jsp</result>
    <result name="success">/nuevoCliente.jsp</result>
</action>

你的第一个参数中也有一个拼写错误,maximum而不是maximun