int x;在此示例中,变量 x 默认设置为零,第二行(值 5)写入 x 指向的内存位置。Java 禁止程序员操作内存盒,它只允许您访问对象的方法,而不能访问内存位置。
x = 5;
Point dot = null;
dot.getX();
线程 main” java.lang.NullPointerException2) 访问或修改 null 对象的实例。
at Test.main(Test.java:27)
Point dot = null;
dot.getX();
线程 main” 中的异常 java.lang.NullPointerException3) 使用 throw null
at Test.main(Test.java:38)
IOException nullException = null;
try {
throw nullException;
} catch (IOException e) {
e.printStackTrace();
}
char[] ch = null;5) 访问 null 位置时,例如 array
int 长度 = ch.length;
char[] ch = null;
char c = ch[0];
Thread th=null;
synchronized(th){
System.out.print(此线程已同步”);
}
void display(String phrase){在某些情况下,此解决方案不是最佳解决方案,因为对象为 null,因此在 display() 过程中不会发生任何事情。
//检查句子是否不是null
if(phrase!=null)
System.out.print(phrase);
}
String string=”;
//Declaration
Thread[] th=new Thread[3];
//初始化
th[0]=new Thread();
th[1]=new Thread();
th[2]=new Thread();
Please disable your ad blocker and refresh the window to use this website.