0

I have the following code in a servlet:

try
{
    EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("Flights_AssignmentPU");
    EntityManager em = emFactory.createEntityManager();

    Query query = em.createNamedQuery("Passengers.findByPassportNum");
    query.setParameter("passportNum", passport);
    List<Passengers> result = query.getResultList();            
    em.close();

    for(int i = 0; i < result.size(); i++)
    {
        name = result.get(i).getName();
        surname = result.get(i).getSurname();
        email_address = result.get(i).getEmail();
    }
}
catch(Exception e)
{
    response.sendRedirect("ErrorPage.html");
}

if(email_address.isEmpty() == false)
{
      //Send email using email address
}

This code works just fine when the user has an email address in the database. However, if the email field is empty in the database, the GlassFish Server is giving me a null pointer exception.

The line to blame for this is definitely this one:

        email_address = result.get(i).getEmail();

For some reason, when the user does not have an e-mail, this line is giving me the error just described. How can I solve this problem?

Edit

The method getEmail is automatically generated when creating the entity class (I have used persistence).

Here is its code:

public String getEmail() {
    return email;
}
6
  • The problem lies in Passengers.getEmail the source of which you have not included. Commented Dec 7, 2012 at 16:22
  • Show us the full stack trace, and your entity class. Commented Dec 7, 2012 at 16:23
  • You're implying that the 1st line in the stacktrace of the NPE points to the particular line which you're "definitely blaming". But this seems impossible based on the code logic posted so far. Can you please revise your implications and assumptions? By the way, the sendRedirect() doesn't magically return from the method... Commented Dec 7, 2012 at 16:28
  • I had a mistake. It was the line identified by Nambari. I assumed it was that line because when I commented it out and did some testing, the problem was nonexistent. Commented Dec 7, 2012 at 16:30
  • Ah nevermind, the comments on the answer of Nambari confirms that you were completely wrong in pointing out the line to blame. Please learn how to interpret stacktraces. Commented Dec 7, 2012 at 16:31

1 Answer 1

4

I think issue is this line:

if(email_address.isEmpty() == false)
{
      //Send email using email address
}

When database has email as empty, you might be getting null response. You are calling isEmpty() operation on null reference, which results in NullPointerException.

Do null check before calling isEmpty()

Example:

if(email_address != null && email_address.isEmpty() == false)
    {
          //Send email using email address
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.