我正在尝试实施接收邮件的代码但是我得到了NullPointerException
。
从“Settings.java”程序调用的接收代码的方法是“connect()”,它位于“EmailClient.java”程序中。
身份验证成功,但是当我尝试将消息头放在向量或数组中时,它会给出异常。
Settings.java
public class Settings extends javax.swing.JFrame {
public String uname;
public String pass;
public String smtpserver;
public String port;
Object item, item2;
EmailClient ec;
public Settings() {
initComponents();
}
public String getUname() {
return uname;
}
public String getPass() {
return pass;
}
public String getSmtpserver() {
return smtpserver;
}
public String getPort() {
return port;
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
uname = jTextField1.getText();
pass = jPasswordField1.getText();
item = jComboBox1.getSelectedItem();
smtpserver = (String) item;
item2 = jComboBox2.getSelectedItem();
port = (String) item2;
if (uname.equals("") || pass.equals("") || smtpserver.equals("") || port.equals("")) {
JOptionPane.showMessageDialog(this, "All Fields are mandatory");
}
ec = new EmailClient();
ec.connect(uname, pass);
setVisible(false);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Settings().setVisible(false);
}
});
}
EmailClient.java
public class EmailClient extends javax.swing.JFrame {
Settings set = new Settings();
SendMail sm = new SendMail(set);
Vector vdate, vsubject, vmessage, vfrom;
ArrayList adate, asubject, amessage, afrom;
String[] date;
String[] subject;
String[] frm;
String[] mess;
Properties props = null;
private Session session = null;
private Store store = null;
private Folder inbox = null;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
sm.setVisible(true);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
set.setVisible(true);
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
public void connect(String user, String pass) {
final String password = pass;
final String username = user;
try {
props = new Properties();
props.setProperty("mail.host", "imap.gmail.com");
props.setProperty("mail.port", "995");
props.setProperty("mail.transport.protocol", "imaps");
session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
);
store = session.getStore("imaps");
store.connect();
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message messages[] = inbox.search(new FlagTerm(
new Flags(Flags.Flag.SEEN), true));
for (int i = 0; i < messages.length; i++) {
Message message = messages[i];
Address[] from = message.getFrom();
frm[i] = "" + from[0];//Here is the exception
date[i] = "" + message.getSentDate();
Multipart mp = (Multipart) message.getContent();
BodyPart bp = mp.getBodyPart(0);
mess[i] = "" + bp.getContent();
subject[i] = "" + message.getSubject().toString();
}
} catch (Exception mex) {
mex.printStackTrace();
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new EmailClient().setVisible(true);
}
});
}
类型转换也不起作用。当我替换行
时,它工作正常frm[i]="" +from[0];
带
System.out.println("From : " + from[0]);
但这不是我想要的。
错误是
run:
java.lang.NullPointerException
at sendemail.EmailClient.connect(EmailClient.java:321)
at sendemail.Settings.jButton1ActionPerformed(Settings.java:176)
at sendemail.Settings.access$000(Settings.java:25)
at sendemail.Settings$1.actionPerformed(Settings.java:90)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3311)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
BUILD SUCCESSFUL (total time: 1 minute 42 seconds)
答案 0 :(得分:1)
你从未为frm数组分配任何空间。您声明了一个指向数组的指针,但您必须分配该数组。 frm = new String [SOMETHING];
答案 1 :(得分:0)
这是一个固定的(但不是最佳的)版本:
import java.util.Properties;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.search.FlagTerm;
public class GmailTest {
private Session session = null;
private Properties props = null;
private Store store = null;
private Folder inbox = null;
private String[] date;
private String[] subject;
private String[] frm;
private String[] mess;
public static void main(String[] args) {
new GmailTest().connect("user", "pwd");
}
public void connect(String user, String pass) {
try {
props = new Properties();
props.setProperty("mail.imaps.host", "imap.gmail.com");
props.setProperty("mail.imaps.port", "993");
props.setProperty("mail.store.protocol", "imaps");
session = Session.getInstance(props);
store = session.getStore("imaps");
store.connect(user, pass);
inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message messages[] = inbox.search(new FlagTerm(new Flags(Flags.Flag.SEEN), true));
//initialise arrays as Makoto says
date = new String[messages.length];
subject = new String[messages.length];
frm = new String[messages.length];
mess = new String[messages.length];
for (int i = 0; i < messages.length; i++) {
Message message = messages[i];
Address[] from = message.getFrom();
frm[i] = "" + from[0];// Here *was* the exception
date[i] = "" + message.getSentDate();
Multipart mp = (Multipart) message.getContent();
BodyPart bp = mp.getBodyPart(0);
mess[i] = "" + bp.getContent();
subject[i] = "" + message.getSubject().toString();
}
} catch (Exception mex) {
mex.printStackTrace();
} finally {
close(store);
close(inbox);
}
}
public static void close(final Folder folder) {
try {
if (folder != null && folder.isOpen()) {
folder.close(false);
}
} catch (final Exception e) {
// ignore
}
}
public static void close(final Store store) {
try {
if (store != null && store.isConnected()) {
store.close();
}
} catch (final Exception e) {
// ignore
}
}
}