【发布时间】: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变量没有被注入,sCrashMonitorInterval有null值。 -
crash变量为空。 @LuiggiMendoza 如果sCrashMonitorInterval是null,那么它会抛出java.lang.NumberFormatException而不是NullPointerException -
@YatendraGoel 好吧,问题似乎是 EJB 的注入。 OP:请添加有关具有此
private CrashMonitorTimer crash;字段的类定义的更多信息。 -
@BrandonToms 你如何初始化/注入
crashbean?
标签: java nullpointerexception ejb