Delete table records using checkbox in jsp

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ramprakashjava
    New Member
    • Mar 2009
    • 15

    Delete table records using checkbox in jsp

    hi,
    i hav "java.lang.Null PointerExceptio n" error while Deleting table records using checkbox in jsp here i enclosed files help quickly plzzz..
    Regards
    Ramprakash

    /*Adminpage.jsp*/
    <%@ page language="java" import="java.sq l.*"%>
    <html>
    <head>
    <title>Admin Page</title>
    <script language="javas cript">

    function validation()
    {
    var count=0;
    for(vari=0;i<ad min.chbox.lengt h;i++)
    {
    if(admin.chbox[i].checked)
    {
    count++;
    }

    }

    if(count==0)
    {
    alert("check the messages to delete & u had limited msg");
    //document.msgchb ox.checkbox.foc us();
    return false;
    }
    else
    {
    return true;
    }

    }


    </script>
    </head>

    <body>
    <form name="admin" method="post" action="deletep age.jsp" onClick="return validation()">
    <h2 align="center"> Freshers Table Records</h2>
    <div align="right">< input type="submit" value=" DELETE "/>&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;
    </div>
    <div>
    <table border="3" width="95%">
    <tr>

    <td width="14%">&nb sp;&nbsp;&nbsp; &nbsp;Date</td>
    <td width="32%">Nam e</td>
    <td width="40%">Ema il Id</td>
    <td width="14%">Con tact No</td>

    </tr></table>
    </div>
    </form>
    <%

    Connection con = null;
    Statement stmt = null;
    ResultSet rs=null;


    try
    {
    Class.forName(" com.mysql.jdbc. Driver");
    con = DriverManager.g etConnection("j dbc:mysql://localhost:3306/ram","root","su n");
    stmt = con.createState ment();

    String qy = "select * from userinfofresher ";

    rs = stmt.executeQue ry(qy);

    while(rs.next() )
    {
    %>
    <table width="95%">
    <tr><td width="3%"><inp ut type="checkbox" name="chbox" value="<%=rs.ge tString(3)%>"/></td>
    <td width="12%"><%= rs.getString(33 )%></td>
    <td width="33%"><%= rs.getString(1) %></td>
    <td width="41%"><%= rs.getString(2) %></td>
    <td width="15%"><%= rs.getString(3) %></td>
    </tr></table>
    <%
    }

    }
    catch(Exception e)
    {
    out.println(e.g etMessage());
    }

    %>

    </body>
    </html>
    /*deletepage.jsp */
    <%@ page language="java" import="java.sq l.*"%>
    <html>
    <head>
    <title>Delete page</title>

    </head>

    <body>


    <%

    String c[]= request.getPara meterValues("ch box");
    out.println(c[0]);

    Connection con = null;
    Statement stmt = null;

    try
    {

    Class.forName(" com.mysql.jdbc. Driver");
    con = DriverManager.g etConnection("j dbc:mysql://localhost:3306/ram","root","su n");
    stmt = con.createState ment();
    for(String s:c)
    {
    String qy = "delete from userinfofresher where emailid='"+s+"' ";
    stmt.executeUpd ate(qy);
    out.println("Su ccessfully Deleted");
    }

    }
    catch(Exception e)
    {

    out.println(e.g etMessage());
    }
    %>

    </body>
    </html>
    /*db file */
    /*
    MySQL Data Transfer
    Source Host: localhost
    Source Database: ram
    Target Host: localhost
    Target Database: ram
    Date: 3/2/2009 4:03:02 PM
    */

    SET FOREIGN_KEY_CHE CKS=0;
    -- ----------------------------
    -- Table structure for userinfofresher
    -- ----------------------------
    DROP TABLE IF EXISTS `userinfofreshe r`;
    CREATE TABLE `userinfofreshe r` (
    `name` varchar(255) NOT NULL,
    `emailid` varchar(255) NOT NULL,
    `contno` varchar(255) NOT NULL,
    `tds` varchar(255) NOT NULL,
    `ta` varchar(255) NOT NULL,
    `tc` varchar(255) NOT NULL,
    `tz` varchar(255) NOT NULL,
    `pds` varchar(255) NOT NULL,
    `pa` varchar(255) NOT NULL,
    `pc` varchar(255) NOT NULL,
    `pz` varchar(255) NOT NULL,
    `fathersname` varchar(255) NOT NULL,
    `sex` varchar(255) NOT NULL,
    `dob` varchar(255) NOT NULL,
    `age` varchar(255) NOT NULL,
    `bloodgroup` varchar(255) NOT NULL,
    `nationality` varchar(255) NOT NULL,
    `matrialstatus` varchar(255) NOT NULL,
    `language` varchar(255) NOT NULL,
    `sslc` varchar(255) NOT NULL,
    `hsc` varchar(255) NOT NULL,
    `degree1` varchar(255) NOT NULL,
    `degree2` varchar(255) NOT NULL,
    `degree3` varchar(255) NOT NULL,
    `degree4` varchar(255) NOT NULL,
    `software` varchar(255) NOT NULL,
    `ipt` varchar(255) NOT NULL,
    `aoi` varchar(255) NOT NULL,
    `projtitle` varchar(255) NOT NULL,
    `projdes` longtext NOT NULL,
    `ach` varchar(255) NOT NULL,
    `hobby` varchar(255) NOT NULL,
    `date` varchar(255) NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

    -- ----------------------------
    -- Records
    -- ----------------------------
    INSERT INTO `userinfofreshe r` VALUES ('RAMPRAKASH K A', 'ramprakash.jav a@gmail.com', ' 9787818096', 'No 8,First cross st', 'Annanagar West', 'Chennai', '600078', 'No 5/E, cross st', 'West pillaiyar koil st', 'Trichy', '638478', 'Arun K P', 'Male', '03nov1982', '21', 'O+ve ', 'Indian', 'Unmarried', 'Tamil,English' , '1997-2000 S.S.L.C (State Board )Govt High School,Karur -87%', ' 2000-2002 XII Standard (State Board)Govt Higher Secondary School,Karur -82%', '2002-2005 Dipoma in Computer Technology Govt Polytechnic College,Karur -90%', '2002-2005 Bachelor of Computer Science Engineering in St.Joseph?s Engineering College, Anna University,Chen nai -74%', '', '', 'C,java/j2ee', 'HCL technologies,Am battur,Chennai. ', 'web Developement', 'Vendor Information Management System.', 'The VIMS deals with the Online Ship store management system.It is an bridge between the supplier and the ship.The VIMS deals with the Online Ship store management system.It is an bridge between the supplier and the ship.The VIMS deals with the Online Ship store management system.It is an bridge between the supplier and the ship.The VIMS deals with the Online Ship store management system.It is an bridge between the supplier and the ship.The VIMS deals with the Online Ship store management system.It is an bridge between the supplier and the ship.The VIMS deals with the Online Ship store management system.It is an bridge between the supplier and the ship.', 'Won the First Prize for Paper Presentation held on Sankara College-Coimbatore', 'Reading books,Playing cricket', '2009-2-13');
    INSERT INTO `userinfofreshe r` VALUES ('ram', 'ramprakash_ka@ yahoo.co.in', '5454418745', '5,dsghjkghdjka fjf', 'gkjdflgkdfj', 'mvn', '54655646', '6,ssjkfhjhdfjj l', 'lkbv', 'ijhgjkdfh', '624654', 'kdfjnbio', 'male', '03nov1992', '23', 'b+ve', 'indian', 'single', 'nmkgnkg', '1997-2000 S.S.L.C (State Board )Govt High School,Karur -87%', '2000-2002 XII Standard (State Board)Govt Higher Secondary School,Karur -82%', '2002-2005 Dipoma in Computer Technology Govt Polytechnic College,Karur -90%', '', '', '', 'c,java', 'bknjknsghitjkn dfgkjhdfkghjk', 'mgmkjgl', 'automatic alaram', 'bvdbdnm n m\r\nsdnslfnksl \r\nvcslmklgmdk lm\r\nvdfm,;dm\ r\nvmvd', 'jnmnjkgj', 'video game', '2009-2-12');
    INSERT INTO `userinfofreshe r` VALUES ('ram', 'ramprakash_kas @yahoo.co.in', '5454418745', '5,dsghjkghdjka fjf', 'gkjdflgkdfj', 'mvn', '54655646', '6,ssjkfhjhdfjj l', 'lkbv', 'ijhgjkdfh', '624654', 'kdfjnbio', 'male', '03nov1992', '23', 'b+ve', 'indian', 'single', 'nmkgnkg', '1997-2000 S.S.L.C (State Board )Govt High School,Karur -87%', '2000-2002 XII Standard (State Board)Govt Higher Secondary School,Karur -82%', '2002-2005 Dipoma in Computer Technology Govt Polytechnic College,Karur -90%', '', '', '', 'c,java', 'bknjknsghitjkn dfgkjhdfkghjk', 'mgmkjgl', 'automatic alaram', 'bvdbdnm n m\r\nsdnslfnksl \r\nvcslmklgmdk lm\r\nvdfm,;dm\ r\nvmvd', 'jnmnjkgj', 'video game', '2009-2-11');
    INSERT INTO `userinfofreshe r` VALUES ('ram', 'ramprakash_k@y ahoo.co.in', '5454418745', '5,dsghjkghdjka fjf', 'gkjdflgkdfj', 'mvn', '54655646', '6,ssjkfhjhdfjj l', 'lkbv', 'ijhgjkdfh', '624654', 'kdfjnbio', 'male', '03nov1992', '23', 'b+ve', 'indian', 'single', 'nmkgnkg', '1997-2000 S.S.L.C (State Board )Govt High School,Karur -87%', '2000-2002 XII Standard (State Board)Govt Higher Secondary School,Karur -82%', '2002-2005 Dipoma in Computer Technology Govt Polytechnic College,Karur -90%', '', '', '', 'c,java', 'bknjknsghitjkn dfgkjhdfkghjk', 'mgmkjgl', 'automatic alaram', 'bvdbdnm n m\r\nsdnslfnksl \r\nvcslmklgmdk lm\r\nvdfm,;dm\ r\nvmvd', 'jnmnjkgj', 'video game', '2009-2-09');
    INSERT INTO `userinfofreshe r` VALUES ('ram', 'ramprakash_a@y ahoo.co.in', '5454418745', '5,dsghjkghdjka fjf', 'gkjdflgkdfj', 'mvn', '54655646', '6,ssjkfhjhdfjj l', 'lkbv', 'ijhgjkdfh', '624654', 'kdfjnbio', 'male', '03nov1992', '23', 'b+ve', 'indian', 'single', 'nmkgnkg', '1997-2000 S.S.L.C (State Board )Govt High School,Karur -87%', '2000-2002 XII Standard (State Board)Govt Higher Secondary School,Karur -82%', '2002-2005 Dipoma in Computer Technology Govt Polytechnic College,Karur -90%', '', '', '', 'c,java', 'bknjknsghitjkn dfgkjhdfkghjk', 'mgmkjgl', 'automatic alaram', 'bvdbdnm n m\r\nsdnslfnksl \r\nvcslmklgmdk lm\r\nvdfm,;dm\ r\nvmvd', 'jnmnjkgj', 'video game', '2009-2-09');
    INSERT INTO `userinfofreshe r` VALUES ('ram', 'ramprakash@yah oo.co.in', '5454418745', '5,dsghjkghdjka fjf', 'gkjdflgkdfj', 'mvn', '54655646', '6,ssjkfhjhdfjj l', 'lkbv', 'ijhgjkdfh', '624654', 'kdfjnbio', 'male', '03nov1992', '23', 'b+ve', 'indian', 'single', 'nmkgnkg', '1997-2000 S.S.L.C (State Board )Govt High School,Karur -87%', '2000-2002 XII Standard (State Board)Govt Higher Secondary School,Karur -82%', '2002-2005 Dipoma in Computer Technology Govt Polytechnic College,Karur -90%', '', '', '', 'c,java', 'bknjknsghitjkn dfgkjhdfkghjk', 'mgmkjgl', 'automatic alaram', 'bvdbdnm n m\r\nsdnslfnksl \r\nvcslmklgmdk lm\r\nvdfm,;dm\ r\nvmvd', 'jnmnjkgj', 'video game', '2009-2-08');
    INSERT INTO `userinfofreshe r` VALUES ('fsdfa', 'vdvtgert', '4536767', 'bfgbhgfhf', 'bn fghf', 'fngfn fnj', '76766', 'njfjfh', 'fjgj', 'nmghmjg', '25678', 'vcgrsh', 'fnjgj', 'xcfgjgtj', 'cgmjgj', 'cgnmgh', 'n cvnn', 'jyjtyjy', 'njghnjg', '1997-2000 S.S.L.C (State Board )Govt High School,Karur -87%', '2000-2002 XII Standard (State Board)Govt Higher Secondary School,Karur -82%', '2002-2005 Dipoma in Computer Technology Govt Polytechnic College,Karur -90%', '2002-2005 Bachelor of Computer Science Engineering in St.Joseph?s Engineering College, Anna University,Chen nai -74%', '2005-2007 Master of Computer Science Engineering in St.Joseph?s Engineering College, Anna University,Chen nai -70%', '', 'b fhytyhj', 'nghjdtj', 'ghmj', 'bdfbhfhfhfrhrh ', 'hfhdtyjtyhjtjt j2005-2007 Master of Computer Science Engineering in St.Joseph?s Engineering College, Anna University,Chen nai -70%2005-2007 Master of Computer Science Engineering in St.Joseph?s Engineering College, Anna University,Chen nai -70%2005-2007 Master of Computer Science Engineering in St.Joseph?s Engineering College, Anna University,Chen nai -70%', 'cmjmkcfku', 'jghjg', '2009-2-05');
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    Question moved to Java forum.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      1.) Use code tags when posting code.
      2.) Move all that database connection code away from the JSP into some utility class or servlet. JSPs are for displaying data only.
      3.) Check whether c[] is null or not before you try to do anything with it.

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        1.) Please list line number of your error.
        2.) Please change your program in a way that you check for EVERY possible value of the called functions first! For example:

        Your code:
        Code:
        String c[]= request.getParameterValues("chbox");
        out.println(c[0]);
        Change to:
        Code:
        String c[]= request.getParameterValues("chbox");
        if (c != null && c.length != 0) out.println(c[0]);
        else out.println("Something went wrong! I tried to access a non-existing array or the first element of an existing array that is not there!");

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by debasisdas
          Question moved to Java forum.
          Trash belongs in the trash bin, thank you.

          kind regards,

          Jos

          Comment

          • ramprakashjava
            New Member
            • Mar 2009
            • 15

            #6
            Originally posted by chaarmann
            1.) Please list line number of your error.
            2.) Please change your program in a way that you check for EVERY possible value of the called functions first! For example:

            Your code:
            Code:
            String c[]= request.getParameterValues("chbox");
            out.println(c[0]);
            Change to:
            Code:
            String c[]= request.getParameterValues("chbox");
            if (c != null && c.length != 0) out.println(c[0]);
            else out.println("Something went wrong! I tried to access a non-existing array or the first element of an existing array that is not there!");
            /* here i listed line number of error*/
            java.lang.NullP ointerException
            org.apache.jsp. deletepage_jsp. _jspService(del etepage_jsp.jav a:57)
            org.apache.jasp er.runtime.Http JspBase.service (HttpJspBase.ja va:94)
            javax.servlet.h ttp.HttpServlet .service(HttpSe rvlet.java:802)
            org.apache.jasp er.servlet.JspS ervletWrapper.s ervice(JspServl etWrapper.java: 324)
            org.apache.jasp er.servlet.JspS ervlet.serviceJ spFile(JspServl et.java:292)
            org.apache.jasp er.servlet.JspS ervlet.service( JspServlet.java :236)
            javax.servlet.h ttp.HttpServlet .service(HttpSe rvlet.java:802)

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Did you make the code changes suggested?

              Comment

              • chaarmann
                Recognized Expert Contributor
                • Nov 2007
                • 785

                #8
                Originally posted by ramprakashjava
                /* here i listed line number of error*/
                java.lang.NullP ointerException
                org.apache.jsp. deletepage_jsp. _jspService(del etepage_jsp.jav a:57)
                org.apache.jasp er.runtime.Http JspBase.service (HttpJspBase.ja va:94)
                javax.servlet.h ttp.HttpServlet .service(HttpSe rvlet.java:802)
                that's a beginning, but not enough. You should be aware that every jsp-page is converted to java and stored in Apaches "work" directory. The line number listed above references to the line inside this java file and not to a line number in the original jsp.
                In your case, your file "deletepage.jsp " is converted to "deletepage_jsp .java". The error is in line 57 of "deletepage_jsp .java" that you can find in "work" directory.

                Please just grab the line number 57 from it and list it here.

                Comment

                • ramprakashjava
                  New Member
                  • Mar 2009
                  • 15

                  #9
                  s i grabbed it that line is
                  out.println(c[0]);

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    This is turning out to be a rather pointless thread. Did you make all the suggested code changes?
                    There is no point in posting suggestions if you are just ignoring them.

                    Comment

                    • ramprakashjava
                      New Member
                      • Mar 2009
                      • 15

                      #11
                      here i eclosed few lines thats plz check it

                      out.write("\r\n ");
                      out.write("<htm l>\r\n");
                      out.write("<hea d>\r\n");
                      out.write("<tit le>Delete page</title>\r\n");
                      out.write("\r\n ");
                      out.write("</head>\r\n");
                      out.write("\r\n ");
                      out.write("<bod y>\r\n");
                      out.write(" \r\n");
                      out.write("\r\n ");
                      out.write(" ");



                      String c[]= request.getPara meterValues("ch box");
                      out.println(c[0]);



                      Connection con = null;
                      Statement stmt = null;

                      try
                      {

                      Class.forName(" com.mysql.jdbc. Driver");
                      con = DriverManager.g etConnection("j dbc:mysql://localhost:3306/ram","root","ad min");
                      stmt = con.createState ment();
                      for(String s:c)

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Read the replies that have already been posted. Why are you ignoring them?

                        Comment

                        • chaarmann
                          Recognized Expert Contributor
                          • Nov 2007
                          • 785

                          #13
                          Originally posted by ramprakashjava
                          s i grabbed it that line is
                          out.println(c[0]);
                          Ok, so you know that it's crashing because the array c[] is null or empty.
                          If you have checkboxes and you don't check them, the values are not passed, so the array that you want to get is null.
                          So c[]=request.getPar ameterValues("c hbox"); gives you exactly that null array in this case.

                          You cannot access an array that's not there. (i.e. that is null)
                          You cannot access the first element of an array if the array has no elements. (i.e. accessing c[0])
                          That's what I mean with:
                          You have to care about ALL possible values a function might return in your code. I mean every function, especially including the function request.getPara meterValues().

                          So please apply my code changes listed above. I will list them again here:
                          Code:
                          String c[]= request.getParameterValues("chbox"); 
                          if (c != null && c.length != 0) out.println(c[0]); 
                          else out.println("Something went wrong! I tried to access a non-existing array or the first element of an existing array that is not there!");
                          (and write the rest of the code in your program accordingly)

                          Then your program will work bugfree.

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            A relevant read.

                            Comment

                            Working...