The object is not referenced to the instance. The object references the instance.

Source: Internet
Author: User

The object is not referenced to the instance. The object references the instance.
Attribute or method called to an object when the object is null

"Not referencing objects to instances" is a problem that many beginners like me often encounter. It is often annoying. How does this problem happen? First, let's take a look at the figure to analyze the cause of this error.

A lot of people may think this code is ridiculous: "You can write this code. It seems that this person's IQ is" Beyond "The Human category! "

But some uncle Superman wearing underpants often does not occasionally write such code and encounter such problems.

From this figure, we can easily see that Too. f1.array [I] is null. At this time, we will certainly report an error when calling its properties or methods! Why does an error occur when an object is null when its attributes or methods are called?

We all know that arrays are reference types. The reference data values are stored in the heap memory and stored in the stack as a string of memory addresses:

In this figure, we can see that its attribute is stored in the heap memory, and the object is null, which means that the object has not opened up space in the heap memory, therefore, you cannot access the attributes stored in the heap memory through the stack memory. Therefore, an error is prompted during system running.

Problems caused by assigning reference data to data of the same type

Let's take a look at the figure:

At first glance, there seems to be nothing wrong with this code, and there is no syntax error! However, the following figure shows the problem that the object is not referenced to the instance during running:

What is the problem? We have mentioned that the stack memory stores a string of heap memory addresses. While sb = txt only assigns the memory address of the array txt In the heap memory to the array sb, that is, the memory address of the array txt and the array sb in the stack memory are the same, txt [0] = null: Delete the space corresponding to txt [0] In the heap memory, the array txt and array sb share the same memory address in the heap memory. Therefore, the array sb [0] does not have the corresponding heap memory space. Therefore, an error is reported during running, for the principle, see the property or method called to the object when the object is null.

This document is intended for Comrade Lele, who surpasses mankind!

Related Article

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.