Is it possible that Java String.split can return a null String[]
Asked Answered
S

4

48

Is it possible for split to return a null String[]? I am curious as I want to try to be as defensive as possible in my code without having unnecessary checks. The code is as follows:

String[] parts = myString.split("\\w");  

do I need to perform a null check before I use parts post splitting?

Shortcircuit answered 1/8, 2011 at 17:26 Comment(0)
M
59

It never returns null. You should always check the javadoc of the method if you are not sure. For example String#split(String) says

This method works as if by invoking the two-argument split method

...and String#split(String,int) says:

If the expression does not match any part of the input then the resulting array has just one element, namely this string.

From Javadoc you can also find out what kind of exceptions can happen and more importantly why were those exceptions thrown. Also one very important thing to check is if the classes are thread safe or not.

Morale answered 1/8, 2011 at 17:31 Comment(3)
I did check the javadoc, returning null != throwing NPE. I saw the PatternException that it can throw.Shortcircuit
The Javadoc for split(String regex) does not indicate that null cannot be returned.Edelman
@Paul: No, but if null could be returned, it (or the Pattern#split docs) would say so. And the two-argument version, which the single argument says it calls, says "If the expression does not match any part of the input then the resulting array has just one element, namely this string."Helaine
E
7

I recommend you download the Java source code for those times when the API is unclear. All the answers here just tell you the answer but you should really see for yourself. You can download the Java source code from here (look near the bottom of the page).

By following the source code you'll end up at String[] Pattern.split(CharSequence input, int limit). The return value comes from a non-null ArrayList on which toArray is called. toArray does not return null: in the event of an empty list you'll get an empty array back.

So in the end, no, you don't have to check for null.

Edelman answered 1/8, 2011 at 17:44 Comment(1)
See T.J. Crowder's comment elsewhere.Rizal
O
2

No, you don't need to check for null on the parts.

Ovalle answered 1/8, 2011 at 17:27 Comment(0)
P
1

Split method calls the Patter.split method which does this in the beginning of the method:

ArrayList<String> matchList = new ArrayList<String>();

And at the end does matchList.toArray() to return an Array.

So no need to test of nulls.

Popple answered 1/8, 2011 at 17:30 Comment(3)
Never rely on looking at the implementation. The implementation can change. The documentation is the contract you're working to.Helaine
True.. but the implementation of the this method unlikely to change, or the entire java world who've used this method without a null check will be in disarray.Popple
In this case, it's clear from the docs that if it changes, it must not return null.Helaine

© 2022 - 2024 — McMap. All rights reserved.