2 linux hw linux_hw 于 2014.08.22 13:00 提问

关于Threadlocal的用法问题,在线等

最近在做一个关于处理进度功能时用到threadlocal
public class LoadModal {

/**
 * 当前进度
 */
private static ThreadLocal<Double> threadLocal=new ThreadLocal<Double>();

/**
 * 
 * @param count  总数
 * @param currentValue  当前处理值
 * @return
 */
public  static void setProgress( int  count ,int currentValue){
    if (0==count){
        threadLocal.set(new Double(-1));
    }else{
        threadLocal.set(Double.parseDouble(new DecimalFormat("0.0000").format((double)currentValue/count)));
    }
}

/**
 * 初始化进度
 */
public static void initProgress(){
    threadLocal.set(new Double(-1));
}

/**
 * 获取当前进度
 * @return
 */
public static double getProgress(){
    return threadLocal.get();
}

}

更改值的时候循环调用的LoadModal.setProgress(count,row-1 );

现在问题是 用ajax 循环取值的时候LoadModal.getProgress()
出现空指针异常
java.lang.NullPointerException
at com.ultrapower.vo.LoadModal.getProgress(LoadModal.java:41)
at com.ultrapower.servlet.LoadingServlet.doPost(LoadingServlet.java:61)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1815)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!