Java.util.timer计时器空指针异常

时间:2013-04-09 09:24:55

标签: java tomcat timer nullpointerexception

我正在使用Apache Tomcate 7.0.39,Eclipse Java EE Juno,Java JRE 7& Java JDK 1.7.0_13。

我的计时器有问题。我有两个功能,第一个启动计时器并执行任务,另一个停止计时器。 但是计时器永远不会停止,因为它是空的。

我的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>

start()和stop()由简单JSP页面上的Start和Stop按钮调用。

对我来说,问题是计时器仍为空,因为Start()方法中的修改没有考虑在内。 有人可以帮帮我吗? 如果您需要更多信息,请问我

4 个答案:

答案 0 :(得分:1)

在构造函数中启动计时器而不是start方法:

public class TestXMLSQLTimerLocal
{
public Timer timer;    

public TestXMLSQLTimerLocal(){
   this.timer = new Timer();
}

public void start()
{
    TimerTask task = new TimerTask()
    {
        public void run()
        {
        //Some code
        }
    };

    timer.scheduleAtFixedRate(task, 0, 60000);
}
public void stop() {
    this.timer.cancel();
}
}

答案 1 :(得分:1)

您需要确保在stop命令之前调用了start命令。另外,将this.timer = new Timer();放入班级的构造函数中。

答案 2 :(得分:0)

如果从JSP调用它,当用户按下停止按钮时,产生新线程和TestXMLSQLTimerLocal的{​​{1}}实例不可用。

您需要向会话添加Timer的实例,或者更优选的是applicationContext。当您对此实例执行操作时,请始终从会话中拉出它。

向会话添加TestXMLSQLTimerLocal的实例:

TestXMLSQLTimerLocal

要将实例添加到Application Scope,您需要使用ServletContextListener

此外,代码将抛出NPE错误,因为永远不会创建//From Servlet request.getSession().addAttribute("timer", new TestXMLSQLTimerLocal()); 的实例。

答案 3 :(得分:0)

我找到了解决方案,感谢Kevin Bowersok

我将开始和停止按钮分成两页,然后通过会话发送我的TestXMLSQLTimerLocal对象。

通过会话发送对象:

session.putValue("TM",TM);

要获取其他页面中的值:

TestXMLSQLTimerLocal TM = (TestXMLSQLTimerLocal) session.getValue("TM");

谢谢你们帮助人们

相关问题