为什么我在运行时不断收到此错误?线程“main”java.lang.NullPointerException中的异常

时间:2014-02-21 15:26:36

标签: java

我正在尝试对下面的代码使用“Hashmap”实现。我只是想从地图中获取一个元素并将其打印到eclipse中的控制台。不幸的是,我一直收到这个错误(线程“main”java.lang.NullPointerException中的异常)打印到控制台上。我不知道我做错了什么,但我已经尝试过各种各样的解决方法,但我不断收到这个错误。任何人都可以为此提供解决方案吗?

这是我的代码:

package repo;

import java.util.*;
import org.joda.time.DateTime;

 public class ConsoleDisplayData {

// Fields
static DateTime currentTime = new DateTime();
static Map<Integer, String> smtpSuccess = new HashMap<Integer, String>();
public static String step0;

public static void storeSuccessData()
{
    // Store Elements in Map
    smtpSuccess.put(0, "Initiating..");
    smtpSuccess.put(1, "\nS: 220 smtp.localhost.com ESMTP Postfix");
    smtpSuccess.put(2, "\nC: HELO relay.localhost.com");
    smtpSuccess.put(3, "\nS: 250 Hello relay.localhost.com, I am glad to meet you");
    smtpSuccess.put(4, "\nC: MAIL FROM:" + "<"  + presentation.SMTPMailClientGui.jtfSenderEmail.getText() +  ">" );
    smtpSuccess.put(5, "\nC: RCPT TO: " + "<" + presentation.SMTPMailClientGui.jtfRecepientEmail.getText()  + ">" );
    smtpSuccess.put(6, "\nS: 354 End data with <CR><LF>.<CR><LF>");
    smtpSuccess.put(7, "\nC: From: " + presentation.SMTPMailClientGui.jtfSenderName.getText() + "<" + presentation.SMTPMailClientGui.jtfSenderEmail.getText()  +">");
    smtpSuccess.put(8, "\nC: To: Recepient Name" + "<" + presentation.SMTPMailClientGui.jtfRecepientEmail.getText()  +">");
    smtpSuccess.put(9, "\nC: Date: " + currentTime );
    smtpSuccess.put(10, "\nC: Subject: " + presentation.SMTPMailClientGui.jlSubject.getText());
    smtpSuccess.put(11, "\nC: " +  presentation.SMTPMailClientGui.jtaBody.getText());
    smtpSuccess.put(12, "\nS: 221 End");


    // Print Step 0
    step0 = smtpSuccess.get(0);
    System.out.println(step0);

}   

public static void main(String [] args)
{
      // Run    
      storeSuccessData();
}

1 个答案:

答案 0 :(得分:2)

我假设第18行是这一行(或下一行)之一:

smtpSuccess.put(4, "\nC: MAIL FROM:" + "<"  + presentation.SMTPMailClientGui.jtfSenderEmail.getText() +  ">" );

其中任何一个都可以为null,所以请检查(理想情况下使用调试器):

  • 介绍
  • presentation.SMTPMailClientGui
  • presentation.SMTPMailClientGui.jtfSenderEmail

<强>更新

由于presentation似乎是一个包,我认为SMTPMailClientGui是一个类,而jtfSenderEmail是一个静态字段。在那种情况下,jtfSenderEmail似乎是空的(除非那不是正确的行)。