Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对象引用未设置为对象的实例 public播放器(字符串名称、列表卡) { this.name=名称; 尝试 { this.stack.insertCards(cards);//这是NullReferenceExection } 捕获(例外e) { 投掷e; } } 公共作废插入卡(列表卡) { stack.AddRange(卡片); } public List GetSevenCards()//玩家通过此函数获取卡 { 列表=新列表(); int-ran; 随机r=新随机(); 对于(int i=0;i_C# - Fatal编程技术网

C# 对象引用未设置为对象的实例 public播放器(字符串名称、列表卡) { this.name=名称; 尝试 { this.stack.insertCards(cards);//这是NullReferenceExection } 捕获(例外e) { 投掷e; } } 公共作废插入卡(列表卡) { stack.AddRange(卡片); } public List GetSevenCards()//玩家通过此函数获取卡 { 列表=新列表(); int-ran; 随机r=新随机(); 对于(int i=0;i

C# 对象引用未设置为对象的实例 public播放器(字符串名称、列表卡) { this.name=名称; 尝试 { this.stack.insertCards(cards);//这是NullReferenceExection } 捕获(例外e) { 投掷e; } } 公共作废插入卡(列表卡) { stack.AddRange(卡片); } public List GetSevenCards()//玩家通过此函数获取卡 { 列表=新列表(); int-ran; 随机r=新随机(); 对于(int i=0;i,c#,C#,堆栈获得一个包含7张卡的卡片列表应该是: public Player(string name, List<Card> cards) { this.name = name; try { this.stack.insertCards(cards);//Here is the NullReferenceExeption } catch (Exception e) { throw e; } }

堆栈获得一个包含7张卡的卡片列表

应该是:

public Player(string name, List<Card> cards)
{
   this.name = name;
   try
   {
      this.stack.insertCards(cards);//Here is the NullReferenceExeption
   }
   catch (Exception e)
   {                
      throw e;
   }
}

public void insertCards(List<Card> cards)
{
   stack.AddRange(cards);
}

public List<Card> GetSevenCards() //the Player gets the Cards from this function
{
   List<Card> list = new List<Card>();
   int ran;
   Random r = new Random();
   for (int i = 0; i < 7; i++)
   {
      ran = r.Next(0, stack.Count()-1);
      list.Add(stack[ran]);
      stack.RemoveAt(ran);
   }
   return list;
}
并创建堆栈的新实例

因为卡片是空的。首先用函数填充它们

不能说更多的东西,因为您没有提供更多关于定义的信息。应该有更多的代码。

应该是:

public Player(string name, List<Card> cards)
{
   this.name = name;
   try
   {
      this.stack.insertCards(cards);//Here is the NullReferenceExeption
   }
   catch (Exception e)
   {                
      throw e;
   }
}

public void insertCards(List<Card> cards)
{
   stack.AddRange(cards);
}

public List<Card> GetSevenCards() //the Player gets the Cards from this function
{
   List<Card> list = new List<Card>();
   int ran;
   Random r = new Random();
   for (int i = 0; i < 7; i++)
   {
      ran = r.Next(0, stack.Count()-1);
      list.Add(stack[ran]);
      stack.RemoveAt(ran);
   }
   return list;
}
并创建堆栈的新实例

因为卡片是空的。首先用函数填充它们


不能说更多的东西,因为您没有提供更多关于定义的信息。应该有更多的代码。

我没有看到堆栈被初始化。必须先初始化堆栈,然后才能对其调用任何非静态方法

this.stack.insertCards(GetSevenCards());

我没有看到堆栈被初始化。必须先初始化堆栈,然后才能对其调用任何非静态方法

this.stack.insertCards(GetSevenCards());

您可能需要确保
堆栈
在类内实例化:

var stack = new Stack();

您可能需要确保
堆栈
在类内实例化:

var stack = new Stack();

请发布一个简短但完整的程序来演示问题;否则我们只是猜测。请参阅发布一个简短但完整的程序来演示问题;否则我们只是猜测。见附件-1。这不是真正的“堆栈”,因为.net堆栈没有他定义的.insertCards方法。他自己写的。这不是真正的“堆栈”,因为.net堆栈没有他定义的.insertCards方法。他自己写的。谢谢。。。抱歉问了这么愚蠢的问题谢谢。。。很抱歉问了这个愚蠢的问题