Reputation: 73
Hi guys I have such code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
struct Proxy
{
public static List<string> proxyList;
public static string type;
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Title = "Choose file with proxy";
openFileDialog1.InitialDirectory = System.Environment.CurrentDirectory;
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
if (checkBox1.Checked)
Proxy.type = "socks5";
else
Proxy.type = "http";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
foreach (string prox in File.ReadAllLines(openFileDialog1.FileName))
{
if (prox.Contains(":"))
{
string[] proxy = prox.Split(':');
Proxy.proxyList.Add(prox);
}
}
MessageBox.Show(Proxy.proxyList.Count.ToString());
}
}
}
}
But when I load txt file:
62.109.28.37:8085
193.0.147.23:8085
193.0.147.90:8085
193.0.147.61:8085
193.0.147.47:8085
193.0.147.93:8085
I receive an exception: on line Proxy.proxyList.Add(prox);
Object reference not set to an instance of an object.
Why?=\
Upvotes: 1
Views: 119
Reputation: 23324
Because proxyList
is null
. Change
public static List<string> proxyList;
to
public static List<string> proxyList = new List<string>();
Upvotes: 6
Reputation: 9639
You need to construct the list too, before you use it, e.g., before your foreach loop:
Proxy.proxyList = new List<string>();
Or you could do this in the struct definition itself:
struct Proxy
{
public static List<string> proxyList = new List<string>();
public static string type;
}
Upvotes: 6