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;
}
I think issue is this line:
When database has email as empty, you might be getting
nullresponse. You are callingisEmpty()operation on null reference, which results inNullPointerException.Do
nullcheck before callingisEmpty()Example: