2013-04-18 57 views
1

我正在使用struts2應用程序,我必須上傳文件並將其保存到數據庫中。 卻是露出NullPointerException異常我的代碼,每次在這裏NullPointerException在struts2上的文件上傳異常

堆棧跟蹤是 -

java.lang.NullPointerException 
    dao.Clientdao.registration(Clientdao.java:31) 
    action.client.Clientaction.execute(Clientaction.java:18) 
    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:452) 
    com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254) 
    com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176) 
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263) 
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) 
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207) 
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:314) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187) 
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 
    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52) 
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485) 
    org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) 
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91) 

模型類是

import java.io.File; 
public class Clientmodel { 
private String Aggrement; 
private String date; 
private String rate; 
private String maturitydate; 
private String detail; 
private String taxno; 
private File imagefile; 
public File getImagefile() { 
    return imagefile; 
} 

public void setImagefile(File imagefile) { 
    this.imagefile = imagefile; 
} 

public String getRate() { 
    return rate; 
} 

public void setRate(String rate) { 
    this.rate = rate; 
} 

public String getDetail() { 
    return detail; 
} 

public void setDetail(String detail) { 
    this.detail = detail; 
} 

public String getTaxno() { 
    return taxno; 
} 

public void setTaxno(String taxno) { 
    this.taxno = taxno; 
} 

public String getAggrement() { 
    return Aggrement; 
} 

public void setAggrement(String aggrement) { 
    Aggrement = aggrement; 
} 



public String getMaturitydate() { 
    return maturitydate; 
} 

public void setMaturitydate(String maturitydate) { 
    this.maturitydate = maturitydate; 
} 

public String getDate() { 
    return date; 
} 

public void setDate(String date) { 
    this.date = date; 
} 

} 

struts.xml的

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 
    "http://struts.apache.org/dtds/struts-2.3.dtd"> 
<struts> 
     <constant name="struts.enable.DynamicMethodInvocation" value="true"/> 
      <constant name="struts.devmode" value="true"/> 
      <package name="loginmodel" extends ="struts-default"> 

     <action name="clientlogin" class="action.client.Clientaction" > 
      <interceptor-ref name="exception"/> 
      <interceptor-ref name="i18n"/> 
      <interceptor-ref name="fileUpload"> 
      <param name="allowedTypes">text/plain</param> 
      <param name="maximumSize">10240</param> 
     </interceptor-ref> 
      <interceptor-ref name="params"> 
       <param name="excludeParams">dojo\..*,^struts\..*</param> 
      </interceptor-ref> 
      <interceptor-ref name="validation"> 
       <param name="excludeMethods">input,back,cancel,browse</param> 
      </interceptor-ref> 
      <interceptor-ref name="workflow"> 
       <param name="excludeMethods">input,back,cancel,browse</param> 
      </interceptor-ref> 

      <result name="input">/Registration/clientregistration.jsp </result> 
      <result name ="success">/Registration/success.jsp </result> 

      </action> 

     </package> 
</struts> 

clientregistration.jsp

<%@ page language ="java" contentType ="text/html; charset=ISO-8859-1" pageEncoding ="ISO-8859-1"%> 
<%@ taglib uri ="/struts-tags" prefix="s"%> 
<%@ taglib uri ="/struts-dojo-tags" prefix="sx" %> 
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%> 
<html> 
<head> 
<sj:head jqueryui="true" jquerytheme="redmond" /> 
<style> 
    div.ui-datepicker { 
     font-size: 82.5%; 
     } 
</style> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>CLIENT</title> 
<link rel="stylesheet" type="text/css" href="css/emp.css"> 
</head> 
<body > 

<div id ="did" align="center"> <h1 style="color: red"> CLIENT REGISTRATION FORM</h1> 
    <s:form action ="clientlogin" method="post" enctype="multipart/form-data"> 

<s:select name="Aggrement" label ="Type of Aggrement" list ="{'ORAL', 'written'} "/> 
<sj:datepicker name="date" label="Aggrement DATE" changeMonth="true" changeYear="true"/> 

<s:select name ="rate" list="{'1','2','3'}" label ="Aggrement rate"/> 
<s:textarea name ="detail" label ="Aggrement Detail"></s:textarea> 
<s:textfield name ="taxno" label="Tax No "></s:textfield> 
<s:file name ="imagefile" label ="Upload Document" /> 
<tr><td>&nbsp;</td></tr> 
     <tr> 
    <td>&nbsp;</td> 

<s:submit align="center"></s:submit> 
</s:form> 
    </div> 
    </body> 
</html> 

客戶DAO是

package dao; 

import java.sql.Blob; 
import java.sql.SQLException; 
import model.Clientmodel; 
import com.mysql.jdbc.PreparedStatement; 
import com.mysql.jdbc.ResultSet; 

public class Clientdao { 

java.sql.Connection connection =null; 
PreparedStatement pst = null; 
ResultSet rs =null; 

private java.sql.Connection getConnection() throws SQLException{ 
    java.sql.Connection conn; 
conn = Connectionfactory.getInstance().getConntection();  
return conn; 

} 

public int registration(Clientmodel model){ 

    int i= 0; 
    try{ 
     String str = "insert into " + "clientdata(Aggrement,aggrementDate,rate,maturitydate,detail,taxno,image)"+ 
        "values(?,?,?,?,?,?)"; 

     connection =getConnection(); 

    pst =(PreparedStatement) connection.prepareStatement(str); 
    pst.setString(1, model.getAggrement()); 
    pst.setString(2, model.getDate()); 
    pst.setString(3, model.getRate()); 
    pst.setString(4, model.getMaturitydate()); 
    pst.setString(5, model.getDetail()); 
    pst.setString(6, model.getTaxno()); 
    pst.setBlob(7,(Blob) model.getImagefile());  

    i = pst.executeUpdate(); 
    System.out.println("Result " +i); 
    } 
    catch (SQLException se){ 
    se.printStackTrace(); 
    } 
    return i; 

} 
} 
+0

異常在哪裏? –

+0

是'model' null? –

+0

和你的動作課? –

回答

1

落實模型驅動接口,並在你的Action類重寫的方法的對象返回到你的模型類。

相關問題