你调用的对象是空的

时间:2021-07-11 16:57:05

I usually get this error and (always) don't know how to solve it. This time I got it also, it seems that I'm not understanding a concept or missing something Here's the code

我经常得到这个错误,并且(总是)不知道如何解决它。这次我也得到了它,似乎我不理解一个概念或遗漏一些东西这是代码

        // create a new twitteroo core with provided username/password
        TwitterooCore core = new TwitterooCore(username, password);

        // request friends timeline from twitter
        Users users = core.GetTimeline(Timeline.Friends); // error here

Please some help and also some explanations of what's happening Thanks

请一些帮助以及对正在发生的事情的一些解释谢谢

8 个解决方案

#1


I found the problem finally It was because of my Firewall seems to be blocking connections to Visual Studio. Now it works with no changes at all :) Thanks for all your support

我终于找到了问题这是因为我的防火墙似乎阻止了与Visual Studio的连接。现在它完全没有任何变化:)感谢您的支持

#2


This error is a pain, it basically means some vairable you're accessing is still Null.

这个错误是一种痛苦,它基本上意味着你正在访问的一些可用性仍然是空的。

In this instance, where do you initialise Timeline? Should your code be something like:

在这种情况下,您在哪里初始化时间轴?您的代码应该是这样的:

Users users = core.GetTimeline().Friends;

OK, I've been looking at the twiteroo documentation, which is a bit sparse, and I think you definitely need to instantiate an instance of Timeline to pass into GetTimeline, (which returns a collections of Users, not very well named IMHO). What I can't figure out is how to initiate an instance of Timeline.

好吧,我一直在看twiteroo文档,这有点稀疏,我认为你肯定需要实例化一个时间轴实例传递给GetTimeline,(它返回一个用户集合,不是很好地命名为恕我直言)。我无法弄清楚的是如何启动时间轴实例。

