2 Replies - 787 Views - Last Post: 27 November 2013 - 01:34 PM Rate Topic: -----

#1 vpdic  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 48
  • Joined: 24-November 13

Object reference not set to an instance of an object in a class array

Posted 27 November 2013 - 01:21 PM

This error is occurring on line "SL1[arrayIndex].IPAddress = proxyIP;," approximately line #78. I checked the proxyIP variable and it was showing a proper string as intended. Alternatively I could use a List<> but I'm not 100% sure on how to create a new list object per While in the streamreader loop.

An unhandled exception of type 'System.NullReferenceException' occurred in ProxyScanner.exe
Additional information: Object reference not set to an instance of an object.



   public class proxyScanlist
    {
        public string IPAddress { get; set; }
        public ushort Port { get; set; }
        public byte Type { get; set; }
        public bool Verified { get; set; }
    }


    public static class Setup_Proxies
    {
        public static proxyScanlist[] SL1 = new proxyScanlist[File.ReadAllLines("List\\Scanlist_01.txt").Length];
        //create an array based on the amount of lines in a text file
        
        //alternative ---v
        //public static List<proxyScanlist> myScanlist = new List<proxyScanlist>();

        public static void createScanlist()
        {
            string proxyIP;
            ushort proxyPort;
            string fileLine;
            string[] parseBox;
            int indexFinder;
            long arrayIndex = 0; //which index of the array were on. not likely to ever need a value this high
            byte proxyCode;
            //variables :)/>
            StreamReader fileReader = new StreamReader("List\\Scanlist_01.txt");
            
            while ((fileLine = fileReader.ReadLine()) != null)
            {

                fileLine = fileLine.TrimStart();
                //remove any spaces from the start
                parseBox = fileLine.Split(' ');
                fileLine = parseBox[0];
                //get rid of the junk text if there is any

                if (fileLine.Length > 6)
                {
                    indexFinder = fileLine.IndexOf("@");
                    if (indexFinder > 3)
                    {
                        switch (fileLine.ToLower().Substring(fileLine.Length-2))
                        {
                            case "tp": //http
                                proxyCode = 0;
                                break;
                            case "s4": //socks5 or s4
                                proxyCode = 1;
                                break;
                            case "s5": //socks5 or s5
                                proxyCode = 2;
                                break;
                            default:
                                proxyCode = 3;
                                break;
                        }
                        fileLine = fileLine.Substring(0, indexFinder);
                        //remove the proxy type indicator now that its stored as a byte
                    }
                        //determine the proxy type. if none specified set a default value which will be a global setting
                    else {
                        proxyCode = 3; //was never specified, use default
                    }
                    parseBox = fileLine.Split(':');
                    proxyIP = parseBox[0];
                    if (parseBox.Length > 0)
                    {
                        proxyPort = Convert.ToUInt16(parseBox[1]);
                    }
                    else {
                        proxyPort = 1080;
                        //use a default setting here instead if not specified
                    }

                    SL1[arrayIndex].IPAddress = proxyIP;
                    SL1[arrayIndex].Port = proxyPort;
                    SL1[arrayIndex].Type = proxyCode;
                    SL1[arrayIndex].Verified = false;

                    arrayIndex++;
                }
                
            }

            fileReader.Close();
            //close the reader
            
            for (int rra = 0; rra < 10; rra++)
            {
                MessageBox.Show("IP " + SL1[rra].IPAddress + "\nPort: " + SL1[rra].Port + "\nType: " + SL1[rra].Type);
                //just some brief test code
            }
        }

    }



Is This A Good Question/Topic? 0
  • +

Replies To: Object reference not set to an instance of an object in a class array

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3608
  • View blog
  • Posts: 12,399
  • Joined: 12-December 12

Re: Object reference not set to an instance of an object in a class array

Posted 27 November 2013 - 01:30 PM

public static proxyScanlist[] SL1 = new proxyScanlist[File.ReadAllLines("List\\Scanlist_01.txt").Length];

This only creates an array that can store instances of proxyScanlist, it doesn't fill them with new instances of this Class. You have to individually instantiate (using new) each class-instance:
SL1[0] = new proxyScanlist();    // etc.

This post has been edited by andrewsw: 27 November 2013 - 01:31 PM

Was This Post Helpful? 3
  • +
  • -

#3 vpdic  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 48
  • Joined: 24-November 13

Re: Object reference not set to an instance of an object in a class array

Posted 27 November 2013 - 01:34 PM

That solves both my questions. I thought trying to create an instance using the same variable name each time would present a problem but that doesn't seem to be the case. Thank you!

***SOLVED POST 2***
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1