当前位置:网站首页>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 .
边栏推荐
- 银河麒麟系统局域网文件共享教程
- Let Ma Huateng down! Web3.0, hopeless
- 跟我一起AQS SOS AQS
- Love number experiment | Issue 7 - Financial Crisis Analysis Based on random forest
- mysql使用笔记一
- Safe and efficient, non-contact "hand brushing" identification helps epidemic prevention and control
- The difference between scrum and Kanban
- 快递e栈——数组篇小型项目
- Modify large online games through CE modifier
- 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!"
猜你喜欢

Full record of 2022 open source moment at Huawei partners and Developers Conference

让马化腾失望了!Web3.0,毫无希望

Wechat applet based service management system for college party members' Home System applet graduation design, Party members, activists, learning, punch in, forum

Animal breeding production virtual simulation teaching system | Sinovel interactive

What is the core competitiveness of front-line R & D personnel aged 35~40 in this position?

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

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

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

SQL必需掌握的100个重要知识点:排序检索数据

Go从入门到实战——Panic和recover(笔记)
随机推荐
Original translation | comparison of machine learning model service tools: kserve, Seldon core and bentoml
银河麒麟系统局域网文件共享教程
释放开源数据库创新力量 | 【甘肃】openGauss Meetup圆满结束
数据平台调度升级改造 | 从Azkaban 平滑过度到Apache DolphinScheduler 的操作实践
跟我一起AQS SOS AQS
Go从入门到实战——channel的关闭和广播(笔记)
Prospects for enterprise digitalization (38/100)
系统自带的karsonzhang/fastadmin-addons报错
Go从入门到实战——Context与任务取消(笔记)
Very comprehensive dolphin scheduler installation and use documents
让马化腾失望了!Web3.0,毫无希望
100 important knowledge points that SQL must master: retrieving data
Go从入门到实战——仅执行一次(笔记)
SQL server for circular usage
图解基于AQS队列实现的CountDownLatch和CyclicBarrier
Squid proxy server
SQL必需掌握的100个重要知识点:排序检索数据
100 important knowledge points that SQL must master: using functions to process data
Go从入门到实战——任务的取消(笔记)
Let Ma Huateng down! Web3.0, hopeless