当我运行以下代码时,它给了我一个NullPointerException - 我该如何解决它?

时间:2012-07-19 16:47:22

标签: java

我有以下类叫做sendAttachment.java,还有Piechart.java和covertExcelTOCSV.java。我正在阅读els文件,将其转换为csv,然后让饼图读取数据,使饼图保存,然后将其发送到电子邮件附件中。 bellow类sendAttachment.java给了我一个空指针异常,我不知道如何修复它可以有人帮忙吗?

我得到的错误就是这个

Exception in thread "main" java.lang.NullPointerException
    at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:226)
    at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:299)
    at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1375)
    at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1021)
    at javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:419)
    at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1354)
    at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2107)
    at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2075)
    at javax.mail.Transport.send(Transport.java:123)
    at de.vogella.jfreechart.swing.pie.sendAttachment.main(sendAttachment.java:61)

我的代码

package de.vogella.jfreechart.swing.pie;

import java.io.IOException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class sendAttachment
{

    public static void main(String[] args) throws AddressException, MessagingException, IOException
    {
        String host = "relay.apple.com";
        String from = "jsherif@apple.com";
        String to = "techjomana@gmail.com";

        // Get system properties
        Properties props = System.getProperties();

        // Setup mail server
        props.put("mail.smtp.host", host);

        // Get session
        Session session = Session.getDefaultInstance(props, null);

        // Define message
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        message.setSubject("Important Message From VMO");
        message.setText("Please see the attached Chart");

        // Handle attachment 1
        MimeBodyPart messageBodyPart1 = new MimeBodyPart();
        messageBodyPart1.attachFile("/Users/jomanasherif/Documents/mychart.jpg");

//        // Handle attachment 2
//        MimeBodyPart messageBodyPart2 = new MimeBodyPart();
//        messageBodyPart2.attachFile("c:/Temp/b.txt");

        MimeMultipart multipart = new MimeMultipart("mixed");

        multipart.addBodyPart(messageBodyPart1);
       // multipart.addBodyPart(messageBodyPart2);

        message.setContent(multipart);

        // Send message
        Transport.send(message);
        System.out.println("message sent");

    }

}

3 个答案:

答案 0 :(得分:2)

无法读取附件时会发生此错误。

答案 1 :(得分:2)

添加:

fileDataSource.getOutputStream().close();

在代码中创建并使用FileDataSource对象后。它应该解决问题(假设fileDataSource是一个FileDataSource对象)

退房: http://kenai.com/projects/javamail/sources/mercurial/content/mail/src/main/java/javax/mail/internet/MimeUtility.java?rev=469

如果您要在多部分邮件中发送附件,请首先尝试在代码中压缩它们然后发送。您在此问题中遇到编码问题,这些编码器与.zip s友好。

答案 2 :(得分:1)

在MimeUtility(第226行)中抛出NullPointerException,而不是在您发布的类中抛出(尽管问题可能源自您的代码)。

看起来它与编码有关。无法深入研究代码,很难知道。你可以尝试一些事情......

尝试在创建MimeMultipart时删除参数:

MimeMultipart multipart = new MimeMultipart();

尝试附加其他类型的文件,例如一个文本文件。也许它无法检测到“jpg”文件的适当编码。