Null Pointer exception

Source: Internet
Author: User

In Java programming, one of the most common repeated (most complained) errors is a null pointer exception. Tracking the cause of one of these errors will cause you to doubt your decision. In diagnosing this part of Java code, we compile the most common type associated with NULL pointer exceptions into a directory to continue our error type check, analyze a class with NULL pointer exceptions step by step. Then we will review several programming techniques to help you reduce the occurrence of such type errors.
Among all the exceptions that a Java programmer can encounter, the NULL pointer exception is the most terrible because it is the least information exception provided by the program. For example, unlike a class transformation exception, a null pointer exception does not give any information about the content it requires. It only has one null pointer. In addition, it does not indicate where the NULL pointer in the Code is assigned a value. In many NULL pointer exceptions, real errors occur when the variable is assigned a null value. In order to detect errors, we must trace the control flow to find out where the variables are assigned, and determine whether or not to do so is incorrect. When the value assignment appears in the package, rather than in the place where an error is reported, the process is obviously damaged.
Many Java developers tell me that the vast majority of program crashes are null pointer exceptions, and they are eager to have a tool that can statically identify these errors before the program runs for the first time. Unfortunately, the automatic control theory tells us that no tool can statically decide which programs will throw a null pointer exception. However, in a program, it is possible to use a tool to exclude many NULL pointer exceptions, leaving us with only a small number of potential problems that require us to manually check. In fact, some research is being done to provide such a tool for Java programs (see references. But a good tool can only do this for us. Null Pointer exceptions will never be completely eliminated. When they happen, tools can help us identify the types of errors associated with them, so that we can quickly diagnose them. In addition, we can apply some programming and design skills to significantly reduce the appearance of these types of errors.
Suspension compound type:
The first error type we will discuss about NULL pointer exceptions is an error type that I call the suspension compound type. Errors of this type are generated as follows: some of the basic examples defined are not given their own classes, and then a recursive data type is defined using this method. Instead, null pointers are inserted into different composite data types. Data Type instances are used as if the NULL pointer is correctly filled. I call it the suspension compound type because the conflicting code is a flawed application of the composite design type. The composite data type contains the suspension reference (that is, a null pointer ).
Cause:
Consider the following single join execution of the sort list class, which has a suspension compound type. For the sake of simplicity, I only execute some methods defined in Java. util. Collections list. To show how concealed this type of error is, I have introduced an error in the following code. See if you can find it.
List 1. Single-link linked list
Import java. util. nosuchelementexception;
Public class category list {
Private object first;
Private writable list rest;
/**
* Constructs an empty Quota List.
*/
Public writable list (){
This. First = NULL;
This. Rest = NULL;
}
/**
* Constructs a pending list containing only the given element.
*/
Public writable list (object _ First ){
This. First = _ first;
This. Rest = NULL;
}
/**
* Constructs a jsonlist consisting of the given object followed
* All the elements in the given provided list.
*/
Public writable list (object _ first, writable LIST _ rest ){
This. First = _ first;
This. Rest = _ rest;
}
}
This code is pretty bad. It places a null pointer in both fields to represent the empty linked list, instead of defining a separate class for the empty linked list. In the beginning, this method is used to represent an empty linked list to make the code simple. After all, we don't have to define an additional class just for the empty linked list. However, as I will prove, such a simple operation is just an illusion. Let's define some reader and setter methods for this class:

Listing 2. Define a method for the consumer list
Public object getfirst (){
If (! (This. isempty ())){
Return this. First;
}
Else {
Throw new nosuchelementexception ();
}
}
Public writable list getrest (){
If (! (This. isempty ())){
Return this. rest;
}
Else {
Throw new nosuchelementexception ();
}
}
Public void addfirst (Object O ){

Using list oldthis = (using list) This. Clone ();

This. First = O;
This. Rest = oldthis;
}
Public Boolean isempty (){
Return this. First = NULL & this. Rest = NULL;
}
Private object clone (){
Return new vertex list (this. First, this. Rest );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.