radimitrov / CSharpShellApp

77 stars 18 forks source link

HashSet Method #382

Open LucasFerreiraWF opened 3 weeks ago

LucasFerreiraWF commented 3 weeks ago

When trying to use the "bool HashSet.Add(T item" method. An error is thrown. I have already checked everything that could be causing this in my code and found nothing. I even tried passing the code to Visual Studio on my friend's computer and it worked perfectly there. This is the error:

System.NullReferenceException: Object reference not set to an instance of an object at xadrez.PartidaXadrez.colocarNovaPeca(Char coluna, Int32 linha, Peca peca) in xadrez/PartidaXadrez.cs:97 [97:8-97:24] at xadrez.PartidaXadrez.colocarPecas() in xadrez/PartidaXadrez.cs:102 [102:9-102:61] at xadrez.PartidaXadrez..ctor() in xadrez/PartidaXadrez.cs:26 [26:3-26:18] at FuckChess.Program.Main() in Program.cs:15 [15:4-15:48] The "pecas" set is correctly instantiated

radimitrov commented 3 weeks ago

Can't be that unless you are somehow setting your HashSet list object to null. Look for something else on that line.

This code tests HashSet.Add with bool and will run without an Exception:

var set = new HashSet<bool>();
        var set2 = new HashSet<bool?>(); //Nullable bool
        bool a=false;
     bool b = true;
        bool? c = null;
        set.Add(a);
        set.Add(b); 
        //set.Add(c); //Won't compile since bool is a value type and only reference can be set to null by default

        set2.Add(a);
        set2.Add(b);
        set2.Add(c); //Works 

        foreach(var val in set) Console.WriteLine(val);
        Console.WriteLine();
        foreach(var val in set2) Console.WriteLine(val == null ? "null" : val);     

The only other possibility is that you are using a Windows specific API