NullPointerException when setting attribute?

后端 未结 5 608
隐瞒了意图╮
隐瞒了意图╮ 2020-12-19 05:17

For example I have a servlet code that sets attribute to a HttpServletRequest:

request.setAttribute(\"someValue\", someValue());
        RequestDispatcher rd         


        
5条回答
  •  甜味超标
    2020-12-19 06:11

    If you look at implementaion of request interface in tomcat it looks like below code.

    public void setAttribute(String name, Object value) {
    
        // Name cannot be null
        if (name == null)
            throw new IllegalArgumentException
                (sm.getString("coyoteRequest.setAttribute.namenull"));
    
        // Null value is the same as removeAttribute()
        if (value == null) {
            removeAttribute(name);
            return;
        }
    
        if (name.equals(Globals.DISPATCHER_TYPE_ATTR)) {
            internalDispatcherType = (DispatcherType)value;
            return;
        } else if (name.equals(Globals.DISPATCHER_REQUEST_PATH_ATTR)) {
            requestDispatcherPath = value;
            return;
        }
    

    Its clearly seen that if key is null then it throw IllegalArgumentException but if value is null then it simply remove the key and old associated object with that key from reposatory. But if none of them is null them it will associate the object with that key and add them o repository.

    It seems a temporary or tomcat issue.

    for more on implementation refer to below link Source code of Request Implementation by tomcat

提交回复
热议问题