When I run the following code, it gives me a NullPointerException – How do I fix it?
<p>I have the following class called sendAttachment.java, and also Piechart.java, and covertExcelTOCSV.java. I am reading in an els file converting it to csv and then having piechart read the data make a pie chart save it and then send it into an email attachment. the bellow class sendAttachment.java is giving me a null pointer exception and i don't know how to fix it can someone help?</p>

<p>The error i get is this </p>

<pre><code>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)
</code></pre>

<p>My code   </p>

<pre><code>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");

    }

}
</code></pre>
