locked
Object reference error RRS feed

  • Question

  • User172056746 posted

    Hello,

    I'm trying to enter my Instagram account with InstagramApiSharp, but encounter  this expectation error:

    Unable to login: Object reference not set to an instance of an object.

    Do you see anything wrong with this code?

    @code{
    
        public static IInstaApi _instaApi;
    
        public static string username { get; set; }
        public static string password { get; set; }
    
        public static string message = string.Empty;
        
        public static string ExceptionMessage = string.Empty;
       
       
    
        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))
                    {
                        _instaApi.LoadStateDataFromStream(fs);
                        
                    }
                }
            }
    
            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
            var state = _instaApi.GetStateDataAsStream();
            
            using (var fileStream = File.Create(stateFile))
            {
                state.Seek(0, SeekOrigin.Begin);
                state.CopyTo(fileStream);
            }
    
        }
    
    
    
    }
    

    Thursday, March 4, 2021 8:45 AM

All replies

  • User753101303 posted

    Hi,

    The problem is that the error messsage seems to come from

    message = $"Unable to login: {logInResult.Info.Message}";

    So it would be an exception that happens inside the library and is returned this way which doesn't seems a good idea. You can't get the full exception to know in which internal method it happens? Seems you need to initialize something else before calling LoginAsync.

    Wednesday, March 10, 2021 11:34 AM