当前位置:网站首页>Exception handling - a small case that takes you to solve NullPointerException
Exception handling - a small case that takes you to solve NullPointerException
2022-07-24 16:59:00 【qfxietian】
original Qianfeng sol
One . Demand background
Recently, Mr. sol told the students about the basic concepts and usage of classes and objects , And I left a little exercise for you , The topic of the exercise is to use an array to store the student information in the class , Including the student number 、 full name 、 Age attribute , Finally, output the students' information . This topic seems to be very simple , So everyone began to knock the code crazily ...........
20 Minutes later , A student's code appears bug......
Two . Implementation code
The following figure is the code written by a classmate , See if there is any problem ?
1. Students
This is the definition of Student Students , The code is as follows :

2. Student tests
This is the defined student test class , The code is shown in the following figure :

3. Running results
The above code , After running, the following error prompt appears :

According to the abnormal information in the above figure , A null pointer exception occurred in the code ! We know , The reason for the null pointer exception is that an object in the code is null !
4. Abnormal cause analysis
Null pointer exceptions are common , But why do null pointers appear ? Let's analyze the specific reasons ......
According to the prompt, we can see , The exception is in StudentTest Class 28 Row occurrence , So let's take a look at , In the 28 Which object in the row may be empty ? The first 28 The line code is as follows :

Let's look at number one 28 Line code , Which object is empty ? This is obviously students[i] This object is empty , This leads to a null pointer exception !
So why students[i] The object is empty ? our Students Array is not already new Have you passed ?
Be careful : In us new Object array , It just opens up memory space of a specified length for the array , But the element in this array defaults to null Oh ! Next, we can debug it to see if it's like this !

according to debug As a result, we can see , Sure enough, the elements in the array are null!
5. Exception resolution
Now that we know the cause of the fault , Then how to solve this problem ?
We can be in the 28 before new A student object , And store it in the array , As shown in the figure below :

Next, let's run the code , Retest , You will find that the problem of null pointer exception has been solved ! Now you know how null pointer exceptions occur , And how to solve it ? If you have other questions , You can leave a message for Mr. sol in the comment area , I will reply to you in time after I see it .
边栏推荐
- 荣耀CEO赵明:单一厂商很难实现全场景产品覆盖
- QML coding specification
- Causes and solutions of QT signal and slot connection failure
- Simply use MySQL index
- QT design robot simulation controller -- key control robot joint rotation
- Implementation of side list menu (side menu) of wechat applet
- IP day 13 notes
- Envi5.3 open GF-1 WFV data
- Jing Wei PS tutorial: basic part a
- Long awaited full platform support - Open Source im project uniapp update of openim
猜你喜欢

Check the actual data growth of the database

Jing Wei PS tutorial: basic part a

CANN训练营学习2022第二季 模型系列 动漫风格化和AOE ATC调优

Programming language exercises (I)

Cann training camp learns the animation stylization and AOE ATC tuning of the second season of 2022 model series

AXI协议(1):AMBA总线介绍,AXI概念与背景介绍,AXI协议特点与功能

内核开发

Internet Download Manager配置

Win10 download address

Qsqldatabase: solution of qmmysql driver not loaded
随机推荐
AMD锐龙7000预计9月15日上市 3D缓存版还要再等等
[数组]NC143 矩阵乘法-简单
安全的证券公司有哪些?我想在手机上买股票
Problems encountered in upgrading chrome to version 80 - solutions to system login failure
IP第十三天笔记
Programming language exercises (I)
Envi SHP to ROI and mask the grid
Axi protocol (3): handshake mechanism and implementation details of Axi architecture
Want to make sandbox games? Then you must not miss this plug-in (unity3d)
【时序逻辑电路】——计数器
QT keyboard event (I) -- detect key input
Logisim group experiment 10 single cycle MIPS CPU
QT graphical interface beginner project - unmanned aerial vehicle group combat simulation
小端格式和大端格式(Little-Endian&Big-Endian)
Qsqldatabase: solution of qmmysql driver not loaded
Explain Apache Hudi schema evolution in detail
Wechat applet list (list rendering of data rendering)
QT QML virtual keyboard
1184. 公交站间的距离
JS, call in the for loop is asynchronously converted to synchronous execution