C# Visual Studio窗体上的错误";对象引用未设置为实例等“;
所以现在这是一个使用表单的“游戏”。但是我的对象有一个错误,我不知道我为什么会这样。我有两门课:玩家和武器。我试图将player.Attack()的结果显示为字符串,但当我单击表单上的按钮执行此操作时,会出现可爱的“对象引用未设置为对象实例”错误。我尝试了Convert.ToString(player.Attack());还尝试了String.Format(player.Attack()),甚至设置了一个字符串变量来转换为字符串,但我一直遇到这个错误。我只希望方法的结果在表单标签上显示为字符串 这是玩家级C# Visual Studio窗体上的错误";对象引用未设置为实例等“;,c#,forms,methods,overriding,C#,Forms,Methods,Overriding,所以现在这是一个使用表单的“游戏”。但是我的对象有一个错误,我不知道我为什么会这样。我有两门课:玩家和武器。我试图将player.Attack()的结果显示为字符串,但当我单击表单上的按钮执行此操作时,会出现可爱的“对象引用未设置为对象实例”错误。我尝试了Convert.ToString(player.Attack());还尝试了String.Format(player.Attack()),甚至设置了一个字符串变量来转换为字符串,但我一直遇到这个错误。我只希望方法的结果在表单标签上显示为字符串
public class Player
{
private Form1 Login { get; set; }
private Weapon sword;
public string PlayerName { get; set; }
public int Health { get; set; } = 36;
public int Strength { get; set; } = 6;
public Player(string playername, int health, int strength, Weapon s)
{
this.Login.UserName = playername; //gets character name from form1 and makes it the username throughout the game.
// this.PlayerName = playername;
this.Health = health;
this.Strength = strength;
this.sword = s;
}
public int Attack()
{
return sword.Attack(this.Strength);
}
这是武器级
public class Weapon
{
public string Name { get; set; } = "Hel Fire";
public int AtkDmg { get; set; } = 4;
public Weapon(string name, int attack)
{
this.Name = name;
this.AtkDmg = attack;
}
//Attack function that will take users STR and Sword Strength
public int Attack(int strength)
{
int result = strength + this.AtkDmg; // AtkDmg is the weapons Strength
return result;
}
这是按钮单击对象所在的窗体
private void AtkSword_Click(object sender, EventArgs e)
{
int GoblinHealth = 36;
int GoblinAttack = 2;
if (GoblinHealth != 0)
{
GoblinHealth -= player.Attack();
player.Health -= GoblinAttack;
}
//below should display text and results from attack
ResultsLabel.Text = Format.ToString("{0} Attacks the goblin. The goblin is at {1} HP. \n {2} is at {3} HP", player.PlayerName, GoblinHealth, player.PlayerName, player.Health);
}
我认为问题在于:
ResultsLabel.Text = Format.ToString("{0} Attacks the goblin. The goblin is at {1} HP. \n {2} is at {3} HP", **player.PlayerName**, GoblinHealth, **player.PlayerName**, player.Health);
您正在设置变量this.Login.UserName,而不是Player.Playername。
稍后您尝试访问它,但它不存在。
您应该设置:
public Player(string playername, int health, int strength, Weapon s)
{
this.Playername = playername //here
this.Health = health;
this.Strength = strength;
this.sword = s;
}
或者使用登录用户名
ResultsLabel.Text = Format.ToString("{0} Attacks the goblin. The goblin is at {1} HP. \n {2} is at {3} HP", this.Login.UserName, GoblinHealth, this.Login.UserName, player.Health);
您是否将按钮点击选项中的
player
变量分配给玩家,以及玩家类中的swool
变量?你在哪几行出错?你调试代码了吗?您是否检查了player
是否为空?@ChetanRanpariya player不显示为空。我确实在这里调试了它,这是我得到的错误:System.NullReferenceException HResult=0x80004003 Message=Object reference未设置为对象的实例。Source=DungeonCrawler2 StackTrace:at DungeonCrawler2.Encounter1.AtkSword_Click(objectsender,EventArgs e)@ChiliPenguin我在表单中指定玩家为公共玩家。但是我没有在AtkSword_点击按钮下做,如果我应该在那里做的话?它还说第34行我得到了我的player.Attack()所在的错误。调试你的代码。跨过它。如果你不知道怎么做或者觉得很无聊,在你弄明白之前不要再写一行代码,这很容易做到。我有这个.Login.Username,因为我的第一个表单叫做“Login”,我从那里获取用户输入并将其分配给playername。