问题描述
packagecom.pb.web.action;importjava.io.File;importcom.Service.IHouseService;importcom.opensymphony.xwork2.ActionSupport;importcom.pb.entity.House;importcom.pb.entity.Upload;importcom.serviceimpl.HouseServiceImpl;publicclassHouseActionextendsActionSupport{privateIHouseServiceservice=newHouseServiceImpl();privateFileimg;privateStringimgContentType;privateStringimgFileName;privateUploadupload;privateHousehouse;publicStringdoAddHouse(){if(img!=null){upload.setImgfile(img);upload.setFileName(imgFileName);}if(service.save(house,upload)){System.out.println("chenggong");returnSUCCESS;}returnINPUT;}publicHousegetHouse(){returnhouse;}publicvoidsetHouse(Househouse){this.house=house;}publicUploadgetUpload(){returnupload;}publicvoidsetUpload(Uploadupload){this.upload=upload;}publicFilegetImg(){returnimg;}publicvoidsetImg(Fileimg){this.img=img;}publicStringgetImgContentType(){returnimgContentType;}publicvoidsetImgContentType(StringimgContentType){this.imgContentType=imgContentType;}publicStringgetImgFileName(){returnimgFileName;}publicvoidsetImgFileName(StringimgFileName){this.imgFileName=imgFileName;}publicStringgotoAddHouse(){returnSUCCESS;}}
packagecom.serviceimpl;importjava.io.File;importorg.apache.commons.io.FileUtils;importcom.Service.IHouseService;importcom.dao.HouseDaoImpl;importcom.dao.IHouseDao;importcom.pb.entity.House;importcom.pb.entity.HousePicture;importcom.pb.entity.Upload;publicclassHouseServiceImplimplementsIHouseService{privateIHouseDaodao=newHouseDaoImpl();publicbooleansave(Househouse,Uploadfile){try{StringfileName=file.getFileName("logo.gif");System.out.println(fileName);if(file!=null&&upLoad(file,fileName)){HousePicturepicture=newHousePicture();picture.setUrl(fileName);picture.setTitle(file.getTitle());house.setPicture(picture);}dao.save(house);returntrue;}catch(Exceptione){e.printStackTrace();returnfalse;}}publicbooleanupLoad(UploaduploadFile,StringfileName){if(uploadFile!=null&&uploadFile.getImgfile()!=null){try{Stringpath=uploadFile.getPath();System.out.println(path);Filefile=newFile(path+uploadFile.getFileName());if(!file.getParentFile().exists()){file.getParentFile().mkdirs();}FileUtils.copyFile(uploadFile.getImgfile(),file);returntrue;}catch(Exceptione){e.printStackTrace();returnfalse;}}returnfalse;}}
packagecom.serviceimpl;importjava.io.File;importorg.apache.commons.io.FileUtils;importcom.Service.IHouseService;importcom.dao.HouseDaoImpl;importcom.dao.IHouseDao;importcom.pb.entity.House;importcom.pb.entity.HousePicture;importcom.pb.entity.Upload;publicclassHouseServiceImplimplementsIHouseService{privateIHouseDaodao=newHouseDaoImpl();publicbooleansave(Househouse,Uploadfile){try{StringfileName=file.getFileName("logo.gif");System.out.println(fileName);if(file!=null&&upLoad(file,fileName)){HousePicturepicture=newHousePicture();picture.setUrl(fileName);picture.setTitle(file.getTitle());house.setPicture(picture);}dao.save(house);returntrue;}catch(Exceptione){e.printStackTrace();returnfalse;}}publicbooleanupLoad(UploaduploadFile,StringfileName){if(uploadFile!=null&&uploadFile.getImgfile()!=null){try{Stringpath=uploadFile.getPath();System.out.println(path);Filefile=newFile(path+uploadFile.getFileName());if(!file.getParentFile().exists()){file.getParentFile().mkdirs();}FileUtils.copyFile(uploadFile.getImgfile(),file);returntrue;}catch(Exceptione){e.printStackTrace();returnfalse;}}returnfalse;}}
<%@pagelanguage="java"contentType="text/html;charset=utf-8"pageEncoding="utf-8"%><%@taglibprefix="s"uri="/struts-tags"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"><title>Inserttitlehere</title></head><body><formname="form"method="post"action="doUpload"enctype="multipart/form-data"><div><table><tr><tdclass="filed">上传图片</td><td><s:fileid="img"name="img"cssClass="FileSty"cssStyle="width:280"></s:file><br/><fontcolor="red"><s:fielderrorfieldName="img"/></font></td></tr><tr><tdclass="field">图片标题</td><td><s:textfieldname="upLoadFile.title"cssClass="text"></s:textfield></td></tr></table></div><inputtype="submit"name="submit"value="走上传图片去"></form></body></html>
<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><constantname="struts.i18n.encoding"value="UTF-8"/><constantname="struts.ui.theme"value="simple"/><constantname="struts.enable.DynamicMethodInvocation"value="true"/><packagename="house"namespace="/"extends="struts-default"><actionname="doUpload"class="com.pb.web.action.HouseAction"method="doAddHouse"><result>/success.jsp</result><resultname="input">/error.jsp</result></action></package></struts>
提示错误信息是java.lang.NullPointerExceptioncom.pb.web.action.HouseAction.doAddHouse(HouseAction.java:23)sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)java.lang.reflect.Method.invoke(Method.java:597)com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:453)com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255)org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)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:249)com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)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:249)com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:236)com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:236)com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:320)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:192)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)noteThefullstacktraceoftherootcauseisavailableintheApacheTomcat/6.0.32logs.
解决方案
解决方案二:
这个<s:fileid="img"name="img"cssClass="FileSty"cssStyle="width:280">name应该,upload.img,upload这个实体对象的属性,都要加对象名称.sturts,是这样的,spring-mvc就不用加对象名称.
解决方案三:
你的img属性没有传到HouseAction里面
解决方案四:
引用1楼ZuxiangHuang的回复:
这个<s:fileid="img"name="img"cssClass="FileSty"cssStyle="width:280">name应该,upload.img,upload这个实体对象的属性,都要加对象名称.sturts,是这样的,spring-mvc就不用加对象名称.
还是不行一样的错误我传入到了HouseActio类里面但是报的是upload为空;upload为什么能是空的呢
解决方案五:
引用2楼u012412259的回复:
你的img属性没有传到HouseAction里面
不对吧if(img!=null)这个都能往下走了不就说明了img里面是有值得吗
解决方案六:
ok了解决了