469,330 Members | 1,290 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,330 developers. It's quick & easy.

Delete table records using checkbox in jsp

hi,
i hav "java.lang.NullPointerException" 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.sql.*"%>
<html>
<head>
<title>Admin Page</title>
<script language="javascript">

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

}

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

}


</script>
</head>

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

<td width="14%">&nbsp;&nbsp;&nbsp;&nbsp;Date</td>
<td width="32%">Name</td>
<td width="40%">Email Id</td>
<td width="14%">Contact No</td>

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

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


try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ram","root","sun");
stmt = con.createStatement();

String qy = "select * from userinfofresher";

rs = stmt.executeQuery(qy);

while(rs.next())
{
%>
<table width="95%">
<tr><td width="3%"><input type="checkbox" name="chbox" value="<%=rs.getString(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.getMessage());
}

%>

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

</head>

<body>


<%

String c[]= request.getParameterValues("chbox");
out.println(c[0]);

Connection con = null;
Statement stmt = null;

try
{

Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ram","root","sun");
stmt = con.createStatement();
for(String s:c)
{
String qy = "delete from userinfofresher where emailid='"+s+"' ";
stmt.executeUpdate(qy);
out.println("Successfully Deleted");
}

}
catch(Exception e)
{

out.println(e.getMessage());
}
%>

</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_CHECKS=0;
-- ----------------------------
-- Table structure for userinfofresher
-- ----------------------------
DROP TABLE IF EXISTS `userinfofresher`;
CREATE TABLE `userinfofresher` (
`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 `userinfofresher` VALUES ('RAMPRAKASH K A', 'ramprakash.java@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,Chennai -74%', '', '', 'C,java/j2ee', 'HCL technologies,Ambattur,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 `userinfofresher` VALUES ('ram', 'ramprakash_ka@yahoo.co.in', '5454418745', '5,dsghjkghdjkafjf', 'gkjdflgkdfj', 'mvn', '54655646', '6,ssjkfhjhdfjjl', '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\nvcslmklgmdklm\r\nvdfm,;dm\r\nvm vd', 'jnmnjkgj', 'video game', '2009-2-12');
INSERT INTO `userinfofresher` VALUES ('ram', 'ramprakash_kas@yahoo.co.in', '5454418745', '5,dsghjkghdjkafjf', 'gkjdflgkdfj', 'mvn', '54655646', '6,ssjkfhjhdfjjl', '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\nvcslmklgmdklm\r\nvdfm,;dm\r\nvm vd', 'jnmnjkgj', 'video game', '2009-2-11');
INSERT INTO `userinfofresher` VALUES ('ram', 'ramprakash_k@yahoo.co.in', '5454418745', '5,dsghjkghdjkafjf', 'gkjdflgkdfj', 'mvn', '54655646', '6,ssjkfhjhdfjjl', '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\nvcslmklgmdklm\r\nvdfm,;dm\r\nvm vd', 'jnmnjkgj', 'video game', '2009-2-09');
INSERT INTO `userinfofresher` VALUES ('ram', 'ramprakash_a@yahoo.co.in', '5454418745', '5,dsghjkghdjkafjf', 'gkjdflgkdfj', 'mvn', '54655646', '6,ssjkfhjhdfjjl', '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\nvcslmklgmdklm\r\nvdfm,;dm\r\nvm vd', 'jnmnjkgj', 'video game', '2009-2-09');
INSERT INTO `userinfofresher` VALUES ('ram', 'ramprakash@yahoo.co.in', '5454418745', '5,dsghjkghdjkafjf', 'gkjdflgkdfj', 'mvn', '54655646', '6,ssjkfhjhdfjjl', '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\nvcslmklgmdklm\r\nvdfm,;dm\r\nvm vd', 'jnmnjkgj', 'video game', '2009-2-08');
INSERT INTO `userinfofresher` 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,Chennai -74%', '2005-2007 Master of Computer Science Engineering in St.Joseph?s Engineering College, Anna University,Chennai -70%', '', 'b fhytyhj', 'nghjdtj', 'ghmj', 'bdfbhfhfhfrhrh', 'hfhdtyjtyhjtjtj2005-2007 Master of Computer Science Engineering in St.Joseph?s Engineering College, Anna University,Chennai -70%2005-2007 Master of Computer Science Engineering in St.Joseph?s Engineering College, Anna University,Chennai -70%2005-2007 Master of Computer Science Engineering in St.Joseph?s Engineering College, Anna University,Chennai -70%', 'cmjmkcfku', 'jghjg', '2009-2-05');
Mar 3 '09 #1
13 26421
debasisdas
8,127 Expert 4TB
Question moved to Java forum.
Mar 5 '09 #2
r035198x
13,262 8TB
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.
Mar 5 '09 #3
chaarmann
785 Expert 512MB
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:
Expand|Select|Wrap|Line Numbers
  1. String c[]= request.getParameterValues("chbox");
  2. out.println(c[0]); 
  3.  
Change to:
Expand|Select|Wrap|Line Numbers
  1. String c[]= request.getParameterValues("chbox");
  2. if (c != null && c.length != 0) out.println(c[0]);
  3. 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!");
  4.  
Mar 5 '09 #4
JosAH
11,448 Expert 8TB
@debasisdas
Trash belongs in the trash bin, thank you.

kind regards,

Jos
Mar 5 '09 #5
@chaarmann
/* here i listed line number of error*/
java.lang.NullPointerException
org.apache.jsp.deletepage_jsp._jspService(deletepa ge_jsp.java:57)
org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
Mar 6 '09 #6
r035198x
13,262 8TB
Did you make the code changes suggested?
Mar 6 '09 #7
chaarmann
785 Expert 512MB
@ramprakashjava
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.
Mar 6 '09 #8
s i grabbed it that line is
out.println(c[0]);
Mar 6 '09 #9
r035198x
13,262 8TB
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.
Mar 6 '09 #10
here i eclosed few lines thats plz check it

out.write("\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write("<title>Delete page</title>\r\n");
out.write("\r\n");
out.write("</head>\r\n");
out.write("\r\n");
out.write("<body>\r\n");
out.write(" \r\n");
out.write("\r\n");
out.write(" ");



String c[]= request.getParameterValues("chbox");
out.println(c[0]);



Connection con = null;
Statement stmt = null;

try
{

Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ram","root","admin");
stmt = con.createStatement();
for(String s:c)
Mar 6 '09 #11
r035198x
13,262 8TB
Read the replies that have already been posted. Why are you ignoring them?
Mar 6 '09 #12
chaarmann
785 Expert 512MB
@ramprakashjava
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.getParameterValues("chbox"); 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.getParameterValues().

So please apply my code changes listed above. I will list them again here:
Expand|Select|Wrap|Line Numbers
  1. String c[]= request.getParameterValues("chbox"); 
  2. if (c != null && c.length != 0) out.println(c[0]); 
  3. 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!"); 
  4.  
(and write the rest of the code in your program accordingly)

Then your program will work bugfree.
Mar 9 '09 #13
r035198x
13,262 8TB
A relevant read.
Mar 10 '09 #14

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Jacky Luk | last post: by
1 post views Thread by bigabytes | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.