Struts get set 为空 急啊 找了好几天了没找到 (最后一点积分全给了)

 

不知道为什么取不到前台传过来的值!! 急啊 找了好几天了

 

错误信息

 

 

java.lang.NullPointerException
    com.pfdragon.web.struts.action.WorkAction.addWork(WorkAction.java:238)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:585)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:399)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:262)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:224)
    com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:213)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:150)
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:48)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
    com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:123)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.intercept(ParametersInterceptor.java:161)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
    com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:105)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
    org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:83)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
    org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:268)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
    com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:74)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
    com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:127)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
    org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:107)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
    org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:206)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
    com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:115)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
    com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:143)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
    com.opensymphony.xwork2.interceptor.PrepareInterceptor.intercept(PrepareInterceptor.java:115)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
    org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:170)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
    com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:123)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
    com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
    com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
    com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:507)
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:421)
    org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)

 

action

 

 

 

    public String addWork()throws Exception{

      Pfdragonwork pw = new Pfdragonwork();
        
        String src =null;
        
        System.out.println("r======"+r);
        
        if(r.equals("2")){这里错了。 r空的 不知道 为什么是空
            
         src = dao.upload(file,fileFileName,"upload");这里的file 也是空的
    
        pw.setSrc(src);
        
        pw.setType(type);
    
        pw.setName(name);
        
        pw.setIsdowload("putong");
    
        
        
        }else{
            
            src = dao.upload(file,fileFileName,"upload");
            
            pw.setSrc(src);
            
            pw.setType(type);
            
            pw.setName(name);
            
            pw.setIsdowload("putong");
            
            pw.setSrc2(dao.upload(myfile,myfileFileName,"upload"));
            
        }
        share.getDao().addObject(pw);
        return "WorkAction";
    }

 

share.upload() ;

 

public  String upload(File file,String fileFileName,String upload)throws Exception{
        
        Date date = new Date();
        
        InputStream is = new FileInputStream(file); 
        
        //文件名称(当前毫秒数+文件后缀名)
        String filename = date.getTime()+fileFileName.substring(fileFileName.length()-4,fileFileName.length());
        
        //文件路径
        //String root=ServletActionContext.getRequest().getRealPath("//"+upload);
        String path=ServletActionContext.getServletContext().getRealPath(File.separator+upload);
        //System.out.print(root);
        //创建输出文件
        File outfile = new File(path,filename);
        
        //文件输出流
        OutputStream out = new FileOutputStream(outfile);
        
        //定义字节数组
        byte[] buffer = new byte[400];
        
        int length = 0;
        
        while((length = is.read(buffer))>0){
            
            out.write(buffer,0,length);
            
        }
        is.close();
        
        out.close();
        return filename;
    }

 

 

 

struts.xml

 

 

 

  <action name="work" class="com.pfdragon.web.struts.action.WorkAction">
        
            <result name="WorkList">admin/WorkList.jsp</result>
            
            <result name="WorkAction" type="redirect-action">work!workList.action</result>
            
            <result name="indexList">admin/IndexImageList.jsp</result>
            
            <result name="indexAction" type="redirect-action">work!indexImage.action</result>
            
            <result name="randomWork">admin/RandomWork.jsp</result>
            
            <result name="RandomWorkAction" type="redirect-action">work!randomWork.action</result>
            
            
            
        </action>

 

 

前台页面

 

 

<form action="work!addWork.action" enctype="multipart/form-data"  method="post" onsubmit="return tj()">
作品类型:
<select name="type">
    
    <s:iterator value="typelist">
    
    <option value="<s:property value="name"/>"><s:property value="name"/></option>
    
    </s:iterator>
</select><br />

&nbsp;缩&nbsp;微&nbsp;图&nbsp;:

有<input type="radio" name="r" onclick="radio1()" value="1">-

无<input type="radio" name="r" checked="checked" value="2" onclick="radio2()"><br/>

    <div id="divnone" style="display:none;width:100%px;">
    
            &nbsp;缩&nbsp;微&nbsp;图&nbsp;:
    
            <input type="file" name="myfile"/><br/>
    </div>
作品名称:
<input type="text" name="name" id="mingcheng"/><br/>

上传作品:
<input type="file" name="file"/><br/>

<input type="submit" value="添加" />

<input type="button" value="取消" onclick="none()" />

</form>

 

大家帮忙解答啊! 我这找好久不知道 是为什么 配置的拦截器 都去掉了。。

10个回答

[quote]
仔细检查你的配置。。。。

一般没有这种情况,

还有,你的字段名取长一点试试。

或者你把r換成大写的R试试。。。

[/quote]

com.pfdragon.web.struts.action.WorkAction.addWork(WorkAction.java:238)

238行是哪一行

action中没有定义r这个属性和它的get set 方法?

[quote]配置的拦截器 都去掉了。。[/quote]
这是什么意思?默认拦截器也没了吗?Action的属性就是通过拦截器来设置进去的!

希望能贴出action的完整代码,这样更有利于帮你排错

其他 的action都会有空的问题吗,还是只有这个类会出这个问题,

你直接 request.getParameter("r")看有没有值,估计你根据没有提交r域

把file的名称换一个试试,感觉怪怪的,至于action中我帮你看了,没看出来问题。

把file和r这两个变量换个名字。r这个太短了,,改成rrr试试。

没看出啥问题,不过你这命名真是太不规范了吧,"2".equal(r)这样不会报空指针错误,自己多调试下吧

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