Getting exception : java.lang.NullPointerException on my jsp page - java

i am posting my code in which i am getting the java.lang.NullPointerException exception.
<%# page contentType="text/html;charset=UTF-8" %>
<%# page errorPage="myError.jsp?from=customers.jsp" %>
<%# page import="java.sql.*" %>
<html>
<head>
<title>Insurance Quoting System</title>
</head>
<body>
<basefont face="Arial">
<!-- JSP Declarations -->
<%! ResultSet rs = null; %>
<!-- JSP Scriptlet -->
<%
try {
Class.forName("com.mysql.mysql.Driver");
Connection db = DriverManager.getConnection("jdbc:mysql://localhost:3306/quoting","root","root");
Statement s = db.createStatement();
rs = s.executeQuery("select * from customer");
}
catch (Exception e) {
// For now, just report the error to the system log
System.out.println(e.toString());
}
%>
<!-- Template text -->
<table width="550" border="0" align="center">
<tr>
<td bgcolor="#006633">
<div align="center">
<font size="6" color="#FFFFFF">
<b>Insurance Quoting System</b>
</font>
</div>
</td>
</tr>
<tr>
<td>
<p> </p>
<p> </p>
<p align="center"><b>Customers</b></p>
<table width="290" border="0" align="center">
<%
try {
while (rs.next()) {
%>
<!-- JSP Expressions used within template text -->
<tr>
<td width="20"><%= rs.getInt(1) %></td>
<td width="70"><%= rs.getString(2) %></td>
<td width="70"><%= rs.getString(3) %></td>
<td width="40">
<a href="custMaint.jsp?id=<%= rs.getString(1) %>&action=edit">
edit
</a>
</td>
<td width="40">
<a href="custMaint.jsp?id=<%= rs.getString(1) %>&action=delete">
delete
</a>
</td>
<td width="40">
<a href="custMaint.jsp?id=<%= rs.getString(1) %>&action=newQuote">
new quote
</a>
</td>
</tr>
<%
}
}
catch (SQLException e) {
// For now, just report the error to the system log
System.out.println(e.toString());
}
%>
</table>
</td>
</tr>
<tr>
<td>
<p> </p>
<p align="center">New Customer</p>
</td>
</tr>
</table>
</body>
</html>
i am making one jsp page its giving some error java.lang.NullPonterException on my jsp page pls tell me the ans if any one have .

The only thing I can think of that is causing this is that you are catching an exception in your first try-catch block and then trying to use the ResultSet as if it's been initialized, but it's still null, so you are getting NPE. Check to see if your Database operations work correctly.

Related

jsp forward tag is not working in jsp

