【问题标题】:java bean null pointer exceptionjava bean空指针异常
【发布时间】:2014-03-09 13:07:04
【问题描述】:

这是我的第一篇文章,所以希望我能达到社区的高标准。

我正在尝试学习 Enterprise Java bean,但我陷入了困境,并被要求调试一个抛出 NullPointerException 的 EAR 文件。这是抛出异常的行:

private CrashMonitorTimer crash;
...
/*THE NEXT LINE THROWS THE NULL POINTER EXCEPTION*/
crash.createTimer(Long.parseLong(sCrashMonitorInterval),"CrashMoniterScheduler");

这是被调用接口的代码:

import javax.ejb.Local;

@Local
public interface CrashMonitorTimer
{
    public abstract void createTimer(long l, String s);

    public abstract void cancelTimer(String s);
}

这是 Java bean 的代码:

@Stateless(name = "CrashMonitorBean", mappedName = "CrashMonitorBean")
@Local(CrashMonitorTimer.class)

public class CrashMonitorBean
    implements CrashMonitorTimer
{

    @Resource
    SessionContext sessionCtx;
    TimerService timerService;
    Timer timer;
    int iMsgCntBeforeCtxRenew;
    int iCtxReusedCount;
    Context _context;
    CrashMonitorInfoUtil crashMonitorRMIContext[];

    public CrashMonitorBean()
    {
        iMsgCntBeforeCtxRenew = 10;
        iCtxReusedCount = 0;
        _context = null;
        crashMonitorRMIContext = null;
    }

    @Override
    public void createTimer(long lInterval, String sName)
    {

        //CrashMonitorInfoUtil leaves context open for reuse 

        System.out.println((new StringBuilder("Creating ")).append(sName).append(" timer").toString());
        timerService = sessionCtx.getTimerService();
        timer = timerService.createTimer(lInterval, lInterval, sName);
        String sPorts = SystemConfigurator.getConfigValue("RMIPorts");
        String saPorts[] = sPorts.split(",");
        String server = SystemConfigurator.getConfigValue("host");
        crashMonitorRMIContext = new CrashMonitorInfoUtil[saPorts.length];
        for(int i = 0; i < saPorts.length; i++)
        {
            crashMonitorRMIContext[i] = new CrashMonitorInfoUtil(server, saPorts[i]);
        }

    }
...
}

我对此进行了探索,但是对 Java bean 或接口(或新注释)的经验为零,我什至不知道应该尝试什么。任何解释或指导将不胜感激。

【问题讨论】:

  • 能否包含异常堆栈跟踪。我可以看到很多地方可以有 NPE,并且堆栈跟踪会显示正确的地方。
  • 问题可能是:crash变量没有被注入,sCrashMonitorIntervalnull值。
  • crash 变量为空。 @LuiggiMendoza 如果sCrashMonitorIntervalnull,那么它会抛出java.lang.NumberFormatException 而不是NullPointerException
  • @YatendraGoel 好吧,问题似乎是 EJB 的注入。 OP:请添加有关具有此 private CrashMonitorTimer crash; 字段的类定义的更多信息。
  • @BrandonToms 你如何初始化/注入crash bean?

标签: java nullpointerexception ejb


【解决方案1】:

crash 变量是null

如果sCrashMonitorInterval 为空,那么它会抛出NumberFormatException

【讨论】:

  • 那么crash 没有被注入。 IMO 这仍然不足以解决问题。
猜你喜欢
  • 2011-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-28
相关资源
最近更新 更多