NullPointer problem in JSP
Hi. I m pretty new to JSP. So i wrote this code to enter data into a database in MYSQL and den retrieve from it. Now when i press submit, it shows java.lang.NullPointerException in the next page. Heres the code:
<%@page import="java.sql.*"%>
<%@page import="java.io.*"%>
<%!
public class DbBean {
public String dbURL = "jdbc:mysql://";
String dbDriver = "com.mysql.jdbc.Driver";
private Connection dbCon;
private String getname = "";
private String getphoneno = "";
private String userName = "";
private String password = "";
private String database = "";
private String host = "";
%>
<html>
<body>
<form method="post" action="display.jsp">
ENTER NAME
<input type="text" name="getname">
PHONE NUMBER
<input type="text" name="getphoneno">
<input type=submit value=submit>
</form>
<%!
public DbBean(){
super();
}
%>
<%!
public boolean connect() throws ClassNotFoundException,SQLException,NullPointerException{
Class.forName(dbDriver);
dbCon = DriverManager.getConnection( "jdbc:mysql://localhost/cont","root","temp123");
//dbCon = DriverManager.getConnection(dbURL,userName,password);
//dbURL = "jdbc:mysql://";
//database = "";
//host = "";
return true;
}
%>
<%!
public void close() throws SQLException{
dbCon.close();
}
%>
<%!
public ResultSet execSQL(String sql)
{
ResultSet r=null;
try{
Statement st = null;
st = dbCon.createStatement();
String data = "(" + "'" + getname + "'" + "," + "'" + getphoneno + "'" + ")";
sql = "insert into contacts values " + data;
r = st.executeQuery(sql);
}
catch(SQLException e)
{
System.out.println(e);
}
return (r == null) ? null : r;
}
}
%>
</body>
</html>
Please tell me the necessary code which i might need to add. If u want i can giv the code for the nxt jsp file also which is used here.. Help will be much appreciated..
P.S- Is the code appearing all in one line?
Message was edited by:
ubarua123
[2379 byte] By [
ubarua123a] at [2007-11-27 9:28:57]

