当前位置:网站首页>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 .
边栏推荐
- 猜拳游戏专题训练
- Shell script controls the startup and shutdown of services - with detailed cases
- Let Ma Huateng down! Web3.0, hopeless
- oss上传调用的是哪个方法
- Day8 ---- 云资讯项目介绍与创建
- Love math experiment | phase VI - Financial anti fraud case study
- Original translation | comparison of machine learning model service tools: kserve, Seldon core and bentoml
- Go from introduction to practice - Interface (notes)
- Animal breeding production virtual simulation teaching system | Sinovel interactive
- Focus! Tips for installing fonts on domestic computers
猜你喜欢

Go从入门到实战——协程机制(笔记)

Covering access to 2w+ traffic monitoring equipment, EMQ creates a new digital engine for all elements of traffic in Shenzhen

互联网 35~40 岁的一线研发人员,对于此岗位的核心竞争力是什么?

GoLand permanently activated

Data platform scheduling upgrade and transformation | operation practice from Azkaban smooth transition to Apache dolphin scheduler

富文本 考试 填空题

Go从入门到实战——依赖管理(笔记)

White whoring red team goby & POC, how do you call white whoring?

Go从入门到实战——错误机制(笔记)

Codeforces Round #717 (Div. 2)
随机推荐
鲜为人知的mysql导入数据
猜拳游戏专题训练
华为伙伴暨开发者大会2022开源时刻全纪录
ARCS模型介绍
Focus! Tips for installing fonts on domestic computers
io流代码
让马化腾失望了!Web3.0,毫无希望
数组作业题
Full record of 2022 open source moment at Huawei partners and Developers Conference
SQL必需掌握的100个重要知识点:组合 WHERE 子句
Squid proxy server
Go从入门到实战——channel的关闭和广播(笔记)
Tutorial | fNIRS data processing toolkit homer2 download and installation
Use the storcli tool to configure raid. Just collect this article
使用storcli工具配置RAID,收藏这一篇就够了
Covering access to 2w+ traffic monitoring equipment, EMQ creates a new digital engine for all elements of traffic in Shenzhen
AI 绘画极简教程
MySQL performance optimization index function, hidden, prefix, hash index usage (2)
White whoring red team goby & POC, how do you call white whoring?
Go from introduction to actual combat - panic and recover (notes)