目录
NullPointerException 在 Java 中如何工作?
Java NullPointerException 的构造函数
Java NullPointerException 的实现示例
示例#2
Example #3
How to Avoid NullPointerException?
Conclusion
首页 Java java教程 Java 空指针异常

Java 空指针异常

Aug 30, 2024 pm 04:13 PM
java

NullPointer Exception是java中程序员经常出现的异常。这是当对象实例化不正确时发生的运行时异常。该对象被声明为其中包含空值。空指针异常仅表示它正在尝试使用其中包含空值的引用来调用对象。最重要的是要记住,空指针异常与指针无关,因为 java 语言不支持指针概念;相反,它与对象引用相关联。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法:

没有特定的语法将空指针异常指定为运行时错误;它会自动出现并变得可见以供使用。

可以声明并抛出一个catch异常来指向和调试NullPointer异常。针对 NullPointerException 进行调试是一件非常繁琐的事情。需要确保对象实例化和引用声明在执行时正确。

try {
Object obj1 = new Object(); // Instantiate a new object for the class as reference
Obj1=null; //Provide null value to the object created
Logging.log(String.format(“get the object value”, obj1.object1for reference));
}
catch(java.lang.NullPointerException exception)
{
Logging.log(exception);
}
catch(Throwable exception)
{
Logging.log(exception,false);
}
登录后复制

NullPointerException 在 Java 中如何工作?

Java 中的 NullPointerException 是一个对于开发人员来说根本不是一个更好的错误的异常。异常很难发现,因为它每次都会在运行时发生。因此,找到这样的异常是一项繁琐的任务。程序员需要花费几个小时才能找出 NullPointerException。

当其他事情需要对象时,在任何地方不寻常地尝试分配用 null 定义的对象时,就会出现这种情况。所有java错误,包括NullPointerException,都是由于某些原因而发生的,例如:

  • 每当声明 java.lang.the throwable 接口时,它都会抛出所有可能的 java 错误,并通过继承类进一步扩展。
  • lang.Exception 然后继承自 java.lang.throwable。
  • lang.throwable 类扩展了 java.lang.Exception。
  • 甚至Java.lang.RuntimeException也会因为这个继承类而发生。
  • 最终,java.lang.NullPointerException 继承自 java.lang.RuntimeException 类。

正如明确提到和看到的那样,NullPointerException 是从 java.lang.RuntimeException 继承的,只要在编译和执行应用程序时执行应用程序,就会出现此问题。另外,每当尝试直接访问字段、方法或实例化时对象的错误引用时,都非常需要抛出java.lang.NullPointerException。添加记录器,然后创建另一个虚拟对象,然后调用 null 分配对象的方法实例也有助于正确调试代码并找到 NullPointerException 的根本原因以及指向错误和异常的行。因此,这是一种非常传统的故障排除和调试方法,用于了解特定行上发生的 java 错误。

Java NullPointerException 的构造函数

使用 NullPointerException 方法定义和声明的特定构造函数如下:

1。 NullPointerException()

这个构造函数用于构造一个 NullPointerException,没有详细的消息或解释。它可以被视为空异常或 null 异常,不符合要求。

2。 NullPointerException(String s)

这个构造函数的行为与 NullPointerException() 相矛盾,因为它包含了在指定位置引起的一些详细消息,并且它将涉及在构造空指针异常时可以使用的参数。参数 String s 负责创建带有详细消息的空指针异常。

Java NullPointerException 的实现示例

下面是 Java NullPointerException 的示例:

示例#1

此程序用于演示执行时调用无效方法,导致 NullPointerException,这不是必需的。

代码:

public class Null_Pointer_Excptn {
public static void main(String[] args) {
String strng = null;
try
{
if (strng.equals("monkey"))
System.out.println("Let us take a value which needs to be similar");
else
System.out.println("Otherwise it will not take a similar and equal value.");
}
catch(NullPointerException e)
{
System.out.println("It is a need to catch the null pointer exception.");
}
}
}
登录后复制

