簡體   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