繁体   English   中英

在java中设置线程本地时的空指针异常

[英]null pointer exception in setting thread local in java

伙计们,我知道这是一个愚蠢的问题,但是在编写 myThreadLocal.set(str) 时它给了我一个 null ointer 异常..这是我的完整代码

线程本地控制器.java

package threadLocalWeb;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class threadLocalController {
@RequestMapping(value = "/index")
public void doPost()  {
    threadLocal.setMyThreadLocal("name1");
    threadLocal.getMyThreadLocal();
    threadLocal.setMyThreadLocal("name2");
    threadLocal.getMyThreadLocal(); 
  }
 }

线程本地文件

package threadLocalWeb;
public class threadLocal {
 private static ThreadLocal<String> myThreadLocal;

 public threadLocal(){
    myThreadLocal = new ThreadLocal<String>();
  }

 public static String getMyThreadLocal() {
    return myThreadLocal.get();
  }
 public static void setMyThreadLocal(String str) {
    myThreadLocal.set(str);
  }
}

网页.xml

<web-app version="2.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee   
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  

 <servlet>  
 <servlet-name>sdnext</servlet-name>  
   <servlet- 

 class>org.springframework.web.servlet.DispatcherServlet</servlet-

 class>  
 <init-param>  
        <param-name>contextConfigLocation</param-name><param-value>/WEB- 

  INF/config/sdnext-servlet.xml</param-value></init-param>  
 <load-on-startup>1</load-on-startup>  
  </servlet>  

 <servlet-mapping>  
  <servlet-name>sdnext</servlet-name>  
  <url-pattern>/</url-pattern>  
  </servlet-mapping> 

 <welcome-file-list>  
 <welcome-file>index.html</welcome-file>  
 </welcome-file-list>

  </web-app>  

sdnext-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<mvc:annotation-driven/>
<context:annotation-config />
<context:component-scan base-package="threadLocalWeb"/>
 </beans>

任何帮助将不胜感激..谢谢

你永远不会初始化你的对象......改用这个。

public class ThreadLocal {
 private static ThreadLocal<String> myThreadLocal = new ThreadLocal<>();
}

您需要调用threadLocal构造函数来初始化myThreadLocal ,或者您必须直接在变量声明或静态初始化块中进行初始化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM