1

I know there's loads of these threads already and the norm is to Google it, but I can't seem to find the solution. Anyway I keep getting this darned error and need to know how to fix it ASAP, since the Servlet project is due in this Wednesday. Code is here:

package DB_Engine;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;

/**
 * This class is used for all data manipulation regarding the Optician table.
 * @author 
 */
public class Optician 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 {
    }
    // <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 {
        processRequest(request, response);
    }
    /**
     * 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 IOException, ServletException {
        processRequest(request, response);
        String option = request.getParameter("option");
        if(option.equals("add"))
            addOptician(request, response);
        else if(option.equals("edit"))
            editOptician(request, response);
        else if(option.equals("find"))
            findOptician(request, response);
        else if(option.equals("delete"))
            deleteOptician(request, response);
        else option = null;
    }
    
    public static void addOptician (HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();
        try {
            defaultCode.pageSetup("Adding new optician...", true, response);
            Statement state = defaultCode.connect();
            String fname = request.getParameter("fname");
            String lname = request.getParameter("lname");
            String sql = "INSERT INTO optician (fname, lname) VALUES (" +
                    "\'" + fname + "\',\'" + lname + "\')";
            //out.println(sql);
            state.execute(sql);
            out.println("<br/>New optician <b>" + fname + " " + lname + "</b>" +
                    " has been added successfully to the database.");
            out.println("<br/><br/> Now redirecting back to home page (click " +
                    "<a href=\"index.html\">here</a> if it does not load)");
            defaultCode.endHTML(response);
        } catch (ClassNotFoundException ex) {
            out.println(ex);
        } catch (SQLException ex) {
            out.println(ex);
        }
    }
    public static void editOptician (HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();
        try {
            defaultCode.pageSetup("Editing optician details...",true, response);
            Statement state = defaultCode.connect();
            String fname = request.getParameter("fname");
            String lname = request.getParameter("lname");
            int id = Integer.parseInt(request.getParameter("id"));
            if((fname.isEmpty()==false) && (lname.isEmpty()==false)) {
                String sql = "UPDATE optician SET fname = \'"+fname+"\', " +
                        "lname = \'"+lname+"\' WHERE OID = \'"+id+"\'";
                state.execute(sql);
            } else {
                out.println("Please enter a value for both fields.");
            }
        } catch (ClassNotFoundException ex) {
            out.println(ex);
        } catch (SQLException ex) {
            out.println(ex);
        }
    }

    public static void findOptician(HttpServletRequest request,
            HttpServletResponse response) {
    }

    public static void deleteOptician(HttpServletRequest request,
            HttpServletResponse response) {
    }
    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

I've traced it via debug and it stops at editOptician. Yet addOptician works fine. :confused: Any ideas? I'm seriously on the verge of giving up here. :(

Votes + Comments
Used code tags on first post!
1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by helplessSoul
0

I can't edit my post, but what I can say is this: I fixed it! :D Problem was, the null part was referring to the (non-existent) HTML fields in the form. I spent hours looking for the solution only to find it right in fromt of me. xD (server logs are useful) Null pointers are usually referring to an non existent field/method.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.