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."); } } }
输出:
示例#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."); } } }
输出:
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:
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中文网其他相关文章!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

禅工作室 13.0.1
功能强大的PHP集成开发环境

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

记事本++7.3.1
好用且免费的代码编辑器

Atom编辑器mac版下载
最流行的的开源编辑器