Results 1 to 5 of 5
  1. #1
    techsing14 is offline Member
    Join Date
    Oct 2010
    Posts
    24
    Rep Power
    0

    Default NullPointerException in Servlet

    Hi

    I am trying to display records then onclick edit button the fileds will edit then update button , onclick this button the database shd update with the fields, I am getting NullPointer Exception, Dont knw wats the problem, Here is my code

    OutransEdit.java

    Java Code:
    package update;   
      
    import java.io.*;   
    import javax.servlet.*;   
    import javax.servlet.http.*;   
    import java.sql.*;   
      
    public class OutransEdit extends HttpServlet {   
      
    public void doPost(HttpServletRequest request, HttpServletResponse response)   
                throws ServletException, IOException {   
            response.setContentType("text/html");   
            PrintWriter out = response.getWriter();   
            out.println("<title>Update Page</title>" + "<h1 align=\"center\"></h1>" + "<body bgcolor=#99CCFF>");   
            out.println("<center><td  width=\"37\"> <strong><img title=\"Go back\" src=\"Btn_back.png\" width=\"20\" height=\"20\" onClick=\"javascript:history.back()\" style=\"vertical-align:middle;\" onMouseOver=\"this.style.cursor='hand';\"></strong></td></center><br>");   
            System.out.println("Oracle 10g Connect Example.");   
            Connection conn = null;   
            String url = "jdbc:oracle:thin:@localhost:1529:xe";   
            String driver = "oracle.jdbc.driver.OracleDriver";   
            String user = "hr";   
            String pass = "hr";   
            if (request.getParameter("SerialNo") != null && request.getParameter("SerialNo") != "") {   
      
               String SerialNo = request.getParameter("SerialNo").toString();   
                String PartNo = request.getParameter("PartNo").toString();   
                String Material_Number = request.getParameter("Material_Number").toString();   
                String Material_Desc = request.getParameter("Material_Desc").toString();   
                String Pegasus_Work_Order_No = request.getParameter("Pegasus_Work_Order_No").toString();   
                String Project_Code = request.getParameter("Project_Code").toString();   
                 String Dt_of_OUT_Transaction = request.getParameter("Dt_of_OUT_Transaction").toString();   
                String Transaction_Status = request.getParameter("Transaction_Status").toString();   
                String Inventory_Qty = request.getParameter("Inventory_Qty").toString();   
                String Requested_By = request.getParameter("Requested_By ").toString();   
                String Issued_By  = request.getParameter("Issued_By ").toString();   
                String Received_By= request.getParameter("Received_By").toString();   
      
                Statement stmt;   
                try {   
                    Class.forName(driver).newInstance();   
                    conn = DriverManager.getConnection(url, user, pass);   
                    System.out.println("Connected to the database");   
                    String query = "UPDATE InventoryDB_Main SET SerialNo = '"+ SerialNo +"',PartNo='"+ PartNo+"',Material_Number='" + Material_Number + "',Material_Desc='"+ Material_Desc + "',Pegasus_Work_Order_No='"+ Pegasus_Work_Order_No +"',Project_Code='"+Project_Code+"',Dt_of_OUT_Transaction='"+ Dt_of_OUT_Transaction +"',Transaction_Status='"+ Transaction_Status +"',Inventory_Qty='"+Inventory_Qty+"',Requested_By='"+ Requested_By +"',Issued_By='"+ Issued_By +"',Received_By='"+ Received_By+"' WHERE SerialNo='" + SerialNo + "'";   
                    stmt = conn.createStatement();   
                    int i = stmt.executeUpdate(query);   
                    System.out.println("query" + query);   
                       if (i > 0) {   
                       out.print("<br>");   
                       out.print("<table  align=\"center\" font=\"18\" style=\"background-color:#efefef\"width=\"50%\" border=\"1\">");   
                       out.print("<tr><th>Data Modified Successfully</th></tr>");   
                       out.print("</table>");   
                       stmt.close();   
                    }   
    }   
      catch(ClassNotFoundException e){   
      out.println("Couldn't load database driver: " + e.getMessage());   
      }   
      catch(NullPointerException e){   
      out.println("NullPointerException caught: " + e.getMessage());   
      }   
      catch (Exception e){   
      out.println(e);   
      }   
      finally {   
      // Always close the database connection.   
      try {   
      if (conn != null) conn.close();   
      }   
      catch (SQLException ignored){   
      out.println(ignored);   
      }   
      }   
      }   
    }    
    }
    The Dt_of_OUT_Transaction column is VARCHAR2(20).


    This code is giving me NullPointer Exception, Can anyone please look into my code and correct, Its urgent. I really dont knw where is the problem. Please help me out.

    Thanks in advance
    Lissy.

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Hi Lissy. First can you show a full stack trace this exception? Second I piece of advice you use follows construction when you want to get parameters from request.
    Java Code:
    String value = (String) request.getParameter("value");
    when you get this parameter you will not have NPE if value is not request.
    Look at your statement
    Java Code:
    String value = request.getParameter("value").toString();
    if request.getParameter("value") return null you will have NPE.
    Three You can try to separate your code on model-view-controller architecture. Don't use any database code in Servlet's code. It is so badly.
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    techsing14 is offline Member
    Join Date
    Oct 2010
    Posts
    24
    Rep Power
    0

    Default

    java.lang.NullPointerException
    at update.OutransEdit.doPost(OutransEdit.java:35)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:755)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:848)
    at org.apache.catalina.core.StandardWrapper.service(S tandardWrapper.java:1534)
    at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke (StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:595)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipel ine.java:98)
    at com.sun.enterprise.web.PESessionLockingStandardPip eline.invoke(PESessionLockingStandardPipeline.java :91)
    at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:162)
    at org.apache.catalina.connector.CoyoteAdapter.doServ ice(CoyoteAdapter.java:326)
    at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:227)
    at com.sun.enterprise.v3.services.impl.ContainerMappe r.service(ContainerMapper.java:170)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(P rocessorTask.java:822)
    at com.sun.grizzly.http.ProcessorTask.doProcess(Proce ssorTask.java:719)
    at com.sun.grizzly.http.ProcessorTask.process(Process orTask.java:1013)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute (DefaultProtocolFilter.java:225)
    at com.sun.grizzly.DefaultProtocolChain.executeProtoc olFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(Defau ltProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(Defau ltProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(Htt pProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(Pr otocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(Selec tionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:7 1)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doW ork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run (AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:662)
    |#]

    This is the full stack trace, will try with prepared statement again. Please help me.

    Thanks in advance
    Lissy

  4. #4
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Hi Lussy. Can you say you changed your code how I told you?
    Skype: petrarsentev
    http://TrackStudio.com

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    None of those getParameter calls requires a toString (or a cast), since getParameter returns a String.
    Otherwise Petr is probably correct that one of those getParameter calls is returning null.

    Also, when supplying the stack trace you need to show us where in your code it is being thrown from. We aren't going to copy and paste this stuff into an IDE to find out.

Similar Threads

  1. how can i link html+servlet+xml to form servlet
    By balachandarr in forum Java Servlet
    Replies: 0
    Last Post: 04-15-2009, 04:06 PM
  2. Servlet to Servlet connection through URLConnection
    By bsrjavaforum in forum Networking
    Replies: 0
    Last Post: 04-01-2009, 04:12 PM
  3. Replies: 1
    Last Post: 12-02-2008, 12:23 AM
  4. Replies: 0
    Last Post: 08-30-2008, 11:52 AM
  5. Replies: 1
    Last Post: 08-07-2007, 03:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •