Urgent please help me!

java.lang.NullPointerException

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

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

at javax.servlet.http.HttpServlet.service(HttpServlet.java:840)

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(HttpServlet.java:840)

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)

My code is:<%@ 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=null;

ResultSet rs=null;

public void jspInit()

{

try

{

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

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

}catch(java.lang.ClassNotFoundException e)

{

System.out.println("class not found"+e.getMessage());

}

catch(SQLException seq)

{

System.out.println("error in connection"+seq);

}

}

%>

<%

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

{

if(ph!=null)

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>

Please can any one tell where i am doing the mistake,what that error is:

Thanks

[6388 byte] By [plalitha] at [2007-9-26 2:21:42]
# 1

java.lang.NullPointerException

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

Assuming that your jsp was _Company.jsp,

and without (understanding) further details of what you are trying to do,

I personally would go into the Java code mentioned above

and specifically to line 207 and try to trace it back. May not be the best way,

but then when nothing else works, do it the hard way!

Also, IMHO its NOT a good practice to have all the JDBC code

in your JSP. Try approaching this problem with Java beans.

regds.

- madhav

mlakkapr at 2007-6-29 9:27:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

hi,

the problem is that you are calling the page the first time and there is no value in the form fields. but you have not done any validation for this and the jsp portion is also executing when you call the page the first time.

please add the foll. lines and the code works.

<%@ 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>

/*new additions */

<%

if (request.getParameter("code")!=null)

{

%>

/* addition ends heresee the closing braces also */

<%!

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

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

Connection con=null;

ResultSet rs=null;

public void jspInit()

{

try

{

Class.forName("oracle.jdbc.driver.OracleDriver");

con=DriverManager.getConnection("jdbc:oracle:thin:@vamsoft:1521:vamora","scott","lion");

}catch(java.lang.ClassNotFoundException e)

{

System.out.println("class not found"+e.getMessage());

}

catch(SQLException seq)

{

System.out.println("error in connection"+seq);

}

}

%>

<%

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

{

if(ph!=null)

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>

<%

}

/* extra clsoing brace */

}

/* extra closing brace

%>

</body>

</html>

My error is:

vams00 at 2007-6-29 9:27:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...