简体   繁体   English

C#未处理的异常:System.NullReferenceException:对象引用未设置为对象的实例

[英]C# Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object

Hello, I have a problem understanding why this piece of code: 您好,我在理解这段代码的原因时遇到了一个问题:

using System;

class Person
{
    public Person()
    {
    }
}


class NameApp
{
    public static void Main()
    {
        Person me = new Person();
        Object you = new Object();

        me = you as Person;
        //me = (Person) you;

        System.Console.WriteLine("Type: {0}", me.GetType()); //This line throws exception
    }
}

Throws this exception: 引发此异常:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. 未处理的异常:System.NullReferenceException:对象引用未设置为对象的实例。 at NameApp.Main() in C:\\Users\\Nenad\\documents\\visual studio 2010\\Projects\\Exercise 11.3\\Exercise 11.3\\Program.cs:line 21 在NameApp.Main()中的C:\\ Users \\ Nenad \\ documents \\ Visual Studio 2010 \\ Projects \\ Exercise 11.3 \\ Exercise 11.3 \\ Program.cs:第21行

Your line 你的线

me = you as Person;

is failing and assigning null to me because you can't cast a base class type object to child class. 失败并为me分配了null,因为您无法将基类类型对象强制转换为子类。

as (C# Reference) 作为(C#参考)

The as operator is like a cast operation. as运算符就像强制转换操作。 However, if the conversion isn't possible, as returns null instead of raising an exception . 但是, 如果无法进行转换,则as返回null而不是引发exception

You probably want to cast person as object , since me is an Object , but you is not a person. 您可能想将personobject ,因为meObject ,但是you不是人。

This code will always set me as null 此代码将始终将me设置为null

   Object you = new Object();
   me = you as Person;

because Obejct is not person 因为Obejct 不是

but person is object : 但是人是object

object you = new Person();
me = you as Person;
Object you = new Object();
me = you as Person;

you is an object, not a Person so you as Person will simply return null. you是对象,而不是Person,因此you as Person将只返回null。

me = you as Person;

如果无法将you强制转换为Personmenull (这就是您的情况,因为无法将new Object()强制转换为Person

The as operator returns null if the object was not of the type you requested. 如果对象不是您请求的类型,则as运算符返回null。

me = you as Person;

you is an Object, not a Person, so (you as Person) is null and therefore me is null. 您是一个对象,而不是一个人,因此(您作为人)为空,因此我为空。 When you call GetType() on me afterwards, you get a NullReferenceException. 之后,当您对我调用GetType()时,您将收到NullReferenceException。

public static void Main()
{
    Person me = new Person();
    Object you = new Object();

    // you as person = null
    me = you as Person;
    //me = (Person) you;

    System.Console.WriteLine("Type: {0}", me.GetType()); //This line throws exception
}

Object can not be cast to a Person. 无法将对象投射到人。 It is a princip of object oriented programming. 它是面向对象编程的原理。

Object is a parent class of a Person. 对象是Person的父类。 Every class inherits it, 每个班级都继承

you can cast Person to Object, but cannot cast Object to Person 您可以将Person转换为Object,但不能将Object转换为Person

If you cast with the as keyword and the cast is not possible it returns null . 如果使用as关键字进行强制转换并且无法强制转换,则返回null Then in your case you call me.GetType() with me being null at the moment so an exception is thrown. 然后,在您的情况下,您调用me.GetType() ,而此时menull ,因此引发异常。

If you cast like (Person) objectOfTypeThatDoesNotExtendPerson an exception is thrown instantly at the cast. 如果您像(Person) objectOfTypeThatDoesNotExtendPerson那样进行(Person) objectOfTypeThatDoesNotExtendPerson ,则转换时会立即引发异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# Npgsql 连接检查器异常抛出 System.NullReferenceException '对象引用未设置为 object 的实例。 - C# Npgsql connection checker Exception Throw System.NullReferenceException 'Object reference not set to an instance of an object.' CSharp未处理的异常:System.NullReferenceException:对象引用未设置为对象的实例 - CSharp Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object 未处理的异常。 System.NullReferenceException:未将对象引用设置为对象的实例 - Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object 类型'System.NullReferenceException'的异常:对象引用未设置为对象的实例 - An exception of type 'System.NullReferenceException' : Object reference not set to an instance of an object System.NullReferenceException-对象引用未设置为对象的实例。 C#中的原因 - System.NullReferenceException - Object reference not set to an instance of an object. Causes in C# 未处理C#NullReferenceException-未将对象引用设置为对象的实例 - C# NullReferenceException was unhandled - Object reference not set to an instance of an object 会话{“对象引用未设置为对象的实例。”} System.Exception {System.NullReferenceException} - session {“Object reference not set to an instance of an object.”} System.Exception {System.NullReferenceException} System.NullReferenceException - 未将对象引用设置为对象的实例 - System.NullReferenceException – Object reference not set to an instance of an object Foreach System.NullReferenceException:未将对象引用设置为对象的实例 - Foreach System.NullReferenceException: Object reference not set to an instance of an object System.NullReferenceException:对象引用未设置为对象的实例 - System.NullReferenceException: Object reference not set to an instance of an object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM