Reputation: 4607
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;
}
Upvotes: 0
Views: 496
Reputation: 66657
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
}
Upvotes: 4