none
Keeping a List of arrays RRS feed

  • Question

  • I'm trying to find out how to properly use List of arrays for scorekeeping.

    This is to keep scores for both teams for each point

    public class Scores
        {
            public int scoreA { get; set; }
            public int scoreB { get; set; }
        }

    This is the class for one match for 2 opponents

    public class Singles
        {
            public bool playerARightEnd { get; set; } // TRUE:'A'=RightEnd; 'B'=LeftEnd;    FALSE:'A'=LeftEnd; 'B'=RightEnd;
            public bool playerAServer { get; set; } // TRUE:'A' serves; FALSE:'B' serves
            public List<Scores> game1;
            public List<Scores> game2;
            public List<Scores> game3;
        }
    

    Here is what I call everytime the score is updated

    private void scoreUpdate()  
        {
            singles.game1.Add(new Scores { scoreA = 0, scoreB = 0 });
        }
    

    I'm getting the following error when the code runs the method scoreUpdate() line : singles.game1.Add() Object reference not set to an instance of an object.

    Could any one help me to properly initialize the game list?

    Thursday, September 26, 2019 9:59 AM

Answers

  • This is the class for one match for 2 opponents

    public class Singles
        {
            public bool playerARightEnd { get; set; } // TRUE:'A'=RightEnd; 'B'=LeftEnd;    FALSE:'A'=LeftEnd; 'B'=RightEnd;
            public bool playerAServer { get; set; } // TRUE:'A' serves; FALSE:'B' serves
            public List<Scores> game1;
            public List<Scores> game2;
            public List<Scores> game3;
        }

    Here is what I call everytime the score is updated

    private void scoreUpdate()  
        {
            singles.game1.Add(new Scores { scoreA = 0, scoreB = 0 });
        }

    I'm getting the following error when the code runs the method scoreUpdate() line : singles.game1.Add() Object reference not set to an instance of an object.

    Try this:

    //public List<Scores> game1;
    public List<Scores> game1 = new List<Scores>();
    

    Do the same for game2 and game3.

    - Wayne

    Thursday, September 26, 2019 10:54 AM

All replies

  • This is the class for one match for 2 opponents

    public class Singles
        {
            public bool playerARightEnd { get; set; } // TRUE:'A'=RightEnd; 'B'=LeftEnd;    FALSE:'A'=LeftEnd; 'B'=RightEnd;
            public bool playerAServer { get; set; } // TRUE:'A' serves; FALSE:'B' serves
            public List<Scores> game1;
            public List<Scores> game2;
            public List<Scores> game3;
        }

    Here is what I call everytime the score is updated

    private void scoreUpdate()  
        {
            singles.game1.Add(new Scores { scoreA = 0, scoreB = 0 });
        }

    I'm getting the following error when the code runs the method scoreUpdate() line : singles.game1.Add() Object reference not set to an instance of an object.

    Try this:

    //public List<Scores> game1;
    public List<Scores> game1 = new List<Scores>();
    

    Do the same for game2 and game3.

    - Wayne

    Thursday, September 26, 2019 10:54 AM
  • Hi Jedimaster100,

    The cause of the error is that game1 is not instantiated, so game1 is null and you should use the keyword new to instantiate game1 first.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, September 27, 2019 5:56 AM
  • Why would you want game1 to be a LIST of scores?  Wouldn't you just want it to hold a single score -- the current score?  What use is the history?  You want to be able to answer the question, "what is the current score?" or "what was the final score?", right?


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Friday, September 27, 2019 6:07 PM
  • Hello Tim.

    Actually no. As I need to later print a scoresheet containing all the parts of the match so I need to know which player(s) scored each point etc...


    Thursday, October 3, 2019 6:51 AM
  • Thank you!!! Works perfectly.
    Thursday, October 3, 2019 6:52 AM