I'm really new to Java and JSTL so sorry if this is a super simple question. I'm trying to take an example I found online and get it to work but I seem to be running into problems. All that is suppose to happen is you create a java bean and access properties from the java bean. But instead I'm getting a null pointer exception on the line that I'm calling the class in the JSTL jsp:useBean id="students" class="com.beans.Students". Here is the java class:
package com.beans;
public class Students implements java.io.Serializable
{
private String firstName = null;
private int age = 0;
public Students() {
}
public String getFirstName(){
return firstName;
}
public int getAge(){
return age;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public void setAge(Integer age){
this.age = age;
}
}
JSTL in which I'm trying to access Java bean:
<jsp:useBean id="students"
class="com.beans.Students">
<jsp:setProperty name="students" property="firstName"
value="Zara"/>
<jsp:setProperty name="students" property="age"
value="10"/>
</jsp:useBean>
<p>Student First Name:
<jsp:getProperty name="students" property="firstName"/>
</p>
<p>Student Age:
<jsp:getProperty name="students" property="age"/>
</p>
Stack trace:
Caused by: org.apache.sling.api.SlingException: An exception occurred processing JSP page /students.jsp at line 18
at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.handleJspExceptionInternal(JspServletWrapper.java:574)
at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:499)
at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
at org.apache.sling.scripting.jsp.JspServletWrapperAdapter.service(JspServletWrapperAdapter.java:59)
at org.apache.sling.scripting.jsp.JspScriptEngineFactory$JspScriptEngine.eval(JspScriptEngineFactory.java:453)
at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:358)
... 174 more
Caused by: java.lang.NullPointerException
at org.apache.sling.scripting.jsp.jasper.runtime.JspRuntimeLibrary.internalIntrospecthelper(JspRuntimeLibrary.java:322)
at org.apache.sling.scripting.jsp.jasper.runtime.JspRuntimeLibrary.introspecthelper(JspRuntimeLibrary.java:308)
at org.apache.jsp.students_002d_jsp._jspServ
Any help is greatly appreciated!
If you've put a Students
object on the request in your servlet doing something like request.setAttribute("students", myStudentObject);
, then the JSTL equivalent of what you have on your page would be this:
<p>Student First Name: <c:out value="${students.firstName}"/></p>
<p>Student Age: <c:out value="${students.age}"/></p>
Make sure you include the JSTL core tags at the top of your page like this:
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
Using the c:out
tag is good if you need to worry about XSS attacks, but if that's not a concern you can just skip the c:out
tag and use an EL expression like this:
<p>Student First Name: ${students.firstName}</p>
<p>Student Age: ${students.age}</p>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With