• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Harshal Gurav
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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..
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rajkumar balakrishnan
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


You are Welcome
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
reply
    Bookmark Topic Watch Topic
  • New Topic