Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException possibility in Integer.toString(arg) method in java

Is there any possibility that Integer.toString(args) give an NullPointerException like String.valueOf(args).

I know its Silly Question but I want to be clear that is there any possibility that Integer.toString() can give NullPointerException.

like image 491
user3297173 Avatar asked Jan 03 '16 19:01

user3297173


2 Answers

Technically yes, due to unboxing. Although I'm not sure if that is what you meant:

public class Test {

    public static void main(String[] args) {
        Integer i = null;
        System.out.println(Integer.toString(i)); // NullPointerException
    }
}
like image 83
Marvin Avatar answered Sep 27 '22 22:09

Marvin


Integer.toString(args) may give an NullPointerException unlike String.valueOf(args).

Integer i = null; 
Integer.toString(i);        // Null pointer exception!! 
String.valueOf(i);          // No exception 
i.toString();               // Again, Null pointer exception!!

See my experiment here : http://rextester.com/YRGGY86170

like image 30
Let'sRefactor Avatar answered Sep 27 '22 23:09

Let'sRefactor