那system.out.println空指针吗
时间: 2023-11-11 19:05:02 浏览: 45
如果System.out被赋值为null,那么使用System.out.println就会出现空指针异常。因为在调用println方法的时候,需要使用System.out对象,如果System.out为null,就相当于调用一个null对象的方法,会导致空指针异常。
不过在Java中,System.out是一个静态的PrintStream对象,它在JVM启动时就已经被初始化了,因此通常情况下不会为null。如果出现为null的情况,那么很可能是程序中某个地方对System.out进行了赋值操作,不过这种情况不太常见。
相关问题
public class Tt { public static void main(String[] args) { try { int number = Integer.parseInt("6P"); System.out.println("8"); } catch (NumberFormatException e) { System.out.println("学号请输入整数!"); System.out.println("错误:" + e.getMessage()); } finally { System.out.println("2"); } System.out.println("3"); } }修改以上代码1.3使得程序可能抛出两种及以上类型的异常,try- catch-finally语句中用多个catch子句对不同类型的异常进行处理,分析程序在不同情况下的运行结果。
public class Tt {
public static void main(String[] args) {
try {
int number = Integer.parseInt("6P");
System.out.println("8");
String s = null;
s.charAt(0); // 这里会抛出NullPointerException异常
} catch (NumberFormatException e) {
System.out.println("学号请输入整数!");
System.out.println("错误:" + e.getMessage());
} catch (NullPointerException e) {
System.out.println("出现空指针异常!");
System.out.println("错误:" + e.getMessage());
} catch (Exception e) { // 这里捕获所有的异常类型
System.out.println("出现未知异常!");
System.out.println("错误:" + e.getMessage());
} finally {
System.out.println("2");
}
System.out.println("3");
}
}
运行结果:
学号请输入整数!
错误:For input string: "6P"
2
3
如果将代码中的"6P"修改为整数"6",则运行结果为:
8
出现空指针异常!
错误:null
2
3
如果将代码中的"6P"修改为"abc",则运行结果为:
出现未知异常!
错误:For input string: "abc"
2
3
可以看到,当出现不同类型的异常时,程序可以通过多个catch子句来分别处理各种类型的异常。同时,finally语句块无论是否出现异常都会执行。
参考下面的程序,试修改程序,捕获相关异常,使得程序能正常运行。public static void main(String[] args) { String[] array = {"acb","c","ddd"}; System.out.println("异常测试"); System.out.println(Arrays.toString(array)); String str = array[0]; System.out.println("第四个字符为" + str.charAt(3)); System.out.println("异常发生后的代码"); int aa = 0; aa = Integer.parseInt(array[1]); System.out.println(aa+"平方为" + aa * aa); }
可以使用 try-catch 块来捕获异常。在程序中可能会抛出的异常有索引越界异常(IndexOutOfBoundsException),空指针异常(NullPointerException)和数字格式异常(NumberFormatException)。
下面是修改后的代码:
```
public static void main(String[] args) {
String[] array = {"acb", "c", "ddd"};
System.out.println("异常测试");
System.out.println(Arrays.toString(array));
try {
String str = array[0];
System.out.println("第四个字符为" + str.charAt(3));
System.out.println("异常发生后的代码");
int aa = Integer.parseInt(array[1]);
System.out.println(aa + "平方为" + aa * aa);
} catch (IndexOutOfBoundsException e) {
System.out.println("索引越界异常:" + e.getMessage());
} catch (NullPointerException e) {
System.out.println("空指针异常:" + e.getMessage());
} catch (NumberFormatException e) {
System.out.println("数字格式异常:" + e.getMessage());
}
}
```
在程序中,我们将可能抛出异常的代码放入 try 块中,并在 catch 块中分别捕获不同类型的异常。如果程序抛出异常,将会输出异常信息。如果程序正常执行,将不会进入 catch 块。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)