-3

So this is a "game" using forms for now. But I am getting an error with my object and I'm not sure exactly why I am. I have 2 classes below : Player & Weapon. I'm trying to get the result of player.Attack() to show as a string, but when I click the button on the form to do so, it gives me the lovely "object reference not set to an instance of an object" error. I've tried Convert.ToString(player.Attack()); also tried String.Format(player.Attack()), even set a string variable to take the conversion to a string, but i keep getting this error. I just want the result of the method to show as a string on my form's label.

This is the PLAYER class

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);
        }

This is the WEAPON class

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;
        }

This is the forms where the button_click object is.

 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);
            }
  • Are you assigning the `player` variable in the button_click option to the player, and the `sword` variable in the player class? What lines are you getting errors on? – ChilliPenguin Nov 02 '20 at 00:18
  • 1
    Did you debug the code? Did you check if `player` is null or not? – Chetan Nov 02 '20 at 00:18
  • @ChetanRanpariya player does not show as null. I did debug it at this is the error i'm getting: System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object. Source=DungeonCrawler2 StackTrace: at DungeonCrawler2.Encounter1.AtkSword_Click(Object sender, EventArgs e) – BlizzyBlake278 Nov 02 '20 at 00:29
  • @ChiliPenguin I assigned player in the form itself as public Player player. but i didn't do it under the AtkSword_Click button if I'm supposed to do it there instead? Also it says line 34 I'm getting the error on which is where my player.Attack() is at. – BlizzyBlake278 Nov 02 '20 at 00:29
  • Debug your code. Step through it. If you don't know how or find it tedious, don't write another line of code until you figure that out and it is easy to do. – Señor CMasMas Nov 02 '20 at 00:30
  • As @SeñorCMasMas said, try debugging it step by step. Can you also provide how you create and add information to your player and sword, as this is probably where the issue would lie at. – ChilliPenguin Nov 02 '20 at 00:42
  • @ChilliPenguin right now the only info i have is what i posted here. the only other difference being on my 1st form called Login, i get the UserName from a textbox there. which is why I have private Form login above so i can access that info and assign it to playername. I should also mention that I'm still somewhat of a newbie. I'm trying to create a "dungeon crawler" using forms right now. but i am having trouble figuring out how to access classes and info between forms. so that may be part of my problem – BlizzyBlake278 Nov 02 '20 at 01:52
  • @MickyD I'll try it. i've gone through the code for the last 2 days. but i'll try it again with that link you sent and see if there's anything i missed. – BlizzyBlake278 Nov 02 '20 at 02:03

1 Answers1

0

I think that the problem lays here:

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);

You are setting the variable this.Login.UserName instead of Player.Playername. An later you try to access it and it does not exist. You should set:

public Player(string playername, int health, int strength, Weapon s)
{
     this.Playername = playername //here
     this.Health = health;
     this.Strength = strength;
     this.sword = s;
}

or use the login username

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);
Rod Ramírez
  • 1,138
  • 11
  • 22
  • i have this.Login.Username because my 1st form is called "Login" and i'm getting the user input from there and assigning it to the playername. – BlizzyBlake278 Nov 02 '20 at 01:47