| Author |
Cannot insert data into table and throwing null message at Exception
|
Danny Baquilod
Greenhorn
Joined: Jun 26, 2011
Posts: 13
|
|
Hello, can somebody help.
I can't insert data into table and it thows null message.
Netbeans displays : Error null (captured at the Exception)
I used the same script for other servlet to insert to other table and it is working fine.
Thanks in advance
Danny
========================================
1.) import java.io.IOException;
2.) import java.io.PrintWriter;
3.) import javax.servlet.ServletException;
4.) import javax.servlet.http.HttpServlet;
5.) import javax.servlet.http.HttpServletRequest;
6) import javax.servlet.http.HttpServletResponse;
7) import java.sql.*;
8) import java.text.DateFormat;
9) import java.text.ParseException;
10) import java.text.SimpleDateFormat;
11) import java.util.Date;
12)
13) public class AddUser extends HttpServlet {
14) Date newDate;
15)
16) protected void processRequest(HttpServletRequest request, HttpServletResponse response)
17) throws ServletException, IOException {
18) response.setContentType("text/html;charset=UTF-8");
19) PrintWriter out = response.getWriter();
20)
21) String userId = request.getParameter("txtUserId");
22) String userName = request.getParameter("txtUserName");
23) String dept = request.getParameter("txtDept");
24) String costCenter = request.getParameter("txtCostCenter");
25) String desig = request.getParameter("txtDesig");
26)
27) try {
28) Class.forName("oracle.jdbc.driver.OracleDriver");
29) String serverName = "localhost";
30) String portNumber = "1521";
31) String sid = "dbsin";
32) String url = "jdbc racle:thin:@" + serverName + ":" + portNumber + ":" + sid;
33) String username = "wpjfk";
34) String password = "wpjfk";
35) Connection conn = DriverManager.getConnection(url, username, password);
36)
37) Statement stmt = conn.createStatement();
38) String sql = "insert into sys_user_mast values (?,?,?,?,?,?)";
39) PreparedStatement pst = conn.prepareStatement(sql);
40)
41) String dateCreate = request.getParameter("dateCreate");
42) SimpleDateFormat formater = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
43) Date dateCreated = formater.parse(dateCreate);
44) newDate = dateCreated;
45)
46) pst.setString(1, userId);
47) pst.setString(2, userName);
48) pst.setString(3, dept);
49) pst.setString(4, costCenter);
50) pst.setString(5, desig);
51) pst.setDate(6, new java.sql.Date(newDate.getTime()));
52)
53) int numRowsAdded = pst.executeUpdate();
54)
55) out.println(" Record saved successfully : "+numRowsAdded);
56) pst.close();
57)
58)
59) }catch (Exception e){
60) System.out.println("Error"+e.getMessage());
61)
62) } finally {
63) out.close();
64
65 }
}
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
That probably means that something was null in a place which can't handle nulls. Unfortunately you don't know where that place is, because when you printed the exception you didn't print that information. So take line 60 and print the stack trace:
(There's no point in printing the word "Error", when you see the stack trace you'll know that an error occurred anyway.)
The stack trace will tell you what line of your code threw the exception, so start there.
|
 |
Danny Baquilod
Greenhorn
Joined: Jun 26, 2011
Posts: 13
|
|
Paul Clapham wrote:That probably means that something was null in a place which can't handle nulls. Unfortunately you don't know where that place is, because when you printed the exception you didn't print that information. So take line 60 and print the stack trace:
(There's no point in printing the word "Error", when you see the stack trace you'll know that an error occurred anyway.)
The stack trace will tell you what line of your code threw the exception, so start there.
Ok, now after I've e.printStackTrace I got the following error :
java.lang.NullPointerException
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1235)
at java.text.DateFormat.parse(DateFormat.java:335)
at AddUser.doPost(AddUser.java:80)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Danny Baquilod wrote:java.lang.NullPointerException
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1235)
at java.text.DateFormat.parse(DateFormat.java:335)
at AddUser.doPost(AddUser.java:80)
The only parsing I see is that of dateCreate which is initialized here:
This error indicates that dateCreate is null, which means that the parameter is not present.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Danny Baquilod
Greenhorn
Joined: Jun 26, 2011
Posts: 13
|
|
Rob Spoor wrote:
Danny Baquilod wrote:java.lang.NullPointerException
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1235)
at java.text.DateFormat.parse(DateFormat.java:335)
at AddUser.doPost(AddUser.java:80)
The only parsing I see is that of dateCreate which is initialized here:
This error indicates that dateCreate is null, which means that the parameter is not present.
Yes, that's the culprit
Thanks Rob. It's working now :)
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
You're welcome.
|
 |
 |
|
|
subject: Cannot insert data into table and throwing null message at Exception
|
|
|