java空指针异常解决

Java programming language provides the Null Pointer Exception type 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 name num.  Then we initialize it with the new Integer(10) line. If we do not initialize the variable num and 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值。
.u434d00bb6601851c1c296071a2076bb6 , .u434d00bb6601851c1c296071a2076bb6 .postImageUrl , .u434d00bb6601851c1c296071a2076bb6 .centered-text-area { min-height: 80px; position: relative; } .u434d00bb6601851c1c296071a2076bb6 , .u434d00bb6601851c1c296071a2076bb6:hover , .u434d00bb6601851c1c296071a2076bb6:visited , .u434d00bb6601851c1c296071a2076bb6:active { border:0!important; } .u434d00bb6601851c1c296071a2076bb6 .clearfix:after { content: ""; display: table; clear: both; } .u434d00bb6601851c1c296071a2076bb6 { display: block; transition: background-color 250ms; webkit-transition: background-color 250ms; width: 100%; opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; background-color: #ECF0F1; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); -moz-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); -o-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); -webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); } .u434d00bb6601851c1c296071a2076bb6:active , .u434d00bb6601851c1c296071a2076bb6:hover { opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; background-color: #D35400; } .u434d00bb6601851c1c296071a2076bb6 .centered-text-area { width: 100%; position: relative; } .u434d00bb6601851c1c296071a2076bb6 .ctaText { border-bottom: 0 solid #fff; color: #3498DB; font-size: 16px; font-weight: bold; margin: 0; padding: 0; text-decoration: underline; } .u434d00bb6601851c1c296071a2076bb6 .postTitle { color: #27AE60; font-size: 16px; font-weight: 600; margin: 0; padding: 0; width: 100%; } .u434d00bb6601851c1c296071a2076bb6 .ctaButton { background-color: #e6e6e6!important; color: #3498DB; border: none; border-radius: 3px; box-shadow: none; font-size: 14px; font-weight: bold; line-height: 26px; moz-border-radius: 3px; text-align: center; text-decoration: none; text-shadow: none; width: 80px; min-height: 80px; background: url(https://www.poftut.com/wp-content/plugins/intelly-related-posts/assets/images/simple-arrow.png)no-repeat; position: absolute; right: 0; top: 0; } .u434d00bb6601851c1c296071a2076bb6:hover .ctaButton { background-color: #E67E22!important; } .u434d00bb6601851c1c296071a2076bb6 .centered-text { display: table; height: 80px; padding-left: 18px; top: 0; } .u434d00bb6601851c1c296071a2076bb6 .u434d00bb6601851c1c296071a2076bb6-content { display: table-cell; margin: 0; padding: 0; padding-right: 108px; position: relative; vertical-align: middle; width: 100%; } .u434d00bb6601851c1c296071a2076bb6:after { content: ""; display: block; clear: both; }

LEARN MORE  Python "with" Statement By Examples

.u434d00bb6601851c1c296071a2076bb6 , .u434d00bb6601851c1c296071a2076bb6 .postImageUrl , .u434d00bb6601851c1c296071a2076bb6 .centered-text-area { min-height: 80px; position: relative; } .u434d00bb6601851c1c296071a2076bb6 , .u434d00bb6601851c1c296071a2076bb6:hover , .u434d00bb6601851c1c296071a2076bb6:visited , .u434d00bb6601851c1c296071a2076bb6:active { border:0!important; } .u434d00bb6601851c1c296071a2076bb6 .clearfix:after { content: ""; display: table; clear: both; } .u434d00bb6601851c1c296071a2076bb6 { display: block; transition: background-color 250ms; webkit-transition: background-color 250ms; width: 100%; opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; background-color: #ECF0F1; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); -moz-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); -o-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); -webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.17); } .u434d00bb6601851c1c296071a2076bb6:active , .u434d00bb6601851c1c296071a2076bb6:hover { opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; background-color: #D35400; } .u434d00bb6601851c1c296071a2076bb6 .centered-text-area { width: 100%; position: relative; } .u434d00bb6601851c1c296071a2076bb6 .ctaText { border-bottom: 0 solid #fff; color: #3498DB; font-size: 16px; font-weight: bold; margin: 0; padding: 0; text-decoration: underline; } .u434d00bb6601851c1c296071a2076bb6 .postTitle { color: #27AE60; font-size: 16px; font-weight: 600; margin: 0; padding: 0; width: 100%; } .u434d00bb6601851c1c296071a2076bb6 .ctaButton { background-color: #e6e6e6!important; color: #3498DB; border: none; border-radius: 3px; box-shadow: none; font-size: 14px; font-weight: bold; line-height: 26px; moz-border-radius: 3px; text-align: center; text-decoration: none; text-shadow: none; width: 80px; min-height: 80px; background: url(https://www.poftut.com/wp-content/plugins/intelly-related-posts/assets/images/simple-arrow.png)no-repeat; position: absolute; right: 0; top: 0; } .u434d00bb6601851c1c296071a2076bb6:hover .ctaButton { background-color: #E67E22!important; } .u434d00bb6601851c1c296071a2076bb6 .centered-text { display: table; height: 80px; padding-left: 18px; top: 0; } .u434d00bb6601851c1c296071a2076bb6 .u434d00bb6601851c1c296071a2076bb6-content { display: table-cell; margin: 0; padding: 0; padding-right: 108px; position: relative; vertical-align: middle; width: 100%; } .u434d00bb6601851c1c296071a2076bb6:after { content: ""; display: block; clear: both; }

通过示例了解更多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);}
}

