Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi, i'm making a software that you can type the name and it'll search on instagram and scrape the info and show it to you . now, i want it to show the name and other things on a label but it says an object reference is required ... .
i get that error in "lbl_Name" and this is the code:


public static async void PullUserPosts(string usertoscrape)
        {
            IResult<InstaUser> UserSearch = await api.GetUserAsync(usertoscrape);
            lbl_Name.text($"USER: {UserSearch.Value.FullName}\n\tFollowers: {UserSearch.Value.FollowersCount} \n\tVerified {UserSearch.Value.IsVerified}");

            IResult<InstaMediaList> media = await api.GetUserMediaAsync(usertoscrape, PaginationParameters.MaxPagesToLoad(4));
            List<InstaMedia> mediaList = media.Value.ToList();



public partial class Form1 : Form
{
    private const string username = "";
    private const string password = "";

    private static UserSessionData user;

    private static IInstaApi api;


    private void Form1_Load(object sender, EventArgs e)
    {
        user = new UserSessionData();
        user.UserName = username;
        user.Password = password;

        Login();
        //lbl_Name.Text = $"USER: {UserSearch.Value.FullName}\n\tFollowers";
    }

    public Form1()
    {

        InitializeComponent();
    }

    private void Btn_Search_Click(object sender, EventArgs e)
    {



    }

    public static async void Login()
    {
        api = InstaApiBuilder.CreateBuilder()
            .SetUser(user)
            .UseLogger(new DebugLogger(LogLevel.Exceptions))
            .SetRequestDelay(TimeSpan.FromSeconds(8))
            .Build();


        var loginrequest = await api.LoginAsync();
        if (loginrequest.Succeeded)
        {
            MessageBox.Show("logged in");
            PullUserPosts("garyvee");
        }

        else
            MessageBox.Show("error logging in!\n" + loginrequest.Info.Message);
    }




    public static async void PullUserPosts(string usertoscrape)
    {
        IResult<InstaUser> UserSearch = await api.GetUserAsync(usertoscrape);
        lbl_Name.Text ($"USER: {UserSearch.Value.FullName}\n\tFollowers: {UserSearch.Value.FollowersCount} \n\tVerified {UserSearch.Value.IsVerified}");

        IResult<InstaMediaList> media = await api.GetUserMediaAsync(usertoscrape, PaginationParameters.MaxPagesToLoad(4));
        List<InstaMedia> mediaList = media.Value.ToList();


        for (int i = 0; i < mediaList.Count; i++)
        {
            InstaMedia m = mediaList[i];
            if (m != null && m.Caption != null)
            {
                string captiontext = m.Caption.Text;
                if (captiontext != null)
                {
                    if (m.MediaType == InstaMediaType.Image)
                    {
                        for (int x = 0; x < m.Images.Count; x++)
                        {
                            if (m.Images[x] != null && m.Images[x].URI != null)
                            {
                                MessageBox.Show($"\n\t{captiontext}\n\t");
                                string uri = m.Images[x].URI;

                                MessageBox.Show($"{uri}\n\t");

                            }
                        }
                    }
                }
            }
        }

    }


}

so what should i do to make it show the results in a label ?!
thank you.

What I have tried:

messagebox.show and it worked fine
but that's not what i want .
Posted
Updated 30-Oct-18 4:56am
v2

lbl_Name.Text = $"USER: {UserSearch.Value.FullName}\n\tFollowers: {UserSearch.Value.FollowersCount} \n\tVerified {UserSearch.Value.IsVerified}";


If you're still getting a null reference then something you are referencing (something before a "." like lbl_Name for lbl_Name.Text) is null. We can't tell you that from here, you'll need to use the debugger to figure out what is null, and then why it is null, and from that decide what to do when it is null.
 
Share this answer
 
Comments
brandon1999 30-Oct-18 10:24am    
but i checked nothing was null !!! i added the whole code . when i start debuging it logins to my instagram account and then scrape some info, nothing should be null .
F-ES Sitecore 30-Oct-18 10:50am    
You can't get a null reference exception unless you are referencing something that is null.
brandon1999 30-Oct-18 10:57am    
thank you for the answer and the time . i just needed to remove the "static" keyword .
i removed the "static" keyword from PullUserPosts method and public static async void Login and it fixed .

a big thanks to @Luthfay from stackoverflow for helping me .
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900