You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by rahul bhalla <ur...@gmail.com> on 2012/04/23 16:32:19 UTC

using struts2 i want to upload image but nullpointerexception occur

Hello Every one
I m new Struts 2 and i m trying to upload a image on server with the help
of struts <http://www.coderanch.com/forums/f-58/Struts> 2 but i was unable
to do that i m pasting my code here ..
please tell where i m wrong

here is my action class

<http://www.coderanch.com/t/578754/Struts/struts-want-upload-image-but#>
        package com.webportal.admin.action;
        import java.io.File;
        import javax.servlet.http.HttpServletRequest;

        import org.apache.commons.io.FileUtils;
        import org.apache.struts2.ServletActionContext;
        import org.apache.struts2.interceptor.ServletRequestAware;

        import com.opensymphony.xwork2.ActionSupport;
        import com.webportal.admin.model.ImageDetail;

        public class SetInfo extends ActionSupport implements
 ServletRequestAware{
            private ImageDetail imageDetailBean;
            HttpServletRequest servletRequest;
                private static final long serialVersionUID = 1L;
            public String execute() throws Exception
                {
            boolean done=upload();
                if(done)
                return "success";
                else
                return "input"
            }
            private boolean upload() {
                    try {
                            String filePath =
servletRequest.getSession().getServletContext().getContextPath();
                            System.out.println("\nServer path:" + filePath+
"                   "+imageDetailBean.getLogoName()+".......");
                            File fileToCreate = new
 File(filePath, imageDetailBean.getLogoName());

FileUtils.copyFile(imageDetailBean.getLogo(), fileToCreate);
                        } catch (Exception e) {
                            e.printStackTrace();
                            addActionError(e.getMessage());

                            return false;
                        }
                        return true;

                    }

            public ImageDetail getImageDetailBean() {
                return imageDetailBean;
            }

            public void
 setImageDetailBean(ImageDetail imageDetailBean) {
                this.imageDetailBean = imageDetailBean;

            }
            public void
 setServletRequest(HttpServletRequest servletRequest) {
                    this.servletRequest = servletRequest;
            }
        }


my beans class is
        package com.webportal.admin.model;

        import java.io.File;

        public class ImageDetail {

            private String logoContentType;
            private String logoName;
            private File logo;

            public File getLogo() {
                return logo;
            }
            public void setLogo(File logo) {
                this.logo = logo;
            }


            public String getLogoContentType() {
                return logoContentType;
            }
            public void setLogoContentType(String logoContentType) {
                this.logoContentType = logoContentType;
            }


            public String getLogoName() {
                return logoName;
            }

            public void setLogoName(String logoName) {
                this.logoName = logoName;
            }

        public ImageDetail() {
                super();
                System.out.print("from the constructor Image detail");
            }

        }



*now my jsp from where i set my bean
*

            <s:form action="settingHeader" cssClass="form" enctype=
"multipart/form-data" method="post"  >
                   <s:div cssClass="fileinputs">
                        <s:file  key="imageDetailBean.logo"  />
                   </s:div>
            </s:form>

my property file
 <http://www.coderanch.com/t/578754/Struts/struts-want-upload-image-but#>

imageDetailBean.logo=Logo




struts.xml

<http://www.coderanch.com/t/578754/Struts/struts-want-upload-image-but#>
        <action name="settingHeader" class=
"com.webportal.admin.action.SetInfo" method="execute" >
                    <interceptor-ref name="fileUpload">
                        <param name="maximumSize">2097152</param>
                        <param name="allowedTypes">
                                image/png,image/gif,image/jpeg,image/jpg
                        </param>
                    </interceptor-ref>
                    <interceptor-ref name="defaultStack">
                    </interceptor-ref>
                    <result name="input">/success.jsp</result>
                    <result name="success">/error.jsp</result>
                </action>

when i deploy my application i got nullpointerexception and on line number
28 of the action class i found it prints null on server
please suggest me what can i do...
thanking you


-----------
Regards
Rahul Bhalla
(9953225211)

Re: using struts2 i want to upload image but nullpointerexception occur

Posted by rahul bhalla <ur...@gmail.com>.
First of all thanks to  Dave Newton and Maurizio Cucchiara for your
precious  reply
@Maurizio Cucchiara you very much write about where i m getting exception
and also you provide correct solution also.
can please tell the reason why  i changed the setter method

Regards
Rahul Bhalla
(9953225211)

Re: using struts2 i want to upload image but nullpointerexception occur

Posted by Maurizio Cucchiara <mc...@apache.org>.
I'm pretty sure that logoName is always null in the following code line.

File(filePath, imageDetailBean.getLogoName());

According with the docs [1], you should change your setter from
setLogoName to set setLogoFileName

[1] http://struts.apache.org/2.x/docs/file-upload.html

Twitter     :http://www.twitter.com/m_cucchiara
G+          :https://plus.google.com/107903711540963855921
Linkedin    :http://www.linkedin.com/in/mauriziocucchiara

Maurizio Cucchiara


On 23 April 2012 18:28, rahul bhalla <ur...@gmail.com> wrote:
>
> my server show like below
>
> Server path:/webportalextn                 null.......
> java.lang.NullPointerException
>    at java.io.File.<init>(Unknown Source)
>    at com.webportal.admin.action.SetInfo.upload(SetInfo.java:89)
>    at com.webportal.admin.action.SetInfo.execute(SeInfo.java:26)
>    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
>    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
> .....
> --
> Regards
> Rahul Bhalla
> (9953225211)

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: using struts2 i want to upload image but nullpointerexception occur

Posted by rahul bhalla <ur...@gmail.com>.
my server show like below

Server path:/webportalextn                 null.......
java.lang.NullPointerException
    at java.io.File.<init>(Unknown Source)
    at com.webportal.admin.action.SetInfo.upload(SetInfo.java:89)
    at com.webportal.admin.action.SetInfo.execute(SeInfo.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
.....
-- 
Regards
Rahul Bhalla
(9953225211)

Re: using struts2 i want to upload image but nullpointerexception occur

Posted by Dave Newton <da...@gmail.com>.
What does your debugger tell you is null?

On Mon, Apr 23, 2012 at 10:32 AM, rahul bhalla <ur...@gmail.com>wrote:

> Hello Every one
> I m new Struts 2 and i m trying to upload a image on server with the help
> of struts <http://www.coderanch.com/forums/f-58/Struts> 2 but i was unable
> to do that i m pasting my code here ..
> please tell where i m wrong
>
> here is my action class
>
> <http://www.coderanch.com/t/578754/Struts/struts-want-upload-image-but#>
>        package com.webportal.admin.action;
>        import java.io.File;
>        import javax.servlet.http.HttpServletRequest;
>
>        import org.apache.commons.io.FileUtils;
>        import org.apache.struts2.ServletActionContext;
>        import org.apache.struts2.interceptor.ServletRequestAware;
>
>        import com.opensymphony.xwork2.ActionSupport;
>        import com.webportal.admin.model.ImageDetail;
>
>        public class SetInfo extends ActionSupport implements
>  ServletRequestAware{
>            private ImageDetail imageDetailBean;
>            HttpServletRequest servletRequest;
>                private static final long serialVersionUID = 1L;
>            public String execute() throws Exception
>                {
>            boolean done=upload();
>                if(done)
>                return "success";
>                else
>                return "input"
>            }
>            private boolean upload() {
>                    try {
>                            String filePath =
> servletRequest.getSession().getServletContext().getContextPath();
>                            System.out.println("\nServer path:" + filePath+
> "                   "+imageDetailBean.getLogoName()+".......");
>                            File fileToCreate = new
>  File(filePath, imageDetailBean.getLogoName());
>
> FileUtils.copyFile(imageDetailBean.getLogo(), fileToCreate);
>                        } catch (Exception e) {
>                            e.printStackTrace();
>                            addActionError(e.getMessage());
>
>                            return false;
>                        }
>                        return true;
>
>                    }
>
>            public ImageDetail getImageDetailBean() {
>                return imageDetailBean;
>            }
>
>            public void
>  setImageDetailBean(ImageDetail imageDetailBean) {
>                this.imageDetailBean = imageDetailBean;
>
>            }
>            public void
>  setServletRequest(HttpServletRequest servletRequest) {
>                    this.servletRequest = servletRequest;
>            }
>        }
>
>
> my beans class is
>        package com.webportal.admin.model;
>
>        import java.io.File;
>
>        public class ImageDetail {
>
>            private String logoContentType;
>            private String logoName;
>            private File logo;
>
>            public File getLogo() {
>                return logo;
>            }
>            public void setLogo(File logo) {
>                this.logo = logo;
>            }
>
>
>            public String getLogoContentType() {
>                return logoContentType;
>            }
>            public void setLogoContentType(String logoContentType) {
>                this.logoContentType = logoContentType;
>            }
>
>
>            public String getLogoName() {
>                return logoName;
>            }
>
>            public void setLogoName(String logoName) {
>                this.logoName = logoName;
>            }
>
>        public ImageDetail() {
>                super();
>                System.out.print("from the constructor Image detail");
>            }
>
>        }
>
>
>
> *now my jsp from where i set my bean
> *
>
>            <s:form action="settingHeader" cssClass="form" enctype=
> "multipart/form-data" method="post"  >
>                   <s:div cssClass="fileinputs">
>                        <s:file  key="imageDetailBean.logo"  />
>                   </s:div>
>            </s:form>
>
> my property file
>  <http://www.coderanch.com/t/578754/Struts/struts-want-upload-image-but#>
>
> imageDetailBean.logo=Logo
>
>
>
>
> struts.xml
>
> <http://www.coderanch.com/t/578754/Struts/struts-want-upload-image-but#>
>        <action name="settingHeader" class=
> "com.webportal.admin.action.SetInfo" method="execute" >
>                    <interceptor-ref name="fileUpload">
>                        <param name="maximumSize">2097152</param>
>                        <param name="allowedTypes">
>                                image/png,image/gif,image/jpeg,image/jpg
>                        </param>
>                    </interceptor-ref>
>                    <interceptor-ref name="defaultStack">
>                    </interceptor-ref>
>                    <result name="input">/success.jsp</result>
>                    <result name="success">/error.jsp</result>
>                </action>
>
> when i deploy my application i got nullpointerexception and on line number
> 28 of the action class i found it prints null on server
> please suggest me what can i do...
> thanking you
>
>
> -----------
> Regards
> Rahul Bhalla
> (9953225211)
>