You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Steve R Burrus <bu...@swbell.net> on 2006/08/30 06:08:09 UTC

Need Help w. Null Pointer Exception.

I need some pretty immediate help/assistance with always getting a 
NullPointerException Java exception when I try to view a particular 
image in my favorite web browser!!! The line of text in my servlet file 
which is, I think, problemmatic is a while loop. Well here is my entire 
servlet file for someone to take a look at to hopefully help me with 
this problem.

                     package org.stephen.burris;

                         import java.io.*;
                         import javax.servlet.*;
                         import javax.servlet.http.*;

  public class One_More_Time extends HttpServlet {
     public void doGet(HttpServletRequest rq, HttpServletResponse rp ) 
throws
                       ServletException, IOException {
        rp.setContentType( "image/jpeg" );

        ServletContext sc = getServletContext ();
        InputStream is = sc.getResourceAsStream("/pamandersonnaked.jpeg");

        int read = 0;
        byte [] bytes = new byte[ 1024 ];

        OutputStream os = rp.getOutputStream ();
        while ((read = is.read(bytes)) != -1) {
        os.write( bytes, 0, read);
        }

        os.flush();
        os.close();
   }
}

And the server error : 


  HTTP Status 500 -

------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented 
it from fulfilling this request.

exception

java.lang.NullPointerException
	org.stephen.burris.One_More_Time.doGet(One_More_Time.java:19)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

note The full stack trace of the root cause is available in the Apache 
Tomcat/5.5.17 logs.

------------------------------------------------------------------------


      Apache Tomcat/5.5.17



Re: Need Help w. Null Pointer Exception.

Posted by David Smith <dn...@cornell.edu>.
getResourceAsStream() will return null if the requested resource is not 
available.  Keep in mind that getRequestAsStream() is rooted to your 
webapp, so the requested image needs to be found relative to the top 
level of your web application.

--David

Steve R Burrus wrote:

> I need some pretty immediate help/assistance with always getting a 
> NullPointerException Java exception when I try to view a particular 
> image in my favorite web browser!!! The line of text in my servlet 
> file which is, I think, problemmatic is a while loop. Well here is my 
> entire servlet file for someone to take a look at to hopefully help me 
> with this problem.
>
>                     package org.stephen.burris;
>
>                         import java.io.*;
>                         import javax.servlet.*;
>                         import javax.servlet.http.*;
>
>  public class One_More_Time extends HttpServlet {
>     public void doGet(HttpServletRequest rq, HttpServletResponse rp ) 
> throws
>                       ServletException, IOException {
>        rp.setContentType( "image/jpeg" );
>
>        ServletContext sc = getServletContext ();
>        InputStream is = sc.getResourceAsStream("/pamandersonnaked.jpeg");
>
>        int read = 0;
>        byte [] bytes = new byte[ 1024 ];
>
>        OutputStream os = rp.getOutputStream ();
>        while ((read = is.read(bytes)) != -1) {
>        os.write( bytes, 0, read);
>        }
>
>        os.flush();
>        os.close();
>   }
> }
>
> And the server error :
>
>  HTTP Status 500 -
>
> ------------------------------------------------------------------------
>
> type Exception report
>
> message
>
> description The server encountered an internal error () that prevented 
> it from fulfilling this request.
>
> exception
>
> java.lang.NullPointerException
>     org.stephen.burris.One_More_Time.doGet(One_More_Time.java:19)
>     javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
>     javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
>
> note The full stack trace of the root cause is available in the Apache 
> Tomcat/5.5.17 logs.
>
> ------------------------------------------------------------------------
>
>
>      Apache Tomcat/5.5.17
>
>
>


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Need Help w. Null Pointer Exception.

Posted by Edoardo Panfili <ed...@aspix.it>.
Steve R Burrus ha scritto:
> I need some pretty immediate help/assistance with always getting a 
> NullPointerException Java exception when I try to view a particular 
> image in my favorite web browser!!! The line of text in my servlet file 
> which is, I think, problemmatic is a while loop. Well here is my entire 
> servlet file for someone to take a look at to hopefully help me with 
> this problem.
> 
>                     package org.stephen.burris;
> 
>                         import java.io.*;
>                         import javax.servlet.*;
>                         import javax.servlet.http.*;
> 
>  public class One_More_Time extends HttpServlet {
>     public void doGet(HttpServletRequest rq, HttpServletResponse rp ) 
> throws
>                       ServletException, IOException {
>        rp.setContentType( "image/jpeg" );
> 
>        ServletContext sc = getServletContext ();
>        InputStream is = sc.getResourceAsStream("/pamandersonnaked.jpeg");
> 
>        int read = 0;
>        byte [] bytes = new byte[ 1024 ];
> 
>        OutputStream os = rp.getOutputStream ();
>        while ((read = is.read(bytes)) != -1) {
>        os.write( bytes, 0, read);
>        }
> 
>        os.flush();
>        os.close();
>   }
> }
> 
> And the server error :
> 
>  HTTP Status 500 -
> 
> ------------------------------------------------------------------------
> 
> type Exception report
> 
> message
> 
> description The server encountered an internal error () that prevented 
> it from fulfilling this request.
> 
> exception
> 
> java.lang.NullPointerException
>     org.stephen.burris.One_More_Time.doGet(One_More_Time.java:19)
>     javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
>     javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
> 
> note The full stack trace of the root cause is available in the Apache 
> Tomcat/5.5.17 logs.
> 
> ------------------------------------------------------------------------
> 
> 
>      Apache Tomcat/5.5.17
> 

line #19 is "while ((read = is.read(bytes)) != -1) {" ?

if so do you can check that "is" is not null?
Are you using some logging system? (log4j?)

Try to put "is.read(bytes)" before the loop and reload the page.
If the problem is on that line check the path of the file.


Edoardo

-- 
edoardo@aspix.it
AIM: edoardopn
Jabber: edoardopa@talk.google.com
tel:075 9142766

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org