-2

Ok so i'm currently building a project where you import a list of proxy's but for some reason I'm getting a System.IndexOutOfRangeException Error which is really bugging me now.

So here is the code it's showing on.

        private void LoadProxies()
    {
        accChecker.Proxies.Clear();

        using (OpenFileDialog ofd = new OpenFileDialog())
        {
            ofd.Title = "Choose a file containing a list of proxies...";
            ofd.Filter = "Text Files (*.txt)|*.txt";

            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                foreach (String line in File.ReadAllLines(ofd.FileName))
                {
                    if (line.Contains(":"))
                    {
                        String[] data = line.Split(':');

                        if (!Properties.Settings.Default.ProxiesLogin)
                            accChecker.QueueProxy(data[0], data[1]);
                        else
                            accChecker.QueueProxy(data[0], data[1], data[2], data[3]);
                    }
                }
                loadProxiesBtn.Text = String.Format("Load Proxies ({0})", accChecker.Proxies.Count);

                if (accChecker.Accounts.Count > 0 && (accChecker.Proxies.Count > 0 || !loadProxiesBtn.Visible))
                    checkBtn.Enabled = true;
                else
                    checkBtn.Enabled = false;

                UpdateInfos(true);
            }
        }
    }

When continue is clicked it then closes the application. I'm not sure if it's a form of my code?

2 Answers2

0

You problem persists with following code set [With reference to attached picture]

if (line.Contains(":"))
{
   String[] data = line.Split(':');

     if (!Properties.Settings.Default.ProxiesLogin)
        accChecker.QueueProxy(data[0], data[1]);
     else
         accChecker.QueueProxy(data[0], data[1], data[2], data[3]); // HERE
 }

Here you do not know whethere your string array actually contains data[0], data[1], data[2] or data[3] positions. More about NullReferenceException

Solution :

Check your array's Length property to verify you have data in these potions

So something like

 if (!Properties.Settings.Default.ProxiesLogin){
       if(data.Length > 1){
          accChecker.QueueProxy(data[0], data[1]);
       }
   }
   else{
       if(data.Length > 3){
         accChecker.QueueProxy(data[0], data[1], data[2], data[3]);
       }
   }

This won't solve your application logic. You will have to implement some logic to face this Length condition fails. something like UpdateInfos(false); [Thinking that's what you expect from this function]

Community
  • 1
  • 1
Kavindu Dodanduwa
  • 9,413
  • 2
  • 26
  • 40
0

You should check the length of data result.

  String[] data = line.Split(':');
Roman Ambinder
  • 369
  • 3
  • 7