简体   繁体   中英

null pointer exception in setting thread local in java

guys i know it is dummy question but it gives me null ointer exception when writing myThreadLocal.set(str) .. here is my full code

threadLocalController.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(); 
  }
 }

threadLocal.java

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);
  }
}

web.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>

any help would be appreciated .. thanks

You never initialize your object... Use this instead.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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