Getting a null pointer exception when loading my JSP result page
Hi all,
I am working on an EJB application which contains some JSP pages.
This is the index page
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Metric to Imperial Converter</title>
</head>
<body>
<h1>Meter to Yard Converter</h1>
<form action="Converted.jsp" method="POST">
Enter Meter Amount: <input type="text" name="metricamount" value="" />
<br>
Enter Yard Amount: <input type="text" name="imperialamount" value="" />
<br>
<input type="submit" value="Submit" /><br>
</form>
</body>
This is the page it goes to once the user hits the submit button
<title>Metric and Imperial converter</title>
</head>
<body>
<h1>Converted Values</h1>
<%!
private MyConverterRemote converter = null;
public void jspInit() {
try {
InitialContext ic = new InitialContext();
converter = (MyConverterRemote)
ic.lookup(MyConverterRemote.class.getName());
} catch (Exception ex) {
System.out.println("Couldn't create converter bean." + ex.getMessage());
}
}
public void jspDestroy() {
converter = null;
}
%>
<%
String MetricAmount = request.getParameter("metricamount");
String ImperialAmount = request.getParameter("imperialamount");
if (MetricAmount != null && MetricAmount.length() > 0) {
BigDecimal d = new BigDecimal(MetricAmount);
BigDecimal metAmount = converter.MetrictToImp(d);
%>
<p>
<%= MetricAmount %> Meters are <%= metAmount %> Yards
<p>
<%
BigDecimal e = new BigDecimal(ImperialAmount);
BigDecimal impAmount = converter.MetrictToImp(e);
%>
<p>
<%= ImperialAmount %> Yards are <%= impAmount %> Meters
<p>
<%
}
%>
</body>
When this page loads I get this exception
org.apache.jasper.JasperException: java.lang.NullPointerException
root cause
java.lang.NullPointerException
If anyone can point me to the right direction it would be awesome
Thanks!
Re: Getting a null pointer exception when loading my JSP result page
you are retrieving a null converter and when you try BigDecimal impAmount = converter.MetrictToImp(e) the NullPointerException fires
check the code where you get the converter:
converter = (MyConverterRemote)ic.lookup(MyConverterRemote.cla ss.getName());
it is possible in your context there is no such an object