2011-01-23 3 views
1

StringTokenizer.nextToken이 null을 반환하는 경우가 있습니까? 내 코드에서 NullPointerException을 디버깅하려고하는데, 지금까지 발견 한 유일한 가능성은 nextToken()에서 반환 된 문자열이 null을 반환한다는 것입니다. 그럴 가능성이 있니? java 문서에서 아무 것도 찾지 못했습니다. 토크 나이의 문자열에 토큰이없는 경우java StringTokenizer - nextToken이 null을 돌려 줄 수 있습니까?

감사

+0

생성자에 null 문자열을 전달하지 않습니까? countTokens()는 무엇을 반환합니까? – Reno

+0

StringTokenzier의 생성자에? 그렇게 생각하지 않는다면 생성자 자체에서 NullPointerException이 발생하고 이것이 내 경우는 아닙니다. – duduamar

답변

1

가 나는 NullPointerException이 던져 수 있다고 생각합니다. nextToken의 부호()를 검사

여기서

public String nextToken() { 
     /* 
     * If next position already computed in hasMoreElements() and 
     * delimiters have changed between the computation and this invocation, 
     * then use the computed value. 
     */ 

     currentPosition = (newPosition >= 0 && !delimsChanged) ? 
      newPosition : skipDelimiters(currentPosition); 

     /* Reset these anyway */ 
     delimsChanged = false; 
     newPosition = -1; 

     if (currentPosition >= maxPosition) 
      throw new NoSuchElementException(); 
     int start = currentPosition; 
     currentPosition = scanToken(currentPosition); 
     return str.substring(start, currentPosition); 
    } 

, skipDelimiters 메소드의 호출() NullPointerException이 던져.

private int skipDelimiters(int startPos) { 
     if (delimiters == null) 
      throw new NullPointerException(); 

     int position = startPos; 
     while (!retDelims && position < maxPosition) { 
      if (!hasSurrogates) { 
       char c = str.charAt(position); 
       if ((c > maxDelimCodePoint) || (delimiters.indexOf(c) < 0)) 
        break; 
       position++; 
      } else { 
       int c = str.codePointAt(position); 
       if ((c > maxDelimCodePoint) || !isDelimiter(c)) { 
        break; 
       } 
       position += Character.charCount(c); 
      } 
     } 
     return position; 
    } 
+0

이것은 단지 가능성 일 뿐이며 귀하의 경우에는 원인이 될 필요가 없습니다! – aNish

관련 문제