当前位置:网站首页>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 .
边栏推荐
猜你喜欢
随机推荐
快速入门
Xxx.pro learning in QT
Internet Download Manager Configuration
OS Experiment 5 process switching based on kernel stack switching
Template method mode
Cross server, insert, search
MODIS 16 day data monthly / quarterly synthesis
CANN训练营学习2022第二季 模型系列 动漫风格化和AOE ATC调优
剑指 Offer 22. 链表中倒数第k个节点
MySQL addition, deletion, modification, retrieval and constraint (detailed teaching)
Meeting OA project progress (I)
[数组]NC143 矩阵乘法-简单
The industrial information security center takes the lead in building a data circulation platform, and ant group and other manufacturers provide technical support
CPU comparison
Duplicate content in lookup table
Shardingsphere database read / write separation
Digital transformation must have digital thinking
[machine learning basics] - another perspective to explain SVM
JSP custom tag library --foreach
Mcd12q1 data shows multiple classifications in envi








