Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Problem with List<>

Problem with List<>

Scheduled Pinned Locked Moved .NET (Core and Framework)
helpcsharpdotnetdata-structuresquestion
4 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    polomint
    wrote on last edited by
    #1

    Hi, I have a problem with VS2010beta2 and .NET Framework Client Profile, although I would expect the problem to be something to do with my inexperienced coding. I have this block of code..

        struct sGamePort
        {
            public string sgpProtocol;
            public UInt16 sgpPortStart;
            public UInt16 sgpPortEnd;
        }
    
        struct sGamePortItem
        {
            public string sgpName;
            public List<sGamePort> sgpPortList;
        }
    
        List<sGamePortItem> sGames;
    
    
        private sGamePort ParsePorts(string\[\] line)
        {
            sGamePort gp = new sGamePort();
            gp.sgpProtocol = line\[0\];
            int num = line.Count();
            if (num == 2)
            {
                // single port opened...
                gp.sgpPortStart =  Convert.ToUInt16(line\[1\]);
                gp.sgpPortEnd = Convert.ToUInt16(line\[1\]);
            }
            if (num == 3)
            {
                // port range opened...
                gp.sgpPortStart = Convert.ToUInt16(line\[1\]);
                gp.sgpPortEnd = Convert.ToUInt16(line\[2\]);
            }
            return gp;
        }
    

    Where line is a string array which essentially holds some tokens. ie. line[0] = "NAME" line[1] = "JOHN" line[2] = "YOUNG" Now, in a seperate function I have

    sGamePortItem pi = new sGamePortItem();
    pi.sgpProtocol = "TCP";
    pi.sgpPortList.Add(ParsePorts(strCMD));

    This gives me a Null Reference Exception, so I changed it to this...

    sGamePortItem pi = new sGamePortItem();
    pi.sgpProtocol = "TCP";
    sGamePort test = new sGamePort();
    test = ParsePorts(strCMD);
    pi.sgpPortList.Add(test); // ERROR HERE

    Now, the error only occurs on the specified line above.. What am I doing wrong? Is it to do with me using the new keyword incorrectly? Or do I not use 'new'? Any help would be appreciated as this has been confusing me for a few hours now, lol Thanks again. John

    Yippee Kai Yai Signed Polomint.......

    V P 2 Replies Last reply
    0
    • P polomint

      Hi, I have a problem with VS2010beta2 and .NET Framework Client Profile, although I would expect the problem to be something to do with my inexperienced coding. I have this block of code..

          struct sGamePort
          {
              public string sgpProtocol;
              public UInt16 sgpPortStart;
              public UInt16 sgpPortEnd;
          }
      
          struct sGamePortItem
          {
              public string sgpName;
              public List<sGamePort> sgpPortList;
          }
      
          List<sGamePortItem> sGames;
      
      
          private sGamePort ParsePorts(string\[\] line)
          {
              sGamePort gp = new sGamePort();
              gp.sgpProtocol = line\[0\];
              int num = line.Count();
              if (num == 2)
              {
                  // single port opened...
                  gp.sgpPortStart =  Convert.ToUInt16(line\[1\]);
                  gp.sgpPortEnd = Convert.ToUInt16(line\[1\]);
              }
              if (num == 3)
              {
                  // port range opened...
                  gp.sgpPortStart = Convert.ToUInt16(line\[1\]);
                  gp.sgpPortEnd = Convert.ToUInt16(line\[2\]);
              }
              return gp;
          }
      

      Where line is a string array which essentially holds some tokens. ie. line[0] = "NAME" line[1] = "JOHN" line[2] = "YOUNG" Now, in a seperate function I have

      sGamePortItem pi = new sGamePortItem();
      pi.sgpProtocol = "TCP";
      pi.sgpPortList.Add(ParsePorts(strCMD));

      This gives me a Null Reference Exception, so I changed it to this...

      sGamePortItem pi = new sGamePortItem();
      pi.sgpProtocol = "TCP";
      sGamePort test = new sGamePort();
      test = ParsePorts(strCMD);
      pi.sgpPortList.Add(test); // ERROR HERE

      Now, the error only occurs on the specified line above.. What am I doing wrong? Is it to do with me using the new keyword incorrectly? Or do I not use 'new'? Any help would be appreciated as this has been confusing me for a few hours now, lol Thanks again. John

      Yippee Kai Yai Signed Polomint.......

      V Offline
      V Offline
      vtchris peterson
      wrote on last edited by
      #2

      The problem is that your line:

      public List<sGamePort> sgpPortList;

      should be:

      public List<sGamePort> sgpPortList = new List<sGamePort>();

      Need to assign a target to sgpPortList before you dereference it, hence the null ref exception.

      P 1 Reply Last reply
      0
      • V vtchris peterson

        The problem is that your line:

        public List<sGamePort> sgpPortList;

        should be:

        public List<sGamePort> sgpPortList = new List<sGamePort>();

        Need to assign a target to sgpPortList before you dereference it, hence the null ref exception.

        P Offline
        P Offline
        polomint
        wrote on last edited by
        #3

        Damn, I knew it would be something trivial... Thanks for waking me up to a silly mistake, :-D :-D :-D EDIT: Now I'm getting the error "cannot have instance field initializers in structs"... Hmmm, this could be a pain... EDIT AGAIN: I realised what was wrong.. I hadn't been initialising the sGamePortItem in the list..

        sGamePortItem pi = new sGamePortItem();
        pi.sgpPortList = new List<sGamePort>(); // THIS WAS NEEDED.

        Thank you for your help, it made me think more about the initialisation...

        Yippee Kai Yai Signed Polomint.......

        modified on Thursday, December 3, 2009 2:09 PM

        1 Reply Last reply
        0
        • P polomint

          Hi, I have a problem with VS2010beta2 and .NET Framework Client Profile, although I would expect the problem to be something to do with my inexperienced coding. I have this block of code..

              struct sGamePort
              {
                  public string sgpProtocol;
                  public UInt16 sgpPortStart;
                  public UInt16 sgpPortEnd;
              }
          
              struct sGamePortItem
              {
                  public string sgpName;
                  public List<sGamePort> sgpPortList;
              }
          
              List<sGamePortItem> sGames;
          
          
              private sGamePort ParsePorts(string\[\] line)
              {
                  sGamePort gp = new sGamePort();
                  gp.sgpProtocol = line\[0\];
                  int num = line.Count();
                  if (num == 2)
                  {
                      // single port opened...
                      gp.sgpPortStart =  Convert.ToUInt16(line\[1\]);
                      gp.sgpPortEnd = Convert.ToUInt16(line\[1\]);
                  }
                  if (num == 3)
                  {
                      // port range opened...
                      gp.sgpPortStart = Convert.ToUInt16(line\[1\]);
                      gp.sgpPortEnd = Convert.ToUInt16(line\[2\]);
                  }
                  return gp;
              }
          

          Where line is a string array which essentially holds some tokens. ie. line[0] = "NAME" line[1] = "JOHN" line[2] = "YOUNG" Now, in a seperate function I have

          sGamePortItem pi = new sGamePortItem();
          pi.sgpProtocol = "TCP";
          pi.sgpPortList.Add(ParsePorts(strCMD));

          This gives me a Null Reference Exception, so I changed it to this...

          sGamePortItem pi = new sGamePortItem();
          pi.sgpProtocol = "TCP";
          sGamePort test = new sGamePort();
          test = ParsePorts(strCMD);
          pi.sgpPortList.Add(test); // ERROR HERE

          Now, the error only occurs on the specified line above.. What am I doing wrong? Is it to do with me using the new keyword incorrectly? Or do I not use 'new'? Any help would be appreciated as this has been confusing me for a few hours now, lol Thanks again. John

          Yippee Kai Yai Signed Polomint.......

          P Offline
          P Offline
          puri keemti
          wrote on last edited by
          #4

          You need to initialize the list of the struct "sGamePortItem" before assigning the object.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups