EJB: Object not found exception

I have a servlet which is communicating with a couple of EJB's which include both session and entity beans. When I run the program it seems to run for six records and then throws up an "javax.ejb.ObjectNotFoundException: Bean not found in ................"

My program is;

public class NewsletterTestServlet extends HttpServlet
    private static Logger log = Logger.getLogger(NewsletterTestServlet.class);

     * Loop through all newsletters. Get office for each newsletter.  Get all users of that office
     * who are subscribed and send them the email
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
        EmailGenerator emailGenerator = new EmailGenerator();
        InputStream newsletterStream  = request.getSession().getServletContext().getResourceAsStream(
            int newsbastsuccess = 0;

            EcpPortalSessionLocal epsl = (EcpPortalSessionLocal) request.getSession().getAttribute(

            // Get all custom newsletters and iterate
            String unsubscribeUrlBase = "http://" + request.getServerName() + ":" + request.getServerPort()
                    + request.getContextPath() + "/servlet/unsubscribe.do?";
            //Collection newsletters = CustomNewsletterTypeUtil.getLocalHome().findAll();
            Connection con = AcuvueFunctions.getConnection();
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(new StringBuffer("SELECT cn.CUSTOMNEWSLETTERPK, nm.MESSAGE, cn.CREATIONDATE, ot.OFFICEPK ,ot.OFFICENAME, nt.SUBSCRIPTIONPK, nt.NEWSLETTERNAME, ")
                  .append("       NVL(cat.ADDRESS1, ' ') ADDRESS1, NVL(cat.ADDRESS2, ' ') ADDRESS2, NVL(cat.SUITE, ' ') SUITE, ")
                  .append("       NVL(cat.CITY, ' ') CITY, NVL(cat.STATE, ' ') STATE, NVL(cat.ZIPCODE, ' ') ZIPCODE, ")
                  .append("       DECODE(NVL(cet.ISPUBLIC, 0), 1, EMAILADDRESS, 'PRIVATE')  EMAILADDRESS, ")
                  .append("       DECODE(NVL(cpt.ISPUBLIC, 0), 1, PHONENUMBER, 'PRIVATE') PHONENUMBER ")
                  .append("  FROM CUSTOMNEWSLETTER cn, ")
                  .append("       NEWSLETTERMESSAGE nm, ")
                  .append("       NEWSLETTERTYPE nt, ")
                  .append("       OFFICETYPE ot, ")
                  .append("       CONTACTADDRESSTYPE cat, ")
                  .append("       CONTACTEMAILTYPE cet, ")
                  .append("       CONTACTPHONETYPE cpt ")
                  .append(" WHERE cn.CUSTOMMESSAGE = nm.NEWSLETTERMESSAGEPK AND ")
                  .append("       nm.NEWSLETTERPK = nt.NEWSLETTERPK AND ")
                  .append("       cn.OFFICEPK = ot.OFFICEPK AND ")
                  .append("       ot.emailpk = cet.emailpk (+) AND ")
                  .append("       ot.phonepk = cpt.phonepk (+) AND ")
                  .append("       ot.ADDRESSPK = cat.ADDRESSPK (+)").toString());

                        while (rs.next())
                        { // Start of loop
                              String office = rs.getString("OFFICEPK");
      System.out.println("Office pk recvd :***"+ office);

                              // Get all users for this office and
                              // see if user is subscribed to the newsletter

      System.out.println("Inside while here 1");
                              Collection users = UserTypeUtil.getLocalHome().findByOffice(office);
      System.out.println("Inside while here 2");

                              log.info("Found " + users.size() + " users");
      System.out.println("The no: of users for this office :****"+users.size());
                              Iterator usersIter = users.iterator();
                              while (usersIter.hasNext())
                              { // Inner loop
                                    UserTypeLocal userLocal = (UserTypeLocal) usersIter.next();
                                    UserTypeLocalValue user = userLocal.getUserTypeLocalValue();
                                    Collection subscriptions = UserSubscriptionTypeUtil.getLocalHome().findUserSubscriptionTypeLocalsByUser(user.getPrimaryKey());
                                    log.info(user.getUserName() + " has " + subscriptions.size() + " subscriptions");
      System.out.println("The no. of Subscriptions are : ***"+ subscriptions.size() );
                                    Iterator it = subscriptions.iterator();
                                    // See if user is subscribed to the newsletter
                                    { // Second inner loop
                                          UserSubscriptionTypeLocal ustl = (UserSubscriptionTypeLocal)it.next();
                                          UserSubscriptionTypeLocalValue userSub = ustl.getUserSubscriptionTypeLocalValue();
      System.out.println("Inside second while here 3");

                                          if (AcuvueEJBConstants.UserSubscriptionStatusSubscribed.equals(userSub.getStatus())
                                                      && rs.getString("SUBSCRIPTIONPK").equals(userSub.getSubscriptionTypeLocalValue().getPrimaryKey()))
      System.out.println("The Subscribed user is:****"+ user);

                                                String unsubscribeUrl = unsubscribeUrlBase + "userpk=" + user.getPrimaryKey()
                                                                                    + "&newsletterpk=" + rs.getString("CUSTOMNEWSLETTERPK");
                                                //Create email and send
                                                String commid = emailGenerator.generateNewsletterEmail(
                                                                        unsubscribeUrl );

                                                 log.info("Sending-" + commid);
                        } // End of While(rs.next())
                  }// End of inner try
                  catch(Exception e)
                        System.out.println("Exception in office record in whiles: ***"+ e);
                  newsbastsuccess = 1;
        } catch (Exception e) {
            log.error(e.getMessage(), e);

        String url = "/secure/admin/newsletter_error.jsp";
        if ( newsbastsuccess == 1 ) {
            url = "/secure/admin/newsletter_sent.jsp";

        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher ( url ) ;
        dispatcher.forward(request, response);

The Exception thrown is;
javax.ejb.ObjectNotFoundException: Bean not found in 'findByOfficeType....................'

Appreciate your help on this.

Best Regards
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Leo EikelmanDirector, IT and Business DevelopmentCommented:
Is your Bean associated with a package name?

And where is your bean located?

It should be in WEB-INF/classes/"BeanClassName".java

and when you package it , it should be in


Hope this helps,


The office type that you are passing, does this have valid entries in the database. If not then the exception will be thrown. No exception if the office type returns more than one row.

The javadocs say

"The ObjectNotFoundException exception is thrown by a finder method to indicate that the specified EJB object does not exist.

Only the finder methods that are declared to return a single EJB object use this exception. This exception should not be thrown by finder methods that return a collection of EJB objects (they should return an empty collection instead).

So you should be getting an empty collection for the same.

One more thing, you said it runs for 6 records, then the exception comes.
Does this mean in one go, the iterations go for 6 loops and then the error.
Six times the method is called successfully nd then the error .
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

M_SYEDAuthor Commented:

mrigank: The method calls are in a while loop which runs for each record retrieved from the database.
The error comes for a specific record after the same piece of code has been executed six times, this code includes method calls too.

I hope that answers your question.

I am thinking that it might be a data quality issue, because the program works for most of the records apart from a few which are causing the error.

Thanks a lot for your posts experts. Appreciate them.
Try deleting those records from the db and running the program and see if the error recurs.
Leo EikelmanDirector, IT and Business DevelopmentCommented:
Is your findByOffice method using getBean(int i) method anywhere in your code?

If you are, try using getBean() instead of getBean(int i)

I don't really see any problem with the code itself.




Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
M_SYEDAuthor Commented:
I think so too. Let me verify the quality of the data in the record thats throwing the error.
M_SYEDAuthor Commented:
I am using getBean() in the code and not getBean(int i)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.