【高薪求解】求大神指点java.lang.NullPointerException为什么没有值

问题描述

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了解决了

时间: 2024-09-05 08:50:10

【高薪求解】求大神指点java.lang.NullPointerException为什么没有值的相关文章

求大神指点 java无参带返回值方法问题

问题描述 求大神指点 java无参带返回值方法问题 max=(age>max)?age:max; 这这话是什么意思? 解决方案 max=(age>max)?age:max; 这是三步运算给max赋值,就相当于: f(age>max){ max=age; }else{ max=max; } 解决方案二: 如果age>max为真,则max=age,否则max=max 解决方案三: 如果age>max,就把age放入max 这个程序配合循环可以找到年纪最大的人 解决方案四: jav

跪求大神指点java算法

问题描述 跪求大神指点java算法 如何实现1+21+31+42+32+43+41+2+3.........的不规则加法,加的位数不确定 解决方案 这既是找规律,这就是1234...n的排列组合,你找个组合的算法,计算出各种组合,然后每个组合中的数相加

java中网络编程关于使用ObjetInputStream传输报错的问题,求大神指点

问题描述 java中网络编程关于使用ObjetInputStream传输报错的问题,求大神指点 以下贴出来的代码为通过ObjectInputStream(OutputStream)在客户端与服务器端之间交互时传输的对象,提供的图片中一个是客户端的代码片段,一个是服务器端的代码片段. 现在问题出在当客户端输完账号密码点击确定时,客户端卡死(应该是线程阻塞).服务器端抛出异常 :java.lang.ClassNotFoundException: com.wish.data.TransferObjec

但没有用remove函数-编写了一个Java循环报数的问题,应该是哪里卡住了过不去,求大神指点

问题描述 编写了一个Java循环报数的问题,应该是哪里卡住了过不去,求大神指点 public class Jump {/*1-100号人循环报数,报到7的倍数的输出,第一轮输出714,...98第二轮99报1输出513....不管是第几轮,输出完50个数结束.*/ public static void main(String[] args) { int arr_all[] = new int[101]; int m = 1; int i = 1; int out = 0; int count =

string-java 新人求解 不会写jsp求大神指点

问题描述 java 新人求解 不会写jsp求大神指点 后端是这样 样子的 @RequestMapping(value="toUpdate") public String toUpdate(HttpServletRequest request,Model model){ int id = Integer.parseInt(request.getParameter("id")); List<Emp> e=empMappingDao.findbyid(id);

遍历-求大神指点 关于java 的foreach循环问题

问题描述 求大神指点 关于java 的foreach循环问题 import java.util.Arrays; public class lianxi02 { public static void main(String[] args) { // 定义一个整型数组,保存成绩信息 int[] scores = { 89 72 64 58 93 }; // 对Arrays类对数组进行排序 Arrays.sort(scores); // 使用foreach遍历输出数组中的元素 for (int sco

求大神 指点关于java循环的问题

问题描述 求大神 指点关于java循环的问题 判断一个数(小于10位)的位数. 输入999,则输出 "它是个3位的数!" 解决方案 文艺青年用 log10 普通青年 循环整除10 xx青年,转换字符串,求长度 解决方案二: 转化为字符串,然后进行判断就可以 解决方案三: Scanner input = new Scanner(System.in); System.out.println("请输入一个10位以内的整数:"); int number = input.ne

排列-java如何实现全排!求大神指点

问题描述 java如何实现全排!求大神指点 例如12245,要求打印出来的数不能重复,需要打印出所有的排列组合,有么有大神会!主要是不知道该如何去重,希望给个思路.拜托啦!(≧?≦) 解决方案 把结果放在数组里面,每次输出结果之前比较一下之前的结果,有相同的就不输出? 解决方案二: http://blog.csdn.net/wmj2003/article/details/3678941 解决方案三: http://blog.csdn.net/guo_rui22/article/details/2

cpu-怎么用java获取非本机的CPU使用率和硬盘占有率?求大神指点!在线等!

问题描述 怎么用java获取非本机的CPU使用率和硬盘占有率?求大神指点!在线等! 可以获取本电脑的CPU使用率,不过获取他人的电脑的CPU使用率呢? 解决方案 这个一般是通过SNMP,WMI等方式,需要远程电脑上有安装对应的服务等 解决方案二: 请问需要安装什么服务?用java代码可以实现对非本机的CPU使用率的获取吗?谢谢啦!