"Object reference not set to an instance of an object" Exception

SaeedP

Well-known member
Joined
Oct 21, 2020
Messages
95
Programming Experience
3-5
Hello,

I have installed InstagramApiSharp from NuGet to my project. When I try to enter my account I encounter this expection:
"Unable to login: Object reference not set to an instance of an object."
But I have defined the object reference. Here comes a piece of my code:

C#:
public static IInstaApi _instaApi;

private static async Task StartInstagram()
    {
        var userSession = new UserSessionData
        {
            UserName = "username",
            Password = "password"
        };

        var _instaApi = InstaApiBuilder.CreateBuilder()
    .SetUser(userSession)
    .UseLogger(new DebugLogger(LogLevel.Exceptions))
    .Build();

Where do I have made a mistake?

thanks,
 
The downside of fluent interfaces... debugging when a step in the fluent interface returns a null object. Anyway, you'll have to step through the code to try to see which line actually throws the exception. If I had to guess, it's the CreateBuilder() method which returns a null, but the only true way to know is to step through with the debugger.
 
I'll also mention that in another thread asking about this same API, @Sheepings gave a warning that this library is not supported by Facebook as a valid API to use Instagram and that you risk getting your account closed or banned.
 
I'll also mention that in another thread asking about this same API, @Sheepings gave a warning that this library is not supported by Facebook as a valid API to use Instagram and that you risk getting your account closed or banned.

You could see the whole code here:
C#:
private static async Task StartInstagram()
    {
        var userSession = new UserSessionData
        {
            UserName = "username",
            Password = "password"
        };

        var _instaApi = InstaApiBuilder.CreateBuilder()
    .SetUser(userSession)
    .UseLogger(new DebugLogger(LogLevel.Exceptions))
    .Build();


        const string stateFile = "state.bin";
        try
        {
            // load session file if exists
            if (File.Exists(stateFile))
            {
                //Console.WriteLine("message = "";");
                message = "Loading state from file";

                using (var fs = File.OpenRead(stateFile))
                {
                    [B]_instaApi.LoadStateDataFromStream(fs);[/B]
                    
                }
            }
        }

        catch (Exception e)
        {
            ExceptionMessage = $" error: {e}";
        }
        if (!_instaApi.IsUserAuthenticated)
        {
            // login


            message = $"Logging in as {userSession.UserName}";
            var logInResult = await _instaApi.LoginAsync();
            if (!logInResult.Succeeded)
            {
                //Console.WriteLine($"Unable to login: {logInResult.Info.Message}");
                message = $"Unable to login: {logInResult.Info.Message}";
                return;
            }
        }

        // save session in file
        [B]var state = _instaApi.GetStateDataAsStream();[/B]
        
        using (var fileStream = File.Create(stateFile))
        {
            state.Seek(0, SeekOrigin.Begin);
            state.CopyTo(fileStream);
        }

    }

What is your idea about the bold part? should I use string instead of stream?
I just test the API.
 
I have never seen File.OpenRead() return null before, so if I were to make a bet, it would be that Build() returns a null object so that _instaApi is null.

Why don't you just step through your code and actually tell us where the null object is at? What's preventing you from taking advantage of your debugger?
 
I'll also mention that in another thread asking about this same API, @Sheepings gave a warning that this library is not supported by Facebook as a valid API to use Instagram and that you risk getting your account closed or banned.
*sigh* Apparently it wasn't just @Sheepings who warned you. You have at least 2 other threads (if not 3) with regards to using the InstaGram outside of the official InstaGram API's and each time you were warned that you really shouldn't do that because it is not supported.
 
Yes, I have made this app before.
I was informed through forums.asp.net that this works. I just want to try it and just change my account page.
Don't you see all these apps that work with third-party Instagram APIs?
I draw your attention to Canva.com

Tips about my code:
Of course, I will debug it but in sample code is written:

If you make this app for .net core:

C#:
_instaApi.LoadStateDataFromStream(fs);
            // in .net core or uwp apps don't use LoadStateDataFromStream
            // use this one:
            // _instaApi.LoadStateDataFromString(new StreamReader(fs).ReadToEnd());
            // you should pass json string as parameter to this function.

C#:
var state = _instaApi.GetStateDataAsStream();
// in .net core or uwp apps don't use GetStateDataAsStream.
// use this one:
// var state = _instaApi.GetStateDataAsString();
// this returns you session as json string.

I use this app with Blazor server_side, but when I use recommended methods, VS doesn't accept the code and a red line appears under them.
You can see an example:

regards,
 
The redlines are there to indicate an compilation error. Tell us what those compilation errors are.
 
Don't you see all these apps that work with third-party Instagram APIs?
I draw your attention to Canva.com
I don't know. I usually don't disassemble application to see what APIs they use -- e.g. whether they are using the official InstaGram APIs or if they are using some other library which may or may not be using the official InstaGram APIs.
 
Yes, I have made this app before.
I was informed through forums.asp.net that this works.
Yes, and I've also driven my car at 100MPH. It works as well, but It still doesn't make it legal for me to drive at that speed.
 
Hello,

Sorry for the inconvenience.

When I add the ApiSharpPackage to the startup configuration service:
C#:
services.AddInstagramApiSharp();

A red line appears under it and doesn't accept it.

_instaApi.LoadStateDataFromString(new StreamReader(fs).ReadToEnd()); instead of _instaApi.LoadStateDataFromString(new StreamReader(fs).ReadToEnd());

As I told you in a later post a red line appears under it and it says:

IInstaApi does not contain a definition for LoadStateFromString.

What is your opinion?

thanks
 
Your code from post #4 compiles for me without any issues. I'm using version 1.5.0.2 of the Nuget package that you referenced in post #7. The console app I have is using .NET 5.
 
Back
Top Bottom