简体   繁体   English

NullPointerException在javax计时器上吗?

[英]NullPointerException on javax Timer?

I want to show a timer on a status bar. 我想在状态栏上显示一个计时器。

How does I get rid of the " throwing NullPointerException below on the line " t = earthclock.getTimeStamp() ? 如何摆脱“在行下面抛出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();
}

The trace stack said this: 跟踪堆栈表示:

 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)

My getTimeStamp() is nothing complicated: 我的getTimeStamp()并不复杂:

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

Also my EarthClock is just this: 我的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);
    }
}

I've tried 3 locations for placing "String t=null" inside the method startEarthTimer() and none works. 我已经尝试了3个位置,用于在方法startEarthTimer()内放置“ String t = null”,但没有一个起作用。 I also have String t=null in the main class and in all cases it's throwing NullPointerException. 我在主类中也有String t = null,并且在所有情况下都抛出NullPointerException。

Can anyone point out what went wrong? 谁能指出出什么问题了? Thanks much! 非常感谢!

EDIT: In MasterClock, I started an instance of EarthClock as follows: 编辑:在MasterClock中,我如下启动EarthClock的实例:

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

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

    ....

    }

The method getEarthClock() is returning null . 方法getEarthClock()返回null Try breaking up your code like so, and adding some sanity checks to gain certainty of which method is returning 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();

You may be mis-configuring the earth clock, or possibly missing some configuration required for the library you're using. 您可能未正确配置地球时钟,或者可能缺少正在使用的库所需的某些配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM