Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8873801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:33:19+00:00 2026-06-14T18:33:19+00:00

I am trying to add an object into an ArrayList stored in the session.

  • 0

I am trying to add an object into an ArrayList stored in the session.

The object doesn’t seem to be added.

Please check my below code and suggest me a correct answer for this.

While debugging my debugger goes till this code books.add(book); and then it skips the remaining code. I know problem is here books.add(book); . Book detail is not adding into my arraylist books.

AddBookToSession.java

    public class AddBookToSession extends ActionSupport {
         Bookdetails book=new Bookdetails();
            List<Bookdetails> books = new ArrayList<Bookdetails>();

            BookdetailsDAO dao = new BookdetailsDAO(); 
      Map session = ActionContext.getContext().getSession();

        public String execute()
        { 
            HttpServletRequest request = ServletActionContext.getRequest();  
            String bookid = request.getParameter("bid");    
            books = (List) session.get( BillTransactionBooksConstants.BOK ); 
          for(int i=1;i<=1;i++)
        {
          book = dao.listBookDetailsById(Integer.parseInt(bookid));    
          books.add(book); //problem is here obj book is not adding into books list

          books = new ArrayList<Bookdetails>();

          System.out.println("---------------Bookid-"+book); 
        }
        ....................
                ...............

struts.xml

        <package name="testwithrowselect" extends="struts-default" >  
                    <action name="InsertbooksToSession" class="v.esoft.actions.booktransaction.AddBookToSession">
                        <result name="success">/jspHomepage/bookstransaction/shortBill.jsp</result>  
                    </action>
         </package>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:33:21+00:00Added an answer on June 14, 2026 at 6:33 pm

    You add a new, empty list to session:

    for (int i = 1; i <= 1; i++) {
        book = dao.listBookDetailsById(Integer.parseInt(bookid));    
        books.add(book);
        books = new ArrayList<Bookdetails>(); // <-- Now it's a new, empty list?!
    }
    session.put(BillTransactionBooksConstants.BOK, books);
    

    That said, your action code should look something closer to this (untested):

    public class AddBookToSession extends ActionSupport implements SessionAware {
    
        private Integer bid;
        private Map<String, Object> session;
    
        private BookdetailsDAO dao = new BookdetailsDAO(); 
    
        public String execute() { 
            BookDetails book = dao.listBookDetailsById(Integer.parseInt(bookid));
            if (book != null) {
                List<Bookdetails> books = sessionBooks();
                books.add(book);
            }
    
            return SUCCESS;
        } 
    
        private List<Bookdetails> sessionBooks() {
            List<Bookdetails> books = (List) session.get(BillTransactionBooksConstants.BOK);
            if (books == null) {
                books = new ArrayList<BookDetails>();
                session.put(BillTransactionBooksConstants.BOK, books);
            }
            return books;
        }
    
        public void setSession(Map<String, Object> session) {
            this.session = session;
        }
    
        public Integer getBid() {
            return this.bid;
        }
    
        public void setBid(Integer bid) {
            this.bid = bid;
        }
    }
    

    Note that instantiating a book DAO like this may make testing a bit more difficult than necessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I am trying to add an object into an Arraylist, I am using
Trying to add a class object into a List using reflection, but when invoking
I want to add every element of an arraylist into a single object (Which
I am trying to find out the syntax to add a complex object into
Greetings everyone- In my code below I'm trying to add a Row from an
I'm trying to add two object that they are in the same class. In
I am trying to add the datetime object of a person. Whenever the birth
I'm trying to add results from an object to newly created HTML elements: *long
I am trying to add time to a Date object in javascript but am
I'm trying to add a region using Azure Datacache object but I'm getting such

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.