views:

126

answers:

2

Hi everyone,

I'm a newbie on Java EE and got a problem that I don't understand why it's happening. Here is my problem :

I can't access a JavaBean using <jsp:getProperty> which is created in a scriptlet in the same page.It's throwing the below exception :

Attempted a bean operation on a null object.

Here is my bean and jsp page :

package com.webProject3.beans;

public class User implements java.io.Serializable {

private static final long serialVersionUID = 1209333714609490385L;
private int id;
private String lastName;
private String firstName;

public User(){

}

public void setId(int id) {
 this.id = id;
}
public int getId() {
 return id;
}
public void setLastName(String lastName) {
 this.lastName = lastName;
}
public String getLastName() {
 return lastName;
}
public void setFirstName(String firstName) {
 this.firstName = firstName;
}
public String getFirstName() {
 return firstName;
}

}

<body>  
  <%  
  com.webProject3.beans.User requestedUser = new com.webProject3.beans.User();  
  requestedUser.setFirstName("Peter");  
  requestedUser.setLastName("Petrelli");  
  %>  

 <p><jsp:getProperty name="requestedUser" property="firstName"></jsp:getProperty></p>  
</body>
+3  A: 

Your problem is that you're trying to use a variable that is out of scope. You need to use the variable in the same scope as you've instantiated.

try this:

<body>
<jsp:useBean id="user" class="com.webProject3.beans.User"/>
<%
  requestedUser.setFirstName("Peter");  
  requestedUser.setLastName("Petrelli"); 
%>

Then you access using EL notation as follows:

${user.firstName}
${user.lastName}

etc...

Jesse
Yes, when I explored the generated servlet code(displayUserInfo_jsp.java) as McDowell said I saw that when I try to access the bean it was searching it in the page scope but the instance created in scriptlet wasn't in this scope so I was getting a null object related exception like in my first post.
Can Doralp
+3  A: 

For the sake of consistency, it is probably better to stick to one generation of JSP technology. That said, don't let idealism stand in the way of pragmatism.

Scriptlets + expressions (recipe for spaghetti code):

<%
    com.webProject3.beans.User requestedUser = new com.webProject3.beans.User();
    requestedUser.setFirstName("Peter");
%>
<%=requestedUser.getFirstName()%>

Standard actions:

<jsp:useBean id="user" class="com.webProject3.beans.User">
    <jsp:setProperty name="user" property="firstName" value="Peter" />
</jsp:useBean>
<jsp:getProperty name="user" property="firstName" />

JSTL (custom tags), using a standard action to instantiate the bean:

<jsp:useBean id="name" class="com.webProject3.beans.User">
    <c:set target="${name}" property="firstName" value="Peter" />
</jsp:useBean>
<c:out value="${name.firstName}" escapeXml="false" />

If the beans are instantiated in a controller servlet instead of the JSP, standard actions can often be dropped from JSTL code too.

The introduction of custom tags paved the way for other frameworks, so often JSTL isn't used either.

To understand the exact cause of your error, see if you can find the generated servlet code (if any). In Tomcat, this ends up in a work directory with a name like org/apache/jsp/foo_jsp.java

McDowell
+1 a detailed answer.
Mercer Traieste