views:

967

answers:

8

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

A: 

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.

Alex Reitbort
nop it's not null I looked :(
Omar Abid
+2  A: 

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.

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?

Andrew M
what's the null object? Core or users??users is NULL
Omar Abid
I now think it's Core that's null. Possibly because username or password are wrong?
Andrew M
+1  A: 

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

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.

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);
}
cgreeno
+1  A: 

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

Ed Swangren
+1  A: 

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.

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.

Gavin Draper
no one is returning null I checked
Omar Abid
+2  A: 

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

Omar Abid
You should catch that when you can't connect ;)
Stormenet
oh yeah this will be after testing functionalities :)
Omar Abid
+1  A: 

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.

Ole Lynge
Checking that your newly created instance is not null is completely unnecessary.
erikkallen
@erikkallen: Ah... Thanks for that comment. What you write sounds right... Is there absolutely no way that it could be null?
Ole Lynge
+1  A: 

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.

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.

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.

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.

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)".

Casey
this is not true, or I miss understood you :(Integer my_int = 5;it always works !!
Omar Abid
Sorry it is not clear. I did not mean to imply that the code was an example of the error defined in the question, but merely an example to demonstrate the text in the answer. Obviously this code works.
Casey