Reputation: 3351
With the below code i'm trying to get only the emails that matches the subject line.
String subject = "test email";
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect(host, userName, password);
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
int count = inbox.getMessageCount();
System.out.println(count + " total messages");
Message messages[] = inbox.getMessages();
for (int i = 0; i < messages.length; i++) {
if (messages[i].getSubject().contains(subject)) {
System.out.println(messages[i].getSubject() + "\t"
+ messages[i].getSentDate());
} else {
System.out.println("subject mismatch");
}
}
here i'm getting the required result, but the problem kicks when there is a email with no subject line.
I tried the below.
else if (messages[i].getSubject().contains(null)) {
System.out.println(messages[i].getSubject() + "\t"
+ messages[i].getSentDate());
}
else if (messages[i].getSubject().contains("")) {
System.out.println(messages[i].getSubject() + "\t"
+ messages[i].getSentDate());
}
else if (messages[i].getSubject().contains(" ")) {
System.out.println(messages[i].getSubject() + "\t"
+ messages[i].getSentDate());
}
but still i get the below error
java.lang.NullPointerException
at com.getEmails.readEmails.main(readEmails.java:34)
also instead of contains
, i tried equals
and equalsIgnoreCase
, but still the issue is same.
when printing just subject line without the condition, it was showing null
as subject line. So i tried the below.
else if (messages[i].getSubject().contains("null")) {
System.out.println(messages[i].getSubject() + "\t"
+ messages[i].getSentDate());
}
but still the error is same.
please let me know how can i fix this.
Upvotes: 0
Views: 238
Reputation: 29971
In addition to what everyone else said, you might want to look into using the Folder.search method with a SubjectTerm, which will allow the searching to be done on the server:
Message[] messages = inbox.search(new SubjectTerm(subject));
for (int i = 0; i < messages.length; i++) {
System.out.println(messages[i].getSubject() + "\t"
+ messages[i].getSentDate());
}
Upvotes: 0
Reputation: 223
messages[i] != null && messages[i].getSubject() != null && messages[i].getSubject().contains(subject)
Upvotes: 1
Reputation: 52195
The problem is that whatever the getSubject()
yields can sometimes be null
, thus any operations on that will yield a NullPointerException
, regardless of what type of check you do.
To go around this problem you will need to make a null check, something like so:
for (int i = 0; i < messages.length; i++) {
if ((messages[i].getSubject() != null) && (messages[i].getSubject().contains(subject))) {
System.out.println(messages[i].getSubject() + "\t"
+ messages[i].getSentDate());
} else {
System.out.println("subject mismatch");
}
}
Upvotes: 2
Reputation: 23012
You should check for null
first as it will give NullPointerException
if messages[i]
OR messages[i].getSubject()
is null
. Apart from that contains(null)
has no meaning and must cause NullPointerException
but messages[i].getSubject() == null
does make sense.
Upvotes: 1