翻译自: https://www.poftut.com/what-is-null-pointer-exception-in-java-and-how-to-fix/

java空指针异常解决

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

  1. java steam 去重_Java中对List去重 Stream去重的解决方法

    问题 当下互联网技术成熟,越来越多的趋向去中心化.分布式.流计算,使得很多以前在数据库侧做的事情放到了Java端.今天有人问道,如果数据库字段没有索引,那么应该如何根据该字段去重?大家都一致认为用Ja ...

  2. Activiti保存.png 流程图片文件且解决idea中保存图片时显示中文乱码的解决方法

    Activiti保存.png 流程图片文件且解决idea中保存图片时显示中文乱码的解决方法 Eclipse 工具中的操作 流程图片生成的两种方式: 使用 activiti-designer 设计流程图 ...

  3. java中什么是空指针异常_JAVA中的空指针异常如何处理?

    你这个问题的解决 问题定位: 在堆栈异常信息的第一行就可以定位到是哪里出了空指针,倘若这里不是你写的类,可以往下翻一下,找到你写的类,就是这里出现的空指针. 问题解决: 对一个空对象调用里面的方法或者 ...

  4. java为什么要用反射_Java中为什么需要反射?反射要解决什么问题?

    一句话概括就是使用反射可以赋予jvm动态编译的能力,否则类的元数据信息只能用静态编译的方式实现,例如热加载,Tomcat的classloader等等都没法支持 Java中编译类型有两种: 静态编译:在 ...

  5. java显示汉字乱码怎么办_java中的汉字显示问号乱码怎么解决

    java中的汉字显示问号乱码怎么解决 发布时间:2020-06-23 22:16:38 来源:亿速云 阅读:97 作者:元一 java中的汉字显示问号乱码怎么解决?针对这个问题,今天小编总结了这篇文章 ...

  6. java set 空值_Java中如何更优雅的处理空值

    经常看到项目中存在到处空值判断的情况,这些判断,会让人觉得摸不着头绪,它的出现很有可能和当前的业务逻辑并没有关系.但它会让你很头疼.有时候,更可怕的是系统因为这些空值的情况,会抛出空指针异常,导致业务 ...

  7. java gson使用_Java 中 Gson的使用

    JSON 是一种文本形式的数据交换格式,它比XML更轻量.比二进制容易阅读和编写,调式也更加方便;解析和生成的方式很多,Java中最常用的类库有:JSON-Java.Gson.Jackson.Fast ...

  8. java判断类型_Java中类型判断的几种方式 - 码农小胖哥 - 博客园

    1. 前言 在Java这种强类型语言中类型转换.类型判断是经常遇到的.今天就细数一下Java中类型判断的方法方式. 2. instanceof instanceof是Java的一个运算符,用来判断一个 ...

  9. java 判断类型_Java中类型判断的几种方式

    在Java这种强类型语言中类型转换.类型判断是经常遇到的.今天就细数一下Java中类型判断的方法方式.拉勾IT课小编为大家提供java种类型判断方式. instanceof instanceof是Ja ...

最新文章

  1. 「强化学习可解释性」最新2022综述
  2. [华为机试真题][2014]62.去除重复字符并排序
  3. kvm虚拟机命令梳理
  4. Sql Server 查看所有存储过程或视图的位置及内容
  5. yolo 负样本_目标检测介绍之YOLO与SSD
  6. 第六十节,文本元素标签
  7. Java NIO原理和使用
  8. webgl限制帧率_从《长安十二时辰》到《Science Advances》:大帧数高帧率超表面动态全息显示新方法...
  9. SpringCloud工作笔记046---SpringCloud https证书申请_不同证书区别
  10. 1.2_linearbinary_search_顺序(线性)查找二分(折半)查找
  11. 躺着收钱月入过万,怎样才能做到?
  12. 每周荐书(京东篇):618取胜之道、质量保障、技术解密
  13. Quartus II 的MegaWizard字体过小
  14. 0514JS函数练习
  15. 代码 root android4.4,安卓android4.4root图文教程
  16. 买到山寨版康师傅绿茶的后果
  17. 什么是网站被黑?网站被黑具体症状表现、网站被黑了怎么办
  18. [美] 尼古拉斯·卡尔 《浅薄:互联网如何毒化了我们的大脑 》
  19. Spanning-tree生成树协议
  20. 【转】浅谈面向对象的坏处

热门文章

  1. 下载分享永不关机 几步玩转家庭NAS组建
  2. 【PhotoShop基础B篇】准确控制曝光
  3. 同时使用网线以及无线上网
  4. 全局下的isFinite
  5. C/C++蓝桥杯三升序列
  6. IDEA代码以及注释格式化,行宽设置,以及自动换行
  7. sql server数据修复语句
  8. Microsoft编写优质无错C程序秘诀
  9. 程序员找不到合适工作的原因总结
  10. Bob,我要怎样才能成为一名 iOS 开发者