parseOrNull

Returns a NonZeroInteger representing the number described by value, or returns null if value doesn't represent an integer or represents zero.

See the Integer.Companion.parse function for the grammar of a valid integer representation.


Calling from Kotlin

Here's an example of calling this function from Kotlin code:

val result: NonZeroInteger = NonZeroInteger.parse("+00042")
check("$result" == "42")

val invalid: Throwable? =
runCatching { NonZeroInteger.parse("3.14") }.exceptionOrNull()
check(invalid is NumberFormatException)

val zero: Throwable? =
runCatching { NonZeroInteger.parse("0") }.exceptionOrNull()
check(zero is IllegalArgumentException)

check(NonZeroInteger.parseOrNull("3.14") == null)
check(NonZeroInteger.parseOrNull("0") == null)

This function is hidden from Java, because nullability is not explicit in its type system.

See the parse function for throwing an exception instead of returning null in case of invalid or zero value.