I have made a login page and a forward tag is not working it creates error which i can not able to find out.
I uses access database and after clicking sign in button it will check, if user is already registerd then page should forward next page but it does not so.
Here is my code:
<form action="login.jsp" method="get">
<table>
<tr></tr>
<td><br>
<label>EmailID:</label>
</td>
<td>
<input type="text" name="emailid" maxlength="50" size="30">
</td>
<tr></tr>
<td><br>
<label>Password:</label>
</td>
<td>
<input type="password" name="passwordid" maxlength="50" size="30">
<p>
</p>
</td>
<tr></tr>
<td></td>
<td><input type="submit" value="sign in"></center></td>
<%
try
{
String existemail="registration",existpass="12345";
ResultSet rs, rs1;
String emailid1=request.getParameter("emailid");
String passwordid1=request.getParameter("passwordid");
if(emailid1.length()!=0 && passwordid1.length()!=0)
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:collegep");
Statement st = conn.createStatement();
rs=st.executeQuery("select password from collge where
email='"+emailid1+"'");
while(rs.next())
{
existpass= rs.getString(1);
}
if(existpass.equals(passwordid1))
{
<jsp:forward page=
"afterlogin.html" />
}else
{ %> invalid combination of email and password<%
}
}
else
{` %>
<div id="error">all field must be filled.</div>
<%
}
}
catch(Exception e){}
%>
Please try changing from:
<jsp:forward page:
to:
<jsp:forward page=
It seems like you used inappropriate Scriplet Tags(<% %>)..close scriptlet end tag(%>) above ur <jsp:forward page="afterlogin.html"/>
All java code should be in Scriplet Tag only..and html code outside of it..make this changes

html code tables is not workinig

i cannot show any values and i dont get any error messages. . here is my code:
Number: <input type="text" name="number" />
<br>
<% Persone =(Employqq)request.getAttribute("cust"); %>
<table id="table" border="1">
<tbody>
<tr>
<th>Number</th>
</tr>
for(Employee t : emp){ %>
<tr>
<td>
<%= t.getnumber() %>
</td>
<% } } %>
</tbody>
</table>
i appreciate all the help that i can get
You set the attribute in your servlet as Cust, and in your JSP you read it as cust

The type java.lang.Charsequence cannot be resolved. It is indirectly referenced from required .class file

<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%# taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<%# taglib uri="http://jakarta.apache.org/struts/tags-logic"
prefix="logic"%>
<%# page import="de.fhg.fokus.hss.db.model.*, java.util.* " %>
<jsp:useBean id="resultList" type="java.util.List" scope="request"></jsp:useBean>
<jsp:useBean id="maxPages" type="java.lang.String" scope="request"></jsp:useBean>
<jsp:useBean id="currentPage" type="java.lang.String" scope="request"></jsp:useBean>
<jsp:useBean id="rowPerPage" type="java.lang.String" scope="request"></jsp:useBean>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" type="text/css" href="/hss.web.console/style/fokus_ngni.css">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title><bean:message key="result.title" /></title>
<script type="text/javascript" language="JavaScript">
function submitForm(pageId){
document.IMSU_SearchForm.crtPage.value = pageId;
document.IMSU_SearchForm.submit();
}
function rowsPerPageChanged(){
document.IMSU_SearchForm.crtPage.value = 1;
document.IMSU_SearchForm.submit();
}
</script>
</head>
<body>
<table id="title-table" align="center" weight="100%" >
<tr>
<td align="center">
<h1> IMS Subscription - Search Results </h1>
<br/><br/>
</td>
</tr>
</table> <!-- title-table -->
<table id="main-table" align="center" valign="middle" >
<tr>
<td>
<table id="result-table" class="as" width="600" border="0" cellspacing="1" align="center" style="border:2px solid #FF6600;">
<tr class="header">
<td class="header"> ID </td>
<td class="header"> Name </td>
<td class="header"> S-CSCF Name </td>
<td class="header"> Diameter Name </td>
</tr>
<%
if (resultList != null && resultList.size() > 0){
IMSU imsu;
int idx = 0;
Iterator it = resultList.iterator();
while (it.hasNext()){
imsu = (IMSU) it.next();
%>
<tr class="<%= idx % 2 == 0 ? "even" : "odd" %>">
<td>
<%= imsu.getId() %>
</td>
<td>
<a href="/hss.web.console/IMSU_Load.do?id=<%= imsu.getId() %>">
<%= imsu.getName() %>
</a>
</td>
<td>
<%= imsu.getScscf_name() %>
</td>
<td>
<%= imsu.getDiameter_name() %>
</td>
</tr>
<%
idx++;
} //while
} // if
else{
%>
<tr>
<td>
<bean:message key="result.emptyResultSet" />
</td>
</tr>
<%
}
%>
</table> <!-- result-table -->
</td>
</tr>
<tr>
<td colspan="3" class="header">
<html:form action="/IMSU_Search">
<table id="rows-table" align="center">
<tr>
<td>
<%
int length = Integer.parseInt(maxPages) + 1;
int cPage = Integer.parseInt(currentPage) + 1;
for (int iy = 1; iy < length; iy++) {
if (cPage != iy) {
%>
<%=iy%>
<%
} else {
%>
<font style="color:#FF0000;font-weight: 600;">
<%=String.valueOf(iy)%>
</font>
<% }
}
%>
</td>
<td>
<bean:message key="result.rowsPerPage" /><br>
<html:hidden property="crtPage"></html:hidden>
<html:select property="rowsPerPage" onchange="javascript:rowsPerPageChanged();">
<option value="20"
<%= rowPerPage.equals("20") ? "selected" : "" %> >20 </option>
<option value="30"
<%= rowPerPage.equals("30") ? "selected" : "" %> >30 </option>
<option value="50"
<%= rowPerPage.equals("50") ? "selected" : "" %> >50</option>
<option value="100"
<%= rowPerPage.equals("100") ? "selected" : "" %> >100</option>
</html:select>
</td>
</tr>
</table> <!-- rows-table -->
</html:form>
</td>
</tr>
</table> <!-- main-table -->
</body>
</html>
I am trying to run a appliaction, I am accessing the application through a web interface but at some part I am getting the following error:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 116 in the jsp file: /pages/user/imsu_search_result.jsp
Generated servlet error:
The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:397)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1063)
org.apache.struts.tiles.TilesRequestProcessor.doForward(TilesRequestProcessor.java:263)
org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:386)
org.apache.struts.tiles.TilesRequestProcessor.processForwardConfig(TilesRequestProcessor.java:318)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:229)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.9 logs.
Please find the attached jsp file
Please provide your suggestion to resolve the issue.
Thanks
1) Check your compiler and JVM versions as I believe they mismatch OR
2) Compile your all classes with newer version of 1.8 or
3) downgrade to Java 1.7

how to get value using model and scriptlets

I am creating a table in JSP in which i am displaying some values using hard code.
i want to display those value using scriptlets and model. Someone help me how to do that?
<tr>
<td>Automobile</td>
<td>11-JUN-2015</td>
<td>10-FEB-2016</td>
<td>Cars and bikes</td>
</tr>
Getting values from the database:
<%rst=stmt.executeQuery("select * from auto_details");%>
Sample Html Table:
<html>
<body>
<center>
<h2>AutoList</h2>
<table border="1" cellspacing="0" cellpadding ="0">
<tr>
<td><b>S.No</b></td>
<td><b>Date</b></td>
<td><b>typeofvichele</.b></td>
</tr>
<%
int no=1;
while(rst.next()){
%>
<tr>
<td><%=no%></td>
<td><%=rst.getString("date")%></td>
<td> <%=rst.getString("typeofvichele")%> </td>
</tr>
<%
no++;
}
rst.close();
stmt.close();
con.close();
%>
</table>
</center>
</body>
</html>

login page using session in jsp

I've created a login environment using jsp. index.jsp, login.jsp. I take username and password from database. If user name and password matches with the database it does login process perfectly. When user give wrong name or password it shows a error message invalid name or passwordand redirect to the login page. Nothing's wrong, but I am facing a problem when I login first. The place where the error message is shown after submitting wrong name or password that place is showing null.
Why null is showing?
Below is my code
index.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Example</title>
</head>
<body>
<form action="login.jsp" method="post">
<center>
<table border="1" width="30%" cellpadding="3">
<thead>
<tr>
<th colspan="2" align ="left">Login Here</th> <h5><%=request.getAttribute("errorMessage")%> </h5>
</tr>
</thead>
<tbody>
<tr>
<td>User Name</td>
<td><input type="text" name="uname" value="" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass" value="" /></td>
</tr>
<tr>
<td><input type="submit" value="Login" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
</tbody>
</table>
</center>
</form>
</body>
</html>
login.jsp
<%# page import ="java.sql.*" %>
<%
String userid = request.getParameter("uname");
String pwd = request.getParameter("pass");
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/test", "postgres", "root");
Statement st = con.createStatement();
ResultSet rs;
rs = st.executeQuery("select * from member where uname='" + userid + "' and pass='" + pwd + "'");
if (rs.next()) {
session.setAttribute("userid", userid);
response.sendRedirect("success.jsp");
} else {
request.setAttribute("errorMessage", "Invalid user or password");
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
%>
You can use this in your index.jsp
<% if ((String) request.getAttribute("errorMessage") != null) {%>
<h3 style="color: red;"><%=(String) request.getAttribute("errorMessage")%></h3>
<%
}
%>
Instead of this
<%=request.getAttribute("errorMessage")%>
please check condition before showing error, if request parameter is null then it should not be rendered else it would be rendered, modify you code according to below snippet
<thead>
<tr>
<th colspan="2" align ="left">Login Here</th>
<h5>
<%if(request.getParameter("errorMessage")!=null){%>
<%=request.getParameter("errorMessage")%><%}%>
</h5>
</tr>
</thead>
The errorMessage attribute will not be available in request for the first time. Kindly check for a null and then display the message.
<%=request.getAttribute("errorMessage")%>
The below piece of code could help you.
<tr>
<th colspan="2" align ="left">Login Here</th>
<h5>
<%
String errorMsg = request.getAttribute("errorMessage");
if (errorMsg != null) {
%>
<%=errorMsg%>
<%
}
%>
</h5>
</tr>
As errorMessage attribute is null in the request when you load it first time, so you are getting null. You need to add null check for it. You can do this way
<th colspan="2" align ="left">Login Here</th>
<% if (request.getAttribute("errorMessage") != null) {
out.println("<h5>" + request.getAttribute("errorMessage") + "</h5>");
}%>
Correct your code in login.jsp
write session.setAttribute("errorMessage", "Invalid user or password");
instead of request.setAttribute("errorMessage", "Invalid user or password");
..
null message show because of this.

Categories