OK, it's not Timeline that's null, (that's an Enum!) so as bthb says, it can only be the core, perhaps the username or password are wrong, or it can't connect to twitter?

好吧,时间线不是空的,(这是一个枚举!)所以正如bthb所说,它只能是核心,也许用户名或密码是错误的,或者它无法连接到Twitter?

#3


It could be core, username, password, or Timeline.Friends, impossible to know which by the info you have given us.

它可能是核心,用户名,密码或Timeline.Friends,不可能知道你给我们的信息。

#4


If I were you I'd put a breakpoint on the line that errors, then stick a watch on Timeline.Friends and check its not null, if its not then put a watch on core.GetTimeline(Timeline.Friends) and see if thats returning null.

如果我是你,我会在错误的行上设置一个断点,然后在Timeline.Friends上查看并查看它不为空,如果不是那么就把一块表放在core.GetTimeline(Timeline.Friends)上,看看是不是返回null。

It should give you a nudge in the right direction, you'll probably have to read the documentation for the twitter API your using to find out why either of these is returning null.

它应该给你一个正确方向的推动,你可能必须阅读你使用的twitter API的文档,以找出其中任何一个返回null的原因。

#5


How about checking your code like:

检查你的代码怎么样:

Users users = null;
if (Timeline != null)
{
    TwitterooCore core = new TwitterooCore(username, password);
    if (core != null)
    {
        var friends = Timeline.Friends
        if (friends != null)
            users = core.GetTimeline(Timeline.Friends);
    }
}

If this runs without exceptions one of the objects was probably null.

如果运行没有异常,则其中一个对象可能为null。

#6


There are two specific phrases in the error message, object reference and instance of an object. These concepts are very basic when dealing with OOP languages.

错误消息,对象引用和对象实例中有两个特定短语。在处理OOP语言时,这些概念非常基础。

First, an object reference can be thought of a variable in a function or class. This term may also refer to function parameters that expect a specific reference to an object. Initially the value of a variable is NULL until it is set to a value using the '=' operator. Frequently you will have a variable declaration and the '=' operation in the same statement.

首先,可以将对象引用视为函数或类中的变量。该术语还可以指期望对对象的特定引用的函数参数。最初,变量的值为NULL,直到使用'='运算符将其设置为值。通常,您将在同一语句中使用变量声明和“=”操作。

The term instance of an object refers to an object that has been created using the syntax new. When you call new to initialize an object, an unused memory location is allocated to store a copy of the object until the program ends, or the object goes out of scope and is freed by the garbage collector. At creation time the object properties are defined by the constructor method called to create the object.

对象的术语实例是指使用语法new创建的对象。当您调用new来初始化对象时,会分配一个未使用的内存位置来存储该对象的副本,直到该程序结束,或者该对象超出范围并被垃圾收集器释放。在创建时,对象属性由调用以创建对象的构造方法定义。

Consider this code:

考虑以下代码:

Integer my_int;
my_int = new Integer(5);

In this example, 'my_int' is the object reference to an Integer object instance being created.

在此示例中,'my_int'是对正在创建的Integer对象实例的对象引用。

If you try to access 'my_int', before assigning it a reference to an Integer instance, then you would have the error, "an object reference (my_int) not set to an instance of an object (Integer)".

如果您尝试访问'my_int',在为其指定对Integer实例的引用之前,则会出现错误,“对象引用(my_int)未设置为对象的实例(Integer)”。

#7


If you decompile the dll you will see that GetTimeline(Enum) takes in a Enumeration argument.

如果您反编译该DLL,您将看到GetTimeline(Enum)接受Enumeration参数。

The Constructor call will be fine:

构造函数调用没问题:

TwitterooCore core = new TwitterooCore(username, password);

Source:

public TwitterooCore(string username, string password)
{
        this._username = username;
        this._password = password;
}

GetTimeline is were the connection is attempted.

GetTimeline是尝试连接的。

public Users GetTimeline(Timeline timeline)
{
    WebClient client = new WebClient();
    XmlDocument document = new XmlDocument();
    string xml = string.Empty;
    byte[] buffer = null;
    client.set_Credentials(this.GetCredentials());
    buffer = client.DownloadData(this.GetTimelineUrl(timeline));
    xml = Encoding.UTF8.GetString(buffer);
    document.LoadXml(xml);
    return this.DecodeStatusXml(document);
}

#8


May be Timeline.Friends is null and may be Timeline is null. I suggest you look at the stack trace of exception and into documentation of your twitter framework.

可能是Timeline.Friends为null,可能是Timeline为null。我建议你查看异常的堆栈跟踪和twitter框架的文档。

#1


I found the problem finally It was because of my Firewall seems to be blocking connections to Visual Studio. Now it works with no changes at all :) Thanks for all your support

我终于找到了问题这是因为我的防火墙似乎阻止了与Visual Studio的连接。现在它完全没有任何变化:)感谢您的支持

#2


This error is a pain, it basically means some vairable you're accessing is still Null.

这个错误是一种痛苦,它基本上意味着你正在访问的一些可用性仍然是空的。

In this instance, where do you initialise Timeline? Should your code be something like:

在这种情况下,您在哪里初始化时间轴?您的代码应该是这样的:

Users users = core.GetTimeline().Friends;

OK, I've been looking at the twiteroo documentation, which is a bit sparse, and I think you definitely need to instantiate an instance of Timeline to pass into GetTimeline, (which returns a collections of Users, not very well named IMHO). What I can't figure out is how to initiate an instance of Timeline.

好吧,我一直在看twiteroo文档,这有点稀疏,我认为你肯定需要实例化一个时间轴实例传递给GetTimeline,(它返回一个用户集合,不是很好地命名为恕我直言)。我无法弄清楚的是如何启动时间轴实例。

