#vulnerable code 
                                public void add(StreamEvent streamEvent){
        StreamEvent firstConvertedEvent =null;
        StreamEvent lastConvertedEvent = null;
        while (streamEvent!=null){
            StreamEvent borrowedEvent = eventManager.borrowEvent();
            eventManager.convertStreamEvent(streamEvent, borrowedEvent);
            if(firstConvertedEvent ==null){
                firstConvertedEvent = borrowedEvent;
                lastConvertedEvent = borrowedEvent;
            } else {
                lastConvertedEvent.setNext(borrowedEvent);
            }
            streamEvent = streamEvent.getNext();
        }
        if(first==null){
            first = firstConvertedEvent;
        } else if (lastReturned != null){
            StreamEvent nextToLastReturned = lastReturned.getNext();
            lastConvertedEvent.setNext(nextToLastReturned);
            lastReturned.setNext(firstConvertedEvent);
        } else {       //when first!=null
            lastConvertedEvent.setNext(first);
            first = firstConvertedEvent;
        }
    } 
                            #location 22 
                            #vulnerability type NULL_DEREFERENCE
#fixed code
                                public void add(StreamEvent streamEvent){
        StreamEvent borrowedEvent = eventManager.borrowEvent();
        eventManager.convertStreamEvent(streamEvent, borrowedEvent);
        if(lastReturned!=null){
            StreamEvent lastEvent = lastReturned;
            StreamEvent even= null;
            while (lastEvent!=null){
                even = lastEvent;
                lastEvent = lastEvent.getNext();
            }
            even.setNext(borrowedEvent);
        }else if(previousToLastReturned!=null){
            previousToLastReturned.setNext(borrowedEvent);
        }  else if (first != null){
            StreamEvent lastEvent = first;
            StreamEvent even= null;
            while (lastEvent!=null){
                even = lastEvent;
                lastEvent = lastEvent.getNext();
            }
            even.setNext(borrowedEvent);
        } else {
            assignEvent(borrowedEvent);
        }
    }