And
And

Reputation: 343

How to put an object into JSP variable?

I've got collection of objects I want use in my custom tag.But I can get the element of collection only once,because it iterates to next after getting. So I decided to define a variable. But it doesn't work.

<jsp:useBean id="rw" scope="request" class=
"by.epam.web.libruary.transferobject.AvailableBookSet">
    <c:set var="element"
        value="${rw.element}"
        scope="page"/>
    <mytag:bodyattr num="${rw.size}">
        <form name="orderBook" method="POST" action="${controllerpage}">
            <input type="submit" name="submit" 
              value="${rw.element.shortinfo}">
        </form>
    </mytag:bodyattr>
</jsp:useBean>

Here is the code of collection itself:

public class AvailableBookSet extends java.util.HashSet {
    private java.util.Iterator it;

    @SuppressWarnings("unchecked")
    public AvailableBookSet(){

            try {
                    //Retrieving info from DataBase (100% working code)

            } catch (CannotTakeConnectionException e) {
            }
    }

    public String getSize(){
            it = this.iterator();
            return Integer.toString(this.size());
    }

    public Object getElement(){
            return it.next();
    }

}

And here is my exception:

org.apache.jasper.JasperException: An exception occurred processing JSP page /jsp/available_books.jsp at line 51

48:     
49:     <jsp:useBean id="rw" scope="request" class=
50:     "by.epam.web.libruary.transferobject.AvailableBookSet">
51:         <c:set var="element"
52:             value="${rw.element}"
53:             scope="page"/>
54:         <mytag:bodyattr num="${rw.size}">

Will be very grateful for your help.

Upvotes: 0

Views: 3935

Answers (1)

Beau Grantham
Beau Grantham

Reputation: 3456

Unless you've omitted some key code in your post, it is null when you are invoking the .getElement() method. The call to .next() will then throw a NullPointerException.

Try reversing the order of these two lines:

<c:set var="element" value="${rw.element}" scope="page"/>
<mytag:bodyattr num="${rw.size}">

Upvotes: 1

Related Questions