OK, it's not Timeline that's null, (that's an Enum!) so as bthb says, it can only be the core, perhaps the username or password are wrong, or it can't connect to twitter?

好吧,时间线不是空的,(这是一个枚举!)所以正如bthb所说,它只能是核心,也许用户名或密码是错误的,或者它无法连接到Twitter?

#3


It could be core, username, password, or Timeline.Friends, impossible to know which by the info you have given us.

它可能是核心,用户名,密码或Timeline.Friends,不可能知道你给我们的信息。

#4


If I were you I'd put a breakpoint on the line that errors, then stick a watch on Timeline.Friends and check its not null, if its not then put a watch on core.GetTimeline(Timeline.Friends) and see if thats returning null.

如果我是你,我会在错误的行上设置一个断点,然后在Timeline.Friends上查看并查看它不为空,如果不是那么就把一块表放在core.GetTimeline(Timeline.Friends)上,看看是不是返回null。

It should give you a nudge in the right direction, you'll probably have to read the documentation for the twitter API your using to find out why either of these is returning null.

它应该给你一个正确方向的推动,你可能必须阅读你使用的twitter API的文档,以找出其中任何一个返回null的原因。

#5


How about checking your code like:

检查你的代码怎么样:

Users users = null;
if (Timeline != null)
{
    TwitterooCore core = new TwitterooCore(username, password);
    if (core != null)
    {
        var friends = Timeline.Friends
        if (friends != null)
            users = core.GetTimeline(Timeline.Friends);
    }
}

If this runs without exceptions one of the objects was probably null.

如果运行没有异常,则其中一个对象可能为null。

#6


There are two specific phrases in the error message, object reference and instance of an object. These concepts are very basic when dealing with OOP languages.

错误消息,对象引用和对象实例中有两个特定短语。在处理OOP语言时,这些概念非常基础。

First, an object reference can be thought of a variable in a function or class. This term may also refer to function parameters that expect a specific reference to an object. Initially the value of a variable is NULL until it is set to a value using the '=' operator. Frequently you will have a variable declaration and the '=' operation in the same statement.

首先,可以将对象引用视为函数或类中的变量。该术语还可以指期望对对象的特定引用的函数参数。最初,变量的值为NULL,直到使用'='运算符将其设置为值。通常,您将在同一语句中使用变量声明和“=”操作。

The term instance of an object refers to an object that has been created using the syntax new. When you call new to initialize an object, an unused memory location is allocated to store a copy of the object until the program ends, or the object goes out of scope and is freed by the garbage collector. At creation time the object properties are defined by the constructor method called to create the object.

对象的术语实例是指使用语法new创建的对象。当您调用new来初始化对象时,会分配一个未使用的内存位置来存储该对象的副本,直到该程序结束,或者该对象超出范围并被垃圾收集器释放。在创建时,对象属性由调用以创建对象的构造方法定义。

Consider this code:

考虑以下代码:

Integer my_int;
my_int = new Integer(5);

In this example, 'my_int' is the object reference to an Integer object instance being created.

在此示例中,'my_int'是对正在创建的Integer对象实例的对象引用。

If you try to access 'my_int', before assigning it a reference to an Integer instance, then you would have the error, "an object reference (my_int) not set to an instance of an object (Integer)".

如果您尝试访问'my_int',在为其指定对Integer实例的引用之前,则会出现错误,“对象引用(my_int)未设置为对象的实例(Integer)”。

#7


If you decompile the dll you will see that GetTimeline(Enum) takes in a Enumeration argument.

如果您反编译该DLL,您将看到GetTimeline(Enum)接受Enumeration参数。

The Constructor call will be fine:

构造函数调用没问题:

TwitterooCore core = new TwitterooCore(username, password);

Source:

public TwitterooCore(string username, string password)
{
        this._username = username;
        this._password = password;
}

GetTimeline is were the connection is attempted.

GetTimeline是尝试连接的。

public Users GetTimeline(Timeline timeline)
{
    WebClient client = new WebClient();
    XmlDocument document = new XmlDocument();
    string xml = string.Empty;
    byte[] buffer = null;
    client.set_Credentials(this.GetCredentials());
    buffer = client.DownloadData(this.GetTimelineUrl(timeline));
    xml = Encoding.UTF8.GetString(buffer);
    document.LoadXml(xml);
    return this.DecodeStatusXml(document);
}

#8


May be Timeline.Friends is null and may be Timeline is null. I suggest you look at the stack trace of exception and into documentation of your twitter framework.

可能是Timeline.Friends为null,可能是Timeline为null。我建议你查看异常的堆栈跟踪和twitter框架的文档。