当前位置:网站首页>Null pointer exception
Null pointer exception
2022-06-27 21:38:00 【zhengmayusi】
Precondition for null pointer exception : “ Null reference ” Access instance 【 object 】 Relevant data , Null pointer exceptions will occur !
Let's first look at the following example code :
public class NullPointerTest {
public static void main(String[] args) {
// Create customer object
Customer o = new Customer();
// Visit this customer's id
System.out.println(" Customer's id yes "+o.id);
// Back to the Id assignment
o.id = 9521; // Lifetime code
System.out.println(" Customer's id yes "+o.id);
o = null; // representative o Do not save memory address
System.out.println(o.id);
}
}
class Customer{
// Customer id
int id; // Access instance variables in member variables , You should create the object first , adopt " quote ." Mode of access 1
}
Running results :
Customer's id yes 0
Customer's id yes 9521
Exception in thread "main" java.lang.NullPointerException
at com.lxz.study01.NullPointerTest.main(NullPointerTest.java:19)
A null pointer exception is reported in the last line of the running result , This is because of the code :
o = null; // representative o Do not save memory address
So that the local variable does not save the instance object in the stack area Customer The address of , Thus, it cannot find the error that the corresponding object in the heap area reports null pointer exception !
The corresponding memory graph is :
Last , Mention it java Of Garbage collector GC:
stay java In language , Garbage collector mainly aims at heap memory , When one java When an object does not have any references to the object ,GC Will consider recycling the garbage data , The recycled data will not come back .
边栏推荐
- oss上传调用的是哪个方法
- After being forced to develop the app within 20 days, the group was laid off, and the technical director angrily criticized it: I wish "closure as soon as possible!"
- Go from introduction to actual combat - all tasks completed (notes)
- Go从入门到实战——仅执行一次(笔记)
- Covering access to 2w+ traffic monitoring equipment, EMQ creates a new digital engine for all elements of traffic in Shenzhen
- Go从入门到实战——接口(笔记)
- 100 important knowledge points that SQL must master: combining where clauses
- Experience Navicat premium 16, unlimited reset, 14 day trial method (with source code)
- Go从入门到实战——依赖管理(笔记)
- Go从入门到实战——任务的取消(笔记)
猜你喜欢

Modify large online games through CE modifier

Can Oracle's CTAs bring constraints and other attributes to the new table?

Go from introduction to actual combat - panic and recover (notes)

数据平台调度升级改造 | 从Azkaban 平滑过度到Apache DolphinScheduler 的操作实践

Oracle的CTAS能不能将约束等属性带到新表?

Go从入门到实战——任务的取消(笔记)

100 important knowledge points that SQL must master: filtering data

How to participate in openharmony code contribution

银河麒麟系统局域网文件共享教程

How to do a good job of gateway high availability protection in the big promotion scenario
随机推荐
行业案例|从零售之王看银行数字化转型的运营之道
GoLand permanently activated
Go从入门到实战——Context与任务取消(笔记)
White whoring red team goby & POC, how do you call white whoring?
让马化腾失望了!Web3.0,毫无希望
100 important knowledge points that SQL must master: retrieving data
抖音的兴趣电商已经碰到流量天花板?
Serveur mandataire SQUID
SQL必需掌握的100个重要知识点:创建计算字段
What is the core competitiveness of front-line R & D personnel aged 35~40 in this position?
Tiktok's interest in e-commerce has hit the traffic ceiling?
根据自定义excel标题模板快速excel导出
01-Golang-环境搭建
VMware vSphere ESXi 7.0安装教程
Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)
一套系统,减轻人流集中地10倍的通行压力
100 important knowledge points that SQL must master: combining where clauses
Go从入门到实战——Panic和recover(笔记)
华为伙伴暨开发者大会2022开源时刻全纪录
Scrum和看板的区别