NullPointerException
I wonder why this coding could not work since i really need to pass the selected choice inside, giving me real time data change from the database.I guess this is the structure I need to to so.The jsp just couldn't read from the parameter "name" but if i directly insert a string into the varaible answer, it will work. Is there anything can be done to fix this? It is from the 'Passing Parameter' topic.
test.jsp
<%@ page language="java" import="java.util.*"%>
<%@ page import="java.sql.*,java.io.*,javax.servlet.*,javax.servlet.http.*,java.lang.String
"%>
<html>
<head>
<script type="text/javascript">
function change()
{
var answer = document.getElementById('selc').value;
window.location.replace("test.jsp?name="+answer);
<%
String connectionURL = "jdbc:mysql://localhost:3306/test";
Connection connection=null;
ResultSet rs;
response.setContentType("text/html");
String price = "0";
String answer = request.getParameter("name");
try {
// Load the database driver
Class.forName("com.mysql.jdbc.Driver");
// Get a Connection to the database
connection = DriverManager.getConnection(connectionURL, "root", "123");
//Select the data from the database
String sql = "select * from message where message = '"+answer+"'";
Statement s = connection.createStatement();
s.executeQuery (sql);
rs = s.getResultSet();
while (rs.next ()){
price = rs.getString("price");
}
rs.close ();
s.close ();
}
catch(Exception e)
{
System.out.println("Exception is ;"+e);
}
%>
document.getElementById('textvalue').value= <%= price %>;
}
</script>
</head>
<body>
<h1>Use of Select Box in JSP</h1>
<table border="1" width="41%" height="53" cellspacing="0" cellpadding="3"
bgcolor="#000080" bordercolorlight="#FFFFFF">
<tr>
<td width="100%" height="18" colspan="2"><b>
<font color="#FF00FF">Select items from select box</font></b></td>
</tr>
<tr>
<td width="17%" height="23">
<select name="ActionSelect" onChange="change()" id="selc" >
<%Iterator itr;%>
<% List data= (List)request.getAttribute("data");
for (itr=data.iterator(); itr.hasNext(); )
{
String value=(String)itr.next();
%>
<option value=<%=value%>><%=value%></option>
<%}%>
</select>
</td>
<td width="83%" height="23"><input type="text" id="textvalue" />
</td>
</tr>
</table>
</body>
</html>
_________________________________________________________________________
DataServlet.java
package Servlet;
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DataServlet extends HttpServlet{
private ServletConfig config;
//Setting JSP page
String page="test.jsp";
public void init(ServletConfig config)
throws ServletException{
this.config=config;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
//Establish connection to MySQL database
String connectionURL = "jdbc:mysql://localhost:3306/test";
Connection connection=null;
ResultSet rs;
response.setContentType("text/html");
List dataList=new ArrayList();
try {
// Load the database driver
Class.forName("com.mysql.jdbc.Driver");
// Get a Connection to the database
connection = DriverManager.getConnection(connectionURL, "root", "123");
//Select the data from the database
String sql = "select message from message";
Statement s = connection.createStatement();
s.executeQuery (sql);
rs = s.getResultSet();
while (rs.next ()){
//Add records into data list
dataList.add(rs.getString("message"));
}
rs.close ();
s.close ();
}
catch(Exception e)
{
System.out.println("Exception is ;"+e);
}
request.setAttribute("data",dataList);
//Disptching request
RequestDispatcher dispatcher = request.getRequestDispatcher(page);
if (dispatcher != null){
dispatcher.forward(request, response);
}
}
}
___________________________________
message db
id message price
5 amit 5
1 amir 6
2 raghuwanshi 12
3 raghuw 100
4 suman 149.99
6 calamity 99.99
7 muthusamy 99.99
View Answers
April 20, 2010 at 5:02 PM
Hi Friend,
We have made changes in jsp file 'test.jsp'and there is no need to use DataServlet.Here, we have taken our database table fields.
test.jsp:
<%@ page language="java" import="java.util.*"%>
<%@ page import="java.sql.*,java.io.*,javax.servlet.*,javax.servlet.http.*,java.lang.String
"%>
<html>
<head>
<script type="text/javascript">
function change()
{
var answer = document.getElementById('selc').value;
window.location.replace("test.jsp?name="+answer);
}
</script>
</head>
<%
String connectionURL = "jdbc:
mysql://localhost:3306/register";;
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:
mysql://localhost:3306/test";, "root", "root");
String ans=request.getParameter("name");
response.setContentType("text/html");
String address = "";
String name = "";
try {
String sql = "select * from data where name = '"+ans+"'";
Statement s = connection.createStatement();
s.executeQuery (sql);
ResultSet rs = s.getResultSet();
while (rs.next ()){
address = rs.getString("address");
name=rs.getString("name");
}
rs.close ();
s.close ();
}
catch(Exception e){
System.out.println("Exception is ;"+e);
}
System.out.println(address);
%>
<body>
<h1>Use of Select Box in JSP</h1>
<table border="1" width="41%" height="53" cellspacing="0" cellpadding="3"
bgcolor="#000080" bordercolorlight="#FFFFFF">
<tr>
<td width="100%" height="18" colspan="2"><b>
<font color="#FF00FF">Select items from select box</font></b></td>
</tr>
<tr>
<td width="17%" height="23">
<select onChange="change();" id="selc" >
<%
Statement st=connection.createStatement();
ResultSet rs=st.executeQuery("select name from data");
while(rs.next()){
%>
<option value="<%=rs.getString("name")%>"><%=rs.getString("name")%></option>
<%
if(name.equals(rs.getString("name"))){
%>
<option value="<%=rs.getString("name")%>"selected=selected><%=rs.getString("name")%>></option>
<%
}
}%>
</select>
</td>
<td width="83%" height="23"><input type="text" id="textvalue" value="<%= address %>" />
</td>
</tr>
</table>
</body>
</html>
Thanks
Related Tutorials/Questions & Answers:
nullpointerExceptionnullpointerException sir i create the on login module in that mysql generate the
nullpointerException?
please send login mdule code in jsp and servlet using mvc model?
Please visit the following link:
Jsp Servlet
Advertisements
NullPointerException - SQLNullPointerException Greeting Everyone.
I got
NullPointerException when try to run UPDATE query. Here's the code where JSP dies:
String short...) with
NullPointerException
TOMCAT ERROR:
root cause
Java NullPointerException
Java
NullPointerException
NullPointerException is a kind of unchecked exception...
class.
NullPointerException must bethrown when an application attempts
JDK 1.4 the NullPointerException - Java TutorialsNullPointerException In Java
NullPointerException occurs when you perform an operation on a
object or calling a method on the object. The only way to solve Null
Pointer Exception is to avoid executing operation on a object
Java exceptionJava exception What is
NullPointerException and how to handle
SCJP Module-6 Question-30Given below the sample code :
1 try {
2 // code
3 } catch (
NullPointerException ne) {
4 System.out.print("
NullPointerException ");
5 } catch... } finally {
8 System.out.print("END");
9 }
If
NullPointerException Java null pointer exception handling pointer exception handling
What is Java
NullPointerException? In Java
NullpointerException is a RuntimeException and occurs when it confronts a null variable where an object is required. Java
nullpointerexception never provide details
ExceptionException what is
NullPointerException?
Hi,
I java
NullPointerException java.lang.NullPointerException) is defined in the java.lang package.ADS_TO_REPLACE_1
The
NullPointerException is thrown by the Java program
SCJP Module-6 Question-24 Exception");
} catch (
NullPointerException npe) {
System.out.println("...; Compilation error
2. General Exception
3.
NullPointerException
4... for
NullPointerException. It is already handled by the
catch block
javajava 1.Create a class Errorhandle1. Write code that deliberately throws the following exceptions, without using the ââ?¬Å?throwââ?¬Â? keyword
ClassCastException
NullPointerException
ArrayIndexOutOfBoundsException
exceptionexception 1.Create a class Errorhandle1. `Write code that deliberately throws the following exceptions, without using the â??throwâ?? keyword :
ClassCastException
NullPointerException
ArrayIndexOutOfBoundsException
making use java image filtersmaking use java image filters while making use any functions built in jerry's filter in java application i get a
NullPointerException.. I dont know the reason for this.. please give me a solution
Httpservletresponse null pointer exception.Httpservletresponse null pointer exception. What is Httpservletresponse null pointer exception?
NullPointerException occurs when we try to implement an application without referencing the object and allocating
SCJP Module-6 Question-2 exceptions
' ArrayIndexOfBoundException ' , '
NullPointerException... Function(Ingredient[] list) throws
ArrayIndexOfBoundException,
NullPointerException...;ArrayIndexOfBoundException or
NullPointerException SCJP Module-6 Question-20Given below the sample code :
try {
// code
} catch (
NullPointerException npe) {
System.out.print("hello");
} catch (RuntimeException rte) {
System.out.print("brothers");
} finally {
System.out.print("
Array in Java; //right
s[0].p1 = 1; //error
NullPointerException..., why s[0].p1 = 1 is
NullPointerException java,eclipse - Swing AWT
// Stack: 1, Locals: 1
public
NullPointerException();
0 aload_0
1...
NullPointerException(java.lang.String arg0);
0 aload_0
1 aload_1
2
JSP jasper expection - JSP-Interview QuestionsJSP jasper expection What is JSP jasper expection? Answer: JasperException is a subclass of Exception, you can use the usual Exception methods on it. But it's not a
NullPointerException Null Pointer ExceptionNull Pointer Exception whis is the Null Pointer Exception in java? or define Null Pointer Exception with exp?
NullPointerException occurs, when you try to implement an application without referencing the object
Struts 2.1.8 Features in earlier versions. Now is it fixed.
A new fix to
NullPointerException...".
A fix to a
NullPointerException is also added. The
NullPointerException Logging an Exception in Java, blank array is
under the
NullPointerException, blank stack... and
NullPointerException by Logger object. Program contains two method DivByZero... of
NullPointerException
that catch the error when the array is
blank. ADS
Exception handling in java.
NullPointerException.
Class Cast Exception
Unchecked Exception
Reason... an Arithmetic Exception.
NullPointerException.
NullPointerException exception is displayed when we implement an
application without referring
Java - Java BeginnersJava 1.How do you declare the starting point of a Java application?
2. What happened if your program terminates with an OutOfMemoryError, or NoClassDefFoundErroror
NullPointerException? Hi,
Let's see how you can
N - Java Glossary that is included in jdk1.1.
Java
NullPointerException
NullPointerException is a kind of unchecked exception
that occurs when an application
Making Exceptions Unchecked - java tutorial,java tutorials of the common examples are : ArithmeticException,
NullPointerException, ClassCaseException... {
throw new
NullPointerException("demo");
} catch (
NullPointerException e... (
NullPointerException e) {
System.out.println("Recaught :" + e
File.list() method as d:/java it is giving
NullPointerException.
but if I directly write code like
Java Exception NullPointerException if a logic error causes a null to be passed... referenced an object throws
NullPointerException.
Checked Exceptions... for FileReader. However, the constructor will throw
NullPointerException if a logic error
Java - JDBC(InterruptedException ie){
}
throw new
NullPointerException("Application test: throwing an
NullPointerException It should arrive at the console");
}
}
public