on 05-15-2007 6:43 AM
Hi Experts
I am getting the Null Pointer Exception when I execute the following code. Can anybody please suggest where i am going wrong.
public void readInbox( )
{
//@@begin readInbox()
while (! wdContext.nodeEmail().isEmpty()) {
wdContext.nodeEmail().removeElement(wdContext.nodeEmail().getElementAt(0) );
}
wdContext.nodeEmail().invalidate();
Message[] message = new Message[1000];
IPublicEmailInteractiveFormComp.IEmailElement newEmailNodeElement;
//Set properties
// @TODO Enter your email server address here by replacing the text in angle brackets.
String host = "otemail.otegroup.com";
// @TODO Enter your email account here by replacing the text in angle brackets.
String username = "abc@otegroup.com";
// @TODO Enter your email account password here by replacing the text in angle brackets.
String password = "password";
Properties props = new Properties();
props.put("mail.smtp.host", host);
//Set Session
Session session = Session.getInstance(props, null);
//Set the store
try {
Store store = session.getStore("imap");
store.connect(host,username,password);
//Get folder
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
//Get Mails
message = folder.getMessages();
//Fill table with mails
for (int i = 0; i < message.length; i++)
{
if (message<i>.isSet(Flags.Flag.SEEN) == false)
{
if (message<i>.getSubject().equals("Travel Request Form"))
{
newEmailNodeElement = wdContext.createEmailElement();
newEmailNodeElement.setFrom(message<i>.getFrom()[0].toString());
newEmailNodeElement.setSubject(message<i>.getSubject());
newEmailNodeElement.setSentDate(message<i>.getSentDate().toString());
//Check for right Attachment
Object content = message<i>.getContent();
if ( content != null && content instanceof Multipart)
{
for (int j = 0 , n = ((Multipart)content).getCount(); j < n; j++)
{
Part part = ((Multipart)content).getBodyPart(j);
String disposition = part.getDisposition();
if ( disposition != null && (disposition.equals(Part.ATTACHMENT) || disposition.equals(Part.INLINE)))
{
if (part.getFileName().equals("TravelRequest.pdf"))
{
newEmailNodeElement.setAttachment(true);
newEmailNodeElement.setContentType(part.getContentType().toString());
InputStream is = part.getInputStream();
ByteArrayOutputStream bo = new ByteArrayOutputStream();
int c;
while ((c = is.read()) > -1) bo.write(c);
byte[] pdfSource = bo.toByteArray();
newEmailNodeElement.setPdfSource(pdfSource);
break;
}
else
{
newEmailNodeElement.setAttachment(false);
}
}
}
}
wdContext.nodeEmail().addElement(newEmailNodeElement);
}
}
}
//Close connection
folder.close(true);
store.close();
}
catch (MessagingException e)
{
wdComponentAPI.getMessageManager().reportSuccess(e.getMessage());
e.printStackTrace();
}
catch (IOException e)
{
wdComponentAPI.getMessageManager().reportSuccess(e.getMessage());
e.printStackTrace();
}
catch (NullPointerException e)
{
wdComponentAPI.getMessageManager().reportSuccess(e.getMessage());
e.printStackTrace();
}
//@@end
}
regards
Abdullah
Abdullah,
There are several reasons:
First problem is the following fragment:
//Get Mails
message = folder.getMessages();
//Fill table with mails
for (int i = 0; i < message.length; i++) {
...
}
Looks like folder.getMessages() returns null value. So rewrite condition in your loop as:
for (int i = 0; null != message && i < message.length; i++) {
...
}
Second, what if some mails has no subject? Rewrite the following in null-defensive style:
if (message[ i ].getSubject().equals("Travel Request Form")) {
newEmailNodeElement = wdContext.createEmailElement();
newEmailNodeElement.setFrom(message[ i ].getFrom()[0].toString());
newEmailNodeElement.setSubject(message[ i ].getSubject());
newEmailNodeElement.setSentDate(message[ i ].getSentDate().toString());
...
}
Should be:
if ("Travel Request Form".equals( message[ i ].getSubject() ) ) {
newEmailNodeElement = wdContext.createEmailElement();
if ( message[ i ].getFrom() != null && message[ i ].getFrom().length > 0)
newEmailNodeElement.setFrom(message[ i ].getFrom()[0].toString());
newEmailNodeElement.setSubject(message[ i ].getSubject());
if (message[ i ].getSentDate() != null)
newEmailNodeElement.setSentDate(message[ i ].getSentDate().toString());
...
}
Same below:
if (part.getFileName().equals("TravelRequest.pdf")) {
...
}
Must be changed to:
if ( "TravelRequest.pdf".equals(part.getFileName()) ) {
...
}
Valery Silaev
SaM Solutions
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Valery,
thanks once again for helping me out.
I am able to get the pdfs into the table in my application.
When I click on any of the rows. A new opens up that displays the pdf content.
When I click on submit button on this view; My explorer closes. This happens when I have a pdf and i trigger an outbound plug.
Any hints as to why this problem occurs.
Regards
Abdullah
Hi Abdulla,
I think u r getting exception because of not initializing array elements after
Message[] message = new Message[1000];
Add the following after this and try
for(int i=0;i<1000;i++)
{
message<i>=new MimeMessage(sessionObj);
}
Regards
Fahad Hamsa
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
84 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.