Rajesh2389
Rajesh2389

Reputation: 379

ServletContext#getResourceAsStream() returns null

Can anybody answer this?Don't know why i'm getting NullPointerException when trying to execute the below programme.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{
    response.setContentType("image/jpeg");
    ServletContext ctx = getServletContext();
    InputStream is = ctx.getResourceAsStream("C:/Users/Public/Pictures/Sample Pictures/Desert.jpg"); --> The picture exists at this location
    OutputStream os = response.getOutputStream();
    int read = 0;
    byte[] bytes = new byte[1024];
    System.out.println("bytes :" + bytes);
    while((read = is.read(bytes)) != -1) -- Error at this line
    {
    System.out.println("read :" +read);
    os.write(bytes, 0, read);
    }
    os.flush();
    os.close();
    }

When running this in tomcat-Apache Getting the below error.

bytes :[B@158f9d3
Sep 19, 2013 7:23:04 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet Newest threw exception
java.lang.NullPointerException
        at com.examples.Newest.doPost(Newest.java:69)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:873)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:662)

Upvotes: 0

Views: 1455

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279970

This

InputStream is = ctx.getResourceAsStream("C:/Users/Public/Pictures/Sample Pictures/Desert.jpg"); --> The picture exists at this location

is not how you access a file on the file system. With that method call you are trying to get the resource from the context root. And, because you obviously don't have a resource called C:/Users/Public/Pictures/Sample Pictures/Desert.jpg relative to the context root, it returns null.

Use Java 7's NIO

Path path = Paths.get("C:/Users/Public/Pictures/Sample Pictures/Desert.jpg");
InputStream is = Files.newInputStream(path);

Upvotes: 1

Related Questions