Object reference error RRS feed

  • Question

  • User172056746 posted


    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?

        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()
        .UseLogger(new DebugLogger(LogLevel.Exceptions))
            const string stateFile = "state.bin";
                // load session file if exists
                if (File.Exists(stateFile))
                    //Console.WriteLine("message = "";");
                    message = "Loading state from file";
                    using (var fs = File.OpenRead(stateFile))
            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}";
            // save session in file
            var state = _instaApi.GetStateDataAsStream();
            using (var fileStream = File.Create(stateFile))
                state.Seek(0, SeekOrigin.Begin);

    Thursday, March 4, 2021 8:45 AM

All replies

  • User753101303 posted


    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