iam getting an NullPointerException while i run my jsp page.

Here is my code,I dont understand where am i doing a mistake.<%@ page info="Company Master" import="java.sql.*" session="true" %>

<html>

<head>

<script language="javascript">

function f1()

{

var x=document.company.code.value.toUpperCase();

if(x!=null)

document.company.code.value=x;

}

</script>

</head>

<body bgcolor="yellow"> <font color="red"></font>

<h2><center>Company Master </center></h2>

<form name="company">

<pre>

Code : <input type=text name="code">

Name : <input type=text name="name">

Address : <textarea name="add" rows=5 cols=30></textarea>

Contact Person : <input type=text name="contact">

Phone : <input type=text name="ph">

Mail Id : <input type=text name="mail">

<input type=submit name="sub" value="Insert"> <input type=submit name="sub" value="Query">

<input type=submit name="sub" value="Clear"><input type=submit name="sub" value="Back">

</pre>

</form>

<%!

String code,name,add,contact,ph,mail,butt;

String code1=" ",name1=" ",add1=" ",contact1=" ",ph1=" ",mail1=" ",xx1=" ";

Connection con;

ResultSet rs;

public void jspInit()

{

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con=DriverManager.getConnection("jdbc:odbc:plk","scott","tiger");

}catch(Exception e)

{

System.out.println("error in Connection"+e);

}

}

%>

<%

PreparedStatement ps = con.prepareStatement("insert into Company values(?,?,?,?,?,?)");

PreparedStatement ps1 = con.prepareStatement("select * from Company where upper(code)=?");

code=request.getParameter("code");

name=request.getParameter("name");

add=request.getParameter("add");

contact=request.getParameter("contact");

ph=request.getParameter("ph");

int uph=0;

try

{

uph=Integer.parseInt(ph);

}catch(NumberFormatException e)

{

System.out.println(e.getMessage());

}

mail=request.getParameter("mail");

butt=request.getParameter("sub");

if(code!=null && name!=null && add!=null && butt.equals("Insert"))

{

try

{

ps.setString(1,code);

ps.setString(2,name);

ps.setString(3,add);

ps.setString(4,contact);

ps.setInt(5,uph);

ps.setString(6,mail);

int i=ps.executeUpdate();

System.out.println(i+"row is inserted");

}

catch(Exception e)

{

%>

<jsp:forward page="Error.html" />

<%

}

}

if(code!=null && butt.equals("Query"))

{

try

{

ps1.setString(1,code.toUpperCase());

rs=ps1.executeQuery();

System.out.println("query"+rs.next());

code1=rs.getString(1);

name1=rs.getString(2);

add1=rs.getString(3);

contact1=rs.getString(4);

ph1=rs.getString(5);

mail1=rs.getString(6);

}

catch(Exception e)

{

%>

<jsp:forward page="Error.html" />

<%

}

}

if(code!=null && butt.equals("Clear"))

{

code1="";

name1="";

add1="";

contact1="";

ph1="";

mail1="";

}

if( butt.equals("Back"))

{

%>

<a href="http://localhost:8080/Admin.html"></a>

<%

}

%>

</body>

</html>

[4583 byte] By [ljava123] at [2007-9-26 1:53:07]
# 1

try initializing your Connection and ResultSet objects to null....i.e.,

Connection con=null;

ResultSet rs=null;

If the initialization is not working, then maybe your connection object is null, meaning the database driver driver class could not be found by your JSP. in this case, include in your import statement, the jdbc-obdc driver via the statement:

<%@ page import="sun.jdbc.odbc.JdbcOdbcDriver"%>

in addition, revise your jspInit() method to catch a more specific exception, the ClassNotFoundException. These are ways to make sure that your connection object is not null durign the execution of your prepared statement.

here's a suggested revision of your jspInit() method:

public void jspInit()

{

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con=DriverManager.getConnection("jdbc:odbc:plk","scott","tiger");

}catch (java.lang.ClassNotFoundException ex){

System.out.println("Driver class not found" + ex);

}catch (SQLException esql) {

System.out.println("Cannot get connection" + esql);

}

since you now catching an specific exception, go to your application server's System.out.println log to check the specific error. I got a strong feeling that your JSP runner cannot see the path where your JDBC driver is.

I hope this one helps.

Pulat85

pulat85 at 2007-6-29 3:03:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

I tried with all that,then also i am getting the same error and in my application server,the error is null.

Here is my error:

Error during JSP page processing

java.lang.NullPointerException

at pagecompile.jsp._Company._jspService(_Company.java:206)

at com.sun.server.http.pagecompile.jsp.runtime.HttpJspBase.service(HttpJspBase.java:87)

at javax.servlet.http.HttpServlet.service(Compiled Code)

at com.sun.server.http.pagecompile.jsp.runtime.JspServlet.runServlet(JspServlet.java:469)

at com.sun.server.http.pagecompile.jsp.runtime.JspServlet.processJspPage(JspServlet.java:259)

at com.sun.server.http.pagecompile.jsp.runtime.JspServlet.service(JspServlet.java:97)

at javax.servlet.http.HttpServlet.service(Compiled Code)

at com.sun.server.ServletState.callService(ServletState.java:226)

at com.sun.server.ServletManager.callServletService(ServletManager.java:936)

at com.sun.server.ProcessingState.invokeTargetServlet(ProcessingState.java:423)

at com.sun.server.http.HttpProcessingState.execute(HttpProcessingState.java:79)

at com.sun.server.http.stages.Runner.process(Runner.java:79)

at com.sun.server.ProcessingSupport.process(Compiled Code)

at com.sun.server.Service.process(Service.java:204)

at com.sun.server.http.HttpServiceHandler.handleRequest(HttpServiceHandler.java:374)

at com.sun.server.http.HttpServiceHandler.handleRequest(Compiled Code)

at com.sun.server.HandlerThread.run(Compiled Code)

can anyone please tell me where is the mistake.

Thanks in advance.

ljava123 at 2007-6-29 3:03:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Your code:

>try

>{

>uph=Integer.parseInt(ph);

>}catch(NumberFormatException e)

>{

>System.out.println(e.getMessage());

>}

maybe you cat try this :

try

{

if (ph!=null)

uph=Integer.parseInt(ph);

}catch(NumberFormatException e)

{

System.out.println(e.getMessage());

}

selina_c at 2007-6-29 3:03:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...