输出:

Java 空指针异常

示例#2

这个程序说明了java程序,它避免了NullPointerException的创建,因为它不是常规方法。

代码:

public class Null_Pointer_Excptn_Avoid {
public static void main(String[] args) {
String strng2 = null;
try
{
if ("avoid_null".equals(strng2))
System.out.println("Coming out to be equal");
else
System.out.println("It is not at all coming out to be equal");
}
catch(NullPointerException e)
{
System.out.println("Catch the null pointer Exception to get a clarification.");
}
}
}
登录后复制

输出:

Java 空指针异常

Example #3

This program illustrates that the NullPointerException can be avoided using the proper object verification before initialization.

Code:

public class Good_Null_Pntr_Excpn {
public static void main(String[] args) {
String store = "Learning";
try
{
System.out.println(getLength(store));
}
catch(IllegalArgumentException e)
{
System.out.println("Need to catch the definition of illegalArgument.");
}
store = "Educba";
try
{
System.out.println(getLength(store));
}
catch(IllegalArgumentException e)
{
System.out.println("Need to catch the definition of illegalArgument.");
}
store = null;
try
{
System.out.println(getLength(store));
}
catch(IllegalArgumentException e)
{
System.out.println("Need to catch the definition of illegalArgument.");
}
}
public static int getLength(String store)
{
if (store == null)
throw new IllegalArgumentException("Need to catch the definition of illegalArgument.");
return store.length();
}
}
登录后复制

Output:

Java 空指针异常

How to Avoid NullPointerException?

As it is not a good practice to get NullPointerException as it makes the entire codebase cumbersome and consumes time for the programmers to figure out the root cause of the NullPointerException.

Therefore, it is very much needed to avoid these exceptions which can make sure using the following ways:

  • By comparing a string with the literals.
  • By keeping a check on the passed arguments or parameters being passed from the method.
  • By making use of the ternary operator.

Conclusion

Java NullPointerException is an exception which is not a good option and is recommended not to occur as it consumes a lot of time. Debugging and troubleshooting become difficult; therefore, it must keep in mind that before the initialization of the object, the reference of the object must be proper. Therefore, the reference of the object’s null value should be proper, and then it can be avoided.

以上是Java 空指针异常的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1668
14
CakePHP 教程
1426
52
Laravel 教程
1329
25
PHP教程
1273
29
C# 教程
1256
24
PHP:网络开发的关键语言 PHP:网络开发的关键语言 Apr 13, 2025 am 12:08 AM

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP与Python:了解差异 PHP与Python:了解差异 Apr 11, 2025 am 12:15 AM

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

突破或从Java 8流返回? 突破或从Java 8流返回? Feb 07, 2025 pm 12:09 PM

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

PHP与其他语言:比较 PHP与其他语言:比较 Apr 13, 2025 am 12:19 AM

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP与Python:核心功能 PHP与Python:核心功能 Apr 13, 2025 am 12:16 AM

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

PHP的影响:网络开发及以后 PHP的影响:网络开发及以后 Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP:许多网站的基础 PHP:许多网站的基础 Apr 13, 2025 am 12:07 AM

PHP成为许多网站首选技术栈的原因包括其易用性、强大社区支持和广泛应用。1)易于学习和使用,适合初学者。2)拥有庞大的开发者社区,资源丰富。3)广泛应用于WordPress、Drupal等平台。4)与Web服务器紧密集成,简化开发部署。

PHP与Python:用例和应用程序 PHP与Python:用例和应用程序 Apr 17, 2025 am 12:23 AM

PHP适用于Web开发和内容管理系统,Python适合数据科学、机器学习和自动化脚本。1.PHP在构建快速、可扩展的网站和应用程序方面表现出色,常用于WordPress等CMS。2.Python在数据科学和机器学习领域表现卓越,拥有丰富的库如NumPy和TensorFlow。

See all articles