java - javax 计时器上的 NullPointerException?

标签 java swing timer nullpointerexception

我想在状态栏上显示计时器。

如何摆脱“在下面的行中抛出 NullPointerException ” t = Earthclock.getTimeStamp() ?

    public void startEarthTimer() {
    //final String t = null;
    earthTimer = new javax.swing.Timer(TIMEDELAY, 
        new ActionListener() {      
        //String t = null;
            public void actionPerformed(ActionEvent evt) {
                String t = null;
                //leftLabel.setText(statusText); 
                try {
                    // TODO: investigate why the line below is causing NullPointerException
                    EarthClock earthclock = Simulation.instance().getMasterClock().getEarthClock();
                    t = earthclock.getTimeStamp();
                } catch (Exception ee) {
                    ee.printStackTrace(System.err);
                }
                timeLabel.setText("Earth Time: " + t);
            }
        });

    earthTimer.start();
}

跟踪堆栈是这样说的:

 java.lang.NullPointerException
at org.mars_sim.msp.ui.swing.MainWindow$4.actionPerformed(MainWindow.java:277)
at javax.swing.Timer.fireActionPerformed(Unknown Source)
at javax.swing.Timer$DoPostEvent.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

我的 getTimeStamp() 并不复杂:

    public synchronized String getTimeStamp() {
    String result = formatter.format(cal.getTime());// + " UT";
    if (result == null) result = "0";
    return result;
}

我的 EarthClock 就是这样:

public class EarthClock {
    private final GregorianCalendar cal;


private final SimpleDateFormat formatter;

  public EarthClock(String dateString) {

    // Use GregorianCalendar constructor
    cal = new GregorianCalendar();

    // Set GMT timezone for calendar
    SimpleTimeZone zone = new SimpleTimeZone(0, "GMT");
    cal.setTimeZone(zone);

    // Initialize formatter
    formatter = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss 'UT'");
    formatter.setTimeZone(zone);

    // Set Earth clock to Martian Zero-orbit date-time. 
    // This date may need to be adjusted if it is inaccurate.
    cal.clear();
    DateFormat tempFormatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
    tempFormatter.setTimeZone(zone);
    try {
        cal.setTime(tempFormatter.parse(dateString));
    } catch (ParseException ex) {
        throw new IllegalStateException(ex);
    }
}

我尝试了 3 个位置来将“String t=null”放置在 startEarthTimer() 方法中,但没有一个有效。我的主类中也有 String t=null ,并且在所有情况下它都会抛出 NullPointerException。

谁能指出哪里出了问题吗? 非常感谢!

编辑:在 MasterClock 中,我启动了一个 EarthClock 实例,如下所示:

   public MasterClock() {
    // Initialize data members
    SimulationConfig config = SimulationConfig.instance();

    // Create an Earth clock
    earthTime = new EarthClock(config.getEarthStartDateTime());

    ....

    }

最佳答案

方法getEarthClock() 返回null。尝试像这样分解代码,并添加一些健全性检查以确定哪个方法返回 null:

MasterClock master = Simulation.instance().getMasterClock();
if (master == null) {
  throw new IllegalStateException("master clock is null");
}
EarthClock earthclock = master.getEarthClock();
if (earthclock == null) {
  throw new IllegalStateException("earthclock is null");
}
t = earthclock.getTimeStamp();

您可能错误配置了地球时钟,或者可能缺少您正在使用的库所需的某些配置。

关于java - javax 计时器上的 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27933083/

相关文章:

Javascript:重新启动倒数计时器无法按我的预期工作

java - 调用 PaintComponent 时数组的大小变为 0

Java setFullScreenWindow() 保持在最前面

java - 您可以在 WPF 应用程序中托管 Java swing 应用程序吗?

javascript - clearInterval ,在鼠标悬停时停止计时器

java - Android 计时器/timertask 导致我的应用程序崩溃?

java - 如何从 Play Framework 中的另一个模板调用一个模板?

java - 二维迷宫的递归算法?

java - 在 Microsoft sql server 中将不同的表连接/附加在一起

java - java中的图像映射?