0

How can I figure out this 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 p1.Download.doGet(Download.java:76) javax.servlet.http.HttpServlet.service(HttpServlet.java:622) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393) note The full stack trace of the root cause is available in the Apache Tomcat/8.0.27 logs.

Apache Tomcat/8.0.27

Code:

package p1;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Admin
 */
public class Download extends HttpServlet
{

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter())
        {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Download</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet Download at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        response.setContentType("application/jar");

        ServletContext ctx=getServletContext();
        InputStream is=ctx.getResourceAsStream("/filefroup1.jar");

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

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

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo()
    {
        return "Short description";
    }// </editor-fold>

}


<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form action="/Download">
            <input type="submit" value="download">
        </form>
    </body>
</html>



<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>Download</servlet-name>
        <servlet-class>p1.Download</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Download</servlet-name>
        <url-pattern>/Download</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>
halfer
  • 19,824
  • 17
  • 99
  • 186
  • So, probably `InputStream is=ctx.getResourceAsStream("/filefroup1.jar");` returns `null`. Check that. Check the location of the `filefroup1.jar` is it inside your `.war`? – PKey Feb 18 '18 at 15:11

1 Answers1

0

Plirkee is correct,

I ran your code and created a file called filefroup.jar, and stuck it in WebContent, and your code worked for me.

Added this line otherwise the file get's named "Download"

response.setHeader("Content-disposition", "attachment; filename="+ "filefroup1.jar");

So it looks like you have an issue with where the file is being stored, since you didn't specify where it is in your structure, I guess it isn't in WebContent and you want to place it elsewhere in your project?

Alan Blyth
  • 186
  • 1
  • 9
  • I was making the project in netbeans. I have stored that file into web pages folder. –  Feb 20 '18 at 12:57