700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java空指针异常解决_Java中的空指针异常是什么以及如何解决?

java空指针异常解决_Java中的空指针异常是什么以及如何解决?

时间:2021-09-13 22:18:05

相关推荐

java空指针异常解决_Java中的空指针异常是什么以及如何解决?

java空指针异常解决

Java programming language provides theNull Pointer Exceptiontype in order to handle object creation related errors and misuse. In this tutorial, we will learn what is Null Pointer Exception, its causes and How To Fix it?

Java编程语言提供了Null Pointer Exception类型,以处理与对象创建相关的错误和滥用。 在本教程中,我们将学习什么是空指针异常,其原因以及如何解决?

空指针异常 (Null Pointer Exception)

Java is an object-oriented programming language. Java provides different objects in order to create and use. In order to use an object, it should be initialized properly. If the object not initialized properly and try to be used it will throw a Null Pointer Exception.

Java是一种面向对象的编程语言。 Java提供了不同的对象以便创建和使用。 为了使用对象,应正确初始化。 如果对象未正确初始化并尝试使用,则将抛出Null Pointer Exception。

In the following code, we define an Integer object with the variable namenum. Then we initialize it with thenew Integer(10)line. If we do not initialize the variablenumand try to use it we will get a Null Pointer Exception.

在下面的代码中,我们使用变量名num定义一个Integer对象。 然后,我们使用new Integer(10)行对其进行初始化。 如果我们不初始化变量num并尝试使用它,我们将得到一个Null Pointer Exception。

class HelloWorld{public static void main(String args[]){Integer num = null;int new_var = num.intValue() ;System.out.println(new_var);}}

We can see that the following information about the null pointer exception is provided when the exception is thrown.

我们可以看到抛出异常时提供了有关空指针异常的以下信息。

`Thread name` is the thread name where the exception occurred.“线程名”是发生异常的线程名。 `Exception type` is the exception name with the full class name which is `java.lang.NullPointerException` in this example.“异常类型”是具有完整类名的异常名称,在本示例中为“ java.lang.NullPointerException”。 `The class name` where the name of the class where the exception occurred which is `HelloWorld` in this example.“类名”是发生异常的类的名称,在此示例中为“ HelloWorld”。 `The function name` is the function name where the exception occurred which is `main` in this example.“函数名”是发生异常的函数名,在本示例中为“ main”。 `The line number` is the source code line number where the exception occurred which is 9 in this case.“行号”是发生异常的源代码行号,在这种情况下为9。

空指针异常的原因 (Causes Of Null Pointer Exception)

Null pointer exception can occur in different ways with different causes. But we can list them like below in general.

空指针异常可能以不同的方式以不同的原因发生。 但是我们可以像下面一般列出它们。

Calling the instance method of a null object.调用空对象的实例方法。 Accessing or modifying the field or a null object.访问或修改字段或空对象。 Taking the length of the null as if it were an array.以null的长度作为数组。 Accessing or modifying the slots of null as if it were an array.访问或修改null插槽,就好像它是一个数组一样。 Throwing null as if it were a Throwable value.将null视为Throwable值。LEARN MORE Python "with" Statement By Examples通过示例了解更多Python“ with”语句

Causes Of Null Pointer Exception
空指针异常的原因

空指针异常的解决方案(Solutions For Null Pointer Exception)

The null pointer exception can be fixed or solved just by properly initializing the given object which will set an instance for the object. So the object will have an instance and will not null which is the cause of the Null Pointer Exception. In the following example, we will initialize the variable named num which is an Integer type.

空指针异常可以通过适当地初始化给定对象(该对象将设置该对象的实例)来解决或解决。 因此,该对象将具有一个实例,并且不会为null,这是Null Pointer Exception的原因。 在下面的示例中,我们将初始化名为num的变量,它是一个Integer类型。

class HelloWorld{public static void main(String args[]){Integer num = 10;int new_var = num.intValue() ;System.out.println(new_var);}}

翻译自: /what-is-null-pointer-exception-in-java-and-how-to-fix/

java空指针异常解决

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。