小编The*_*mon的帖子

为什么第一行抛出NullPointerException但第二行没有?

以下代码中的toString方法抛出NullPointerException,但printl打印后调用null.为什么他们没有相同的结果?THL

package exceptionsNullPointer;

public class NPArrays {

    public static void main(String[] args) {
        String[] laces = new String[2];

        // this line throws NullPointerException
        System.out.println(laces[1].toString());

        // this line prints null
        System.out.println(laces[1]);
    }
}
Run Code Online (Sandbox Code Playgroud)

java arrays tostring nullpointerexception

2
推荐指数
1
解决办法
95
查看次数

标签 统计

arrays ×1

java ×1

nullpointerexception ×1

tostring ×1