1 Reply Latest reply on Apr 25, 2011 10:51 AM by bertoz84

    ERROR [STDERR] java.lang.NullPointerException - can't solve

    Newbie

      Hi all, i really need help to overcome this error.

      I'm going to paste the interesting parts of my code:

       

      TEST BEAN ( we are inside a method called by the java test client ):

           .

           .

           .

            try {

                                              System.out.println("lowering the amount of the selected book");

                                              Book abook = (Book) bookop.findByISBN(80002).get(0);  // Book is an entity bean --- bookop is the istance of a session bean that performs                                                                                                 // operations on Book

                  bookop.updateMin(abook.getISBN(), 2); // tot - 2

       

                   // OPERATION updateMin DONE SUCCESSFULLY

       

            } catch (Exception e) {

                                              e.printStackTrace();

            }

       

       

            Users an_user = (Users) user.findByUser("mouse").get(0);  // Users is an entity bean --- user is the istance of a session bean that performs                                                                                                   // operations on Users

            cart.checkoutCart(an_user); // cart is the istance of a session statefull bean that performs classic operations like in a web site ( bookshop )

                                                       // to see what this method go down

       

      -end

       

       

      CART BEAN ( the called method ):


           public void checkoutCart(Users customer) {

                  for (int i = 0; i < books.size(); i++)   // where books is: private List<CartBook> books , a simple class made up of a Book and the quantity                                                                    // to purchase of that book

                                    {

                                                   Book abook = books.get(i).getBook();

                                                   Integer quantity = books.get(i).getQuantity();

       

                       try {

                               System.out.println("lowering the amount of the selected book");

                              DBOperation.updateMin(abook.getISBN(), quantity); // DBOperation is the same as bookop seen before

       

                               // ERROR [STDERR] java.lang.NullPointerException

       

                                                   } catch (Exception e) {

                               e.printStackTrace();

                                                   }

                                    }

           }

       

      -end

       

       

      so basically the code is the same; the difference is that going through the cart bean i can lower the quantity of a list of books ( that are the books that a customer puts in his cart: when he performs a checkout operation the method updateMin lowers the amount of every books purchased in the store ) instead of only one.

       

      the problem is that through the cart i get always the ERROR [STDERR] java.lang.NullPointerException in the DBOperation.updateMin methods that is exactly the same of bookop.updateMin.

      Every cicle of the for( ) command i get this error.

      I can assure that my list is not empty and abook & quantity are not null.

       

       

       

      I really don't get the problem!

       

      Thank you in advance!!

        • 1. Re: ERROR [STDERR] java.lang.NullPointerException - can't solve
          Newbie

          I think i got the problem:

          the null pointer is the entity manager, but why?

           

          that is a part of the structure of my whole project:

           

                         java client

                         |

                                                       | ( calls a method of )

                         v

                       test stateless bean

                         |

                                                       | ( calls a method of )

                         v

                         Cart operations statefull bean

          ( here i create an entity manager to perform transactional operations and call em.flush )

                         |

                                                       | ( calls a method of )

                         v

                         Book operations steless bean

          ( here i create an entity manager to perform transactional operations and call em.createQuery )

                         |

                                                                                        | ( create the records in the DB - hybernate )

                         v

                         Book entity bean

           

           

           

          I create the em this way:

           

          @PersistenceContext

          EntityManager em;

           

          both in the CART and BOOK OPERATIONS BEANS.

           

          The first calls to the entity manager, such as em.createQuery(....) inside the BOOK OPERATIONS BEAN return a null pointer ( so i think the em is not a valid pointer ). If i call the same method of the BOOK OPERATIONS BEAN called in the CART BEAN but from the TEST BEAN ( so i avoid using the CART BEAN ) i don't get problems.

          I think there's a problem with more than one istance of the em or with the statefull bean but i can't get what's the point.

           

          someone can help me?