我正在使用Apache Tomcate 7.0.39,Eclipse Java EE Juno,Java JRE 7 & Java JDK 1.7.0_13。Java.util.timer計時器空指針異常
我的計時器有問題。我有兩個函數,第一個函數啓動計時器並執行任務,另一個函數停止計時器。 但是,定時器永遠不會停止,因爲它是空的。
我的Java代碼:
public class TestXMLSQLTimerLocal
{
public Timer timer;
public TestXMLSQLTimerLocal()
{
}
public void start()
{
this.timer = new Timer();
TimerTask task = new TimerTask()
{
public void run()
{
//Some code
}
};
timer.scheduleAtFixedRate(task, 0, 60000);
}
public void stop() {
this.timer.cancel();
}
}
我的JSP頁面:
<%@page import="com.accenture.api.TestXMLSQLTimerLocal" %>
<%@page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%
String text = "Press the Start button to begin !";
String action = request.getParameter("action");
TestXMLSQLTimerLocal TM = new TestXMLSQLTimerLocal();
if ("Start".equals(action))
{
TM.start();
text = "The data are being transferred. Press the Stop button when you want to stop the transfer !";
}
if("Stop".equals(action))
{
TM.stop();
text = "The transfer is stopped. Press the Start button to begin the transfer again !";
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Transfer Module</title>
</head>
<body>
<form name="StartTM" action="#">
<input type="hidden" name="action" value="Start"/>
<input type="submit" value="Start"/>
</form>
<br />
<p><%=text%></p>
<br />
<form name="StopTM" action="#">
<input type="hidden" name="action" value="Stop"/>
<input type="submit" value="Stop"/>
</form>
</body>
</html>
啓動()和stop()是由一個起點和一個簡單的JSP頁面上的停止按鈕調用。
對我來說,問題是計時器仍爲空,因爲Start()方法中的修改沒有考慮到。 有人可以幫助我嗎? 如果您需要了解更多信息,請向我
顯示堆棧跟蹤 – PSR 2013-04-09 09:25:50
你確定start()在stop()之前調用?放一些調試語句。 – 2013-04-09 09:26:55
是的,沒有任何東西出現在Tomcat的日誌文件中 – 2013-04-09 09:54:41