# 1
Hi,its very hard to read, so Please paste your queries in correct Format and submit your second page also, i thought you submit your first form only,
# 2
exactly.. i just copied and pasted it here and it all appeared like this..<%@page import="java.sql.*"%><%@page import="java.io.*"%>Message was edited by: ubarua123
# 3
hi,u have to copy in [ pre ]... [/ pre ] this tag, * dont give space in between square brackets ,,[ pre ]//copy your source code here[/ pre ]after we try for solution
# 4
How many times have you edited this message, vijay? =)Ubarua: just copypaste your code between [ code ] and [/ code ] tags. Also see the 'code' button right above the reply message textarea.
# 5
ok thanks :)
<%@page import="java.sql.*"%>
<%@page import="java.io.*"%>
<%!
public class DbBean {
public String dbURL = "jdbc:mysql://";
String dbDriver = "com.mysql.jdbc.Driver";
private Connection dbCon;
private String getname = "";
private String getphoneno = "";
private String userName = "";
private String password = "";
private String database = "";
private String host = "";
%>
<html>
<body>
<form method="post" action="display.jsp">
ENTER NAME
<input type="text" name="getname">
PHONE NUMBER
<input type="text" name="getphoneno">
<input type=submit value=submit>
</form>
<%!
public DbBean(){
super();
}
%>
<%!
public boolean connect() throws ClassNotFoundException,SQLException,NullPointerException{
Class.forName(dbDriver);
dbCon = DriverManager.getConnection( "jdbc:mysql://localhost/cont","root","temp123");
//dbCon = DriverManager.getConnection(dbURL,userName,password);
//dbURL = "jdbc:mysql://";
//database = "";
//host = "";
return true;
}
%>
<%!
public void close() throws SQLException{
dbCon.close();
}
%>
<%!
public ResultSet execSQL(String sql)
{
ResultSet r=null;
try{
Statement st = null;
st = dbCon.createStatement();
String data = "(" + "'" + getname + "'" + "," + "'" + getphoneno + "'" + ")";
sql = "insert into contacts values " + data;
[root@localhost add_dept]#
}
catch(SQLException e)
{
System.out.println(e);
}
return (r == null) ? null : r;
}
}
%>
</body>
</html>
finally got it right ..
Message was edited by:
ubarua123
# 6
Ubarua: use [ code ] and [/ code ] tags. Also see the link 'Formatting tips' in reply page.
# 7
ok heres the problem. I keep getting null pointer exception. Please tell the changes to be made inthe code.. Just trying to insert some values in a mysql database from an html form..
<%@page import="java.sql.*"%>
<%@page import="java.io.*"%>
<%!
public class DbBean {
public String dbURL = "jdbc:mysql://";
String dbDriver = "com.mysql.jdbc.Driver";
private Connection dbCon;
private String getname = "";
private String getphoneno = "";
private String userName = "";
private String password = "";
private String database = "";
private String host = "";
%>
<html>
<body>
<form method="post" action="display.jsp">
ENTER NAME
<input type="text" name="getname">
PHONE NUMBER
<input type="text" name="getphoneno">
<input type=submit value=submit>
</form>
<%!
public DbBean(){
super();
}
%>
<%!
public boolean connect() throws ClassNotFoundException,SQLException,NullPointerException{
Class.forName(dbDriver);
dbCon = DriverManager.getConnection( "jdbc:mysql://localhost/cont","root","temp123");
//dbCon = DriverManager.getConnection(dbURL,userName,password);
//dbURL = "jdbc:mysql://";
//database = "";
//host = "";
return true;
}
%>
<%!
public void close() throws SQLException{
dbCon.close();
}
%>
<%!
public ResultSet execSQL(String sql)
{
ResultSet r=null;
try{
Statement st = null;
st = dbCon.createStatement();
String data = "(" + "'" + getname + "'" + "," + "'" + getphoneno + "'" + ")";
sql = "insert into contacts values " + data;
r = st.executeQuery(sql);
return (r == null) ? null : r;
}
catch(SQLException e)
{
System.out.println(e.getMessage());
return null;
}
}
}
%>
</body>
</html>
help will be appreciated ..
Message was edited by:
ubarua123
# 8
hi,
*) first u check weather connection is success or not,
*)
public [b]ResultSet [/b]execSQL(String sql)
{
ResultSet r=null;
try{
Statement st = null;
st = dbCon.createStatement();
String data = "(" + "'" + getname + "'" + "," + "'" + getphoneno + "'" + ")";
sql = "insert into contacts values " + data;
[b] r = st.executeQuery(sql);
return (r == null) ? null : r;[/b]
}
catch(SQLException e)
{
System.out.println(e.getMessage());
return null;
}
here why you use ResultSet unnessary,
Your aim is to insert the values into database, so you use executeUpdate () or PreparedStatement,
public void execSQL(String sql)
{
try{
Statement st = null;
st = dbCon.createStatement();
String data = "(" + "'" + getname + "'" + "," + "'" + getphoneno + "'" + ")";
sql = "insert into contacts values " + data;
st.executeUpdate ( sql );
System.out.println ( "successfully inserted.. ");
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
}
i hope it will help you..
# 9
nope data is not getting entered into the database :(..
heres the display.jsp which i call for displaying:
<%@page import="java.util.*"%>
<%@page import="java.sql.*"%>
<%@page import="java.lang.*"%>
<%!
ResultSet rs = null;
Connection conn = null;
Statement st2 = null;
String name123 = null;
String phone_no=null;
%>
<html>
<body>
<%
try
{
st2 = conn.createStatement();
rs=st2.executeQuery("select * from contacts");
while(rs.next())
{
name123=rs.getString("name");
phone_no=rs.getString("phoneno");
session.setAttribute("Name",name123);
session.setAttribute("Phone number",phone_no);
}
}
catch (Exception e)
{
out.println(e);
}
%>
<h1>Items from the database</h1>
<table border="1">
<tr>
<td>Name</td>
<td><%=(name123!=null?name123:"")%></td>
</tr>
<tr>
<td>Phone number</td>
<td><%=(phone_no!=null?phone_no:"")%></td>
</tr>
</table>
</body>
</html>
the output that i m getting is:
java.lang.NullPointerException
ITEMS FROM THE DATABASE
and a table wid null values.. simply dont get it..
# 10
Where is your connection Statement in this jsp ,
before u confirm weather record is insert or not,
if insert, connection establish is correct or check any socket problem while u making connection,
or try
manually insert one recode in your database and retrieve, if its works or not we can understand easily...
# 11
Get and read the stacktrace of the NPE. Go to the line which where the NPE is being thrown. Interpret the line yourself and ask yourself which reference call caused the NPE. Finally fix it by instantiating that reference -for example.
# 12
ok i added a connection code to the display.jsp
like this :
<%
//public boolean connect()
//{
try{
//throws ClassNotFoundException,SQLException,NullPointerException{
Class.forName(dbDriver);
dbCon = DriverManager.getConnection( "jdbc:mysql://localhost/cont","root","temp123");
//dbCon = DriverManager.getConnection(dbURL,userName,password);
//dbURL = "jdbc:mysql://";
//database = "";
//host = "";
}
catch(Exception e)
{
System.out.println("your error is here i.e connection error");
}
//return true;
//}
and i also made the following changes to the data retrieval part
try
{
st2=dbCon.createStatement();
rs=st2.executeQuery("select * from contacts");
//while(rs.next())
//{
name123=rs.getString("name");
phone_no=rs.getString("phoneno");
//session.setAttribute("Name",name123);
//session.setAttribute("Phone number",phone_no);
//}
}
catch (Exception e)
{
out.println("Your connection is ok, but you have a null pointer exception");
}
the error comes in the catch. can ne1 giv me a sample code of how to retrieve the data from mysql?.. i even tried entering data manually but just shows null values in the table..
# 13
} catch (Exception e) {
out.println("Your connection is ok, but you have a null pointer exception");
}
Why are you suppressing exceptions here with a dumb out.println which states a wrong assumption?
Print the stacktrace and read it. There you can find the cause of the exception.
# 14
catch (NullPointerException e)
{
//out.println("Caught it" + e);
e.printStackTrace();
}
the first statement gave the output:
caught it java.lang.NullPointerException
ITEMS IN THE DATABASE
the e.printStackTrace() gave no error messages! just a simple
ITEMS FROM DATABASE and an empty table.. really weird stuff going around. I even made the 2 fields in the mysql table as NOT NULL.
Can ne1 copy paste my code and try to run it in their own system?.. then i can know whether the java compiler itself is at fault or something..
# 15
It does not print to the screen, but to the console/logfile.