Dear Everyone,
I am getting NullPointerException in the
JSP in the line <% for (int i=0;i<a.size();i++)
Please correct the error. I am a beginner that's why I am using scriptlet.
Regards,
Salman
package net.codejava;
public class Category {
private int id;
private
String name;
public Category(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
--------------------------------------------------------------------
package net.codejava;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class DAO
{
Connection con ;
{
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookstoredb","root","");
}
catch(java.lang.Exception ex)
{
}
}
public Category list()
{
String sql = " SELECT category_id,name FROM category where name = 'JSP' ";
Statement statement;
try {
statement = con.createStatement();
ResultSet rs = statement.executeQuery(sql);
if(rs.next())
{
int id = rs.getInt("category_id");
String name = rs.getString("name");
Category x = new Category(id, name);
rs.close();
return x;
}
else
{
return null;
}
}
catch (SQLException e)
{
return null;
}
}
}
--------------------------------------------------------------------
package net.codejava;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ListServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
DAO e= new DAO();
Category t = e.list();
int a= t.getId();
String b = t.getName();
Category product= new Category(a,b);
request.setAttribute("product",product);
String url = "/index.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
}
}
--------------------------------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@page import="net.codejava.Category"%>
<%@page import="net.codejava.DAO"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.ArrayList"%>
<html>
<head>
<title></title>
</head>
<%
ArrayList<Category> a = (ArrayList<Category>)request.getAttribute("listCategory");
%>
<body>
<table cellspacing="5" border="0">
<tr>
<td align="right"><p>Category:</td>
<td><select name="category">
<% for (int i=0;i<a.size();i++)
{
Category g = a.get(i);
%>
<option value="<%=g.getId() %>" >
<%=g.getName() %>
</option>
<%
}
%>
</select>
</td>
</tr>
</table>
</body>
</html>
-------------------------------------------------------------
MYSQL
create database bookstoredb;
use bookstoredb;
CREATE TABLE `category` (
`category_id` int(11) NOT NULL,
`name` varchar(30) NOT NULL
);
INSERT INTO `category` (`category_id`, `name`) VALUES
(1, '
Java'),
(2, 'PHP'),
(3, 'C'),
(4, 'JSP');