Question

Is there any case where StringTokenizer.nextToken will return null? I'm trying to debug a NullPointerException in my code, and so far the only possibility I've found is that the string returned from nextToken() returned null. Is that a possibility? Didn't find anything in the java doc.

Thanks

Was it helpful?

Solution

nextToken() throws NoSuchElementException if there are no more tokens in the tokenizer's string; so I would say that it doesn't return null.

http://download.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html#nextToken()

OTHER TIPS

I think it can throw a NullPointerException.

Inspecting the code of 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);
    }

Here, invocation of the method skipDelimiters() can throw 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;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top