aspose file tools*
The moose likes JSP and the fly likes error :java.lang.NullPointer Exception in searchuser.jsp page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "error :java.lang.NullPointer Exception in searchuser.jsp page " Watch "error :java.lang.NullPointer Exception in searchuser.jsp page " New topic
Author

error :java.lang.NullPointer Exception in searchuser.jsp page

Harshal Gurav
Ranch Hand

Joined: May 29, 2008
Posts: 151
Hi,
I am creating the updateuser.jsp page. For that I create search.jsp where one can serch users and after submitting this page.it should display the updateuser.jsp page where I can update the details of user. Here is my code of searchuser.jsp:
<% Connection connection = null;
Statement st = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mynewdatabase","root", "root123");
/// st=connection.createStatement();///
try {
rs = st.executeQuery("Select * from user order by useid Asc");
while (rs.next())
{
%>
<option value = "<%=rs.getString("userid")%>" >
<%=rs.getString("emailid") + " "+rs.getString("groupid")%>
</option>
<%
}

}
finally
{
if (rs != null)
{
rs.close();
rs = null;
}
if (st != null)
{
st.close();
st = null;
}

}
%>
but shows error at above marked line as:

java.lang.NullPointerException
org.apache.jsp.searchuser_jsp._jspService(searchuser_jsp.java:91)

Well, whether it is right way to retrive data and shows to user?
Any suggestion is highly appreciated.
Thanks and Regards
Harshal
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Try to post the code after formatting it.. And post the searchuser.jsp code also.And as far i see your code how could you retrieve your data without any arguments???


Never try to be a hard-worker. Be a smart-worker.
My Blog
Harshal Gurav
Ranch Hand

Joined: May 29, 2008
Posts: 151
Hi, after modifieng the code ,the searchuser.jsp page are working. now i am trying to display the data retrieved form table in the same page in drop-down list format.my above page is running but not showing data retrived from database in drop dowm box. here is code:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page language ="java" %>
<%@ page import="java.sql.*, javax.sql.*, javax.naming.*,java.io.*,java.util.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form name="searchuser" action="updateuser.jsp" method="POST">

<table border="1" width="100">
<thead>
<tr>
<th>Seach criteria</th>
</tr>
</thead>
<tbody>
<tr>
<td><b>Username:</b> </td>
<td><select name="form1">
<% Connection connection = null;
Statement st = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mynewdatabase","root", "root123");
st=con.createStatement();
try {
rs = st.executeQuery("Select * from user order by userid Asc");
while (rs.next())
{
%>
<option value = "<%=rs.getString("user")%>" >
</option>
<%
}

}
finally
{
if (rs != null)
{
rs.close();
rs = null;
}
if (st != null)
{
st.close();
st = null;
}

}
%>
</select>
</td>
</tr>
<tr align="center">
<td><input type="submit" value="Submit" name="submit" /></td>
</tr>

</tbody>
</table>


</form>
</body>
</html>

Kindly help me.
Thanks and Regards
Haresh
[ July 10, 2008: Message edited by: Harshal Gurav ]
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445



What you are doing here? I don't know..You have to place the values in the option and not in option value attribute... So the code will be somewhat looks like this



Try that and it works fine..
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Good that you got it resolved and it is working fine without any errors! You can follow the suggestions given by Rajkumar to get the values displayed in the drop-down box. You can also refer the w3schools.com website for a quick tutorial on the HTML Forms and controls.

Originally posted by Harshal Gurav:

Well, whether it is right way to retrive data and shows to user?


It is *not* the appropriate way to retrieve the data. As per the MVC approach, you should not combine the responsibilities of fetching the data and displaying it at one component. It should be separate so that they are loosely coupled.

In short, your JSP should just display the values and NOT get involved in database related operations.

Try reading about MVC2 and incorporate it.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Yes it's true Raghavan.. Separating business logic and presentation logic will improve the maintainability and reuse of code... But may be he is a beginner and try to do some work in JSP and then he'll realize the use of MVC pattern. So start leaning MVC pattern from now and you will be good enough when you finish it.
Harshal Gurav
Ranch Hand

Joined: May 29, 2008
Posts: 151
Thanks a lot Rajkumar and Raghavan.your code is working fine.
I am working at one client side independenly from 2- months. i am just newbie here. There is no one here to help me other than online forum and most important is like you
helpful person.
Thanks again
Harshal
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Thanks a lot Rajkumar and Raghavan.your code is working fine.

You are Welcome
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Harshal Gurav:
There is no one here to help me other than online forum and most important is like you
helpful person.


That's why this forum is here and all of us are too

Hope you got your query clarified. You are most welcome for any further queries. But seriously try to read, understand and implement the MVC in your applciation . Get back to Ranch with your queries :thumb:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: error :java.lang.NullPointer Exception in searchuser.jsp page