Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6849325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:58:38+00:00 2026-05-27T00:58:38+00:00

OOP beginner here and my problem is when i get to this event :

  • 0

OOP beginner here and my problem is when i get to this event :

    private void button1_Click(object sender, EventArgs e)
    {
        progressBar2.Increment(-25);
        splitContainer1.Panel1.Enabled = false;
        Thread.Sleep(2100);
        int preAttack = myCurrentOpponent.SetAttack();
        oppAttack = myCurrentOpponent.Attack(preAttack);
        progressBar1.Increment(oppAttack.dommage);
        textBox1.Text += myCurrentOpponent.Name + "Used " + oppAttack.AttackName;

    }

I get an error : Object reference not set to an instance of an object.
Things you need to know myCurrentOpponent is an object chosen randomly in a collection of 3 monsters type. I’ll use Gollum for this example wich is one of the 3 types. Gollum inherit from Monster and overrides Name, Level, Health, PreAttack and SetAttack from is base.

    public abstract class Monster
{
    public abstract string Name { get; }
    public abstract int Lv { get; }
    public abstract int Hp { get; }

    public abstract int SetAttack();
    public abstract Attack Attack(int x);


    public Monster()
    {
    }
}

Nothing in here is set because of this :

    public class Gollum : Monster
{
    Attack[] myAttack = new Attack[4];

    public override string Name
    {
        get
        {
            return "Gollum";
        }

    }

    public override int Hp
    {
        get
        {
            return 100;
        }
    }
    public override int Lv
    {
        get
        {
            return 3;
        }
    }

    public override int SetAttack()
    {

        Random randnum = new Random();


        int x = randnum.Next(5);

        myAttack[0].AttackName = "Charge";
        myAttack[0].dommage = -10;
        myAttack[1].AttackName = "Scratch";
        myAttack[1].dommage = -12;
        myAttack[2].AttackName = "Throw Rock";
        myAttack[2].dommage = -15;
        myAttack[3].AttackName = "Strangle";
        myAttack[3].dommage = -16;
        return x;
    }
    public override Attack Attack(int x)
    {
        return myAttack[x];
    }

    public Gollum()
    {
    }

Everything is set in the 3 monster sub-classes. Now, let’s say the chosen monster is Gollum and I click button 1, as soon as the compiler reaches this line…

     myAttack[0].AttackName = "Charge";

… called from this line…

    int preAttack = myCurrentOpponent.SetAttack();

…I get the error.
Am i missing a constructor for my Gollum Class ? And if I am how can i code it
since everything is set directly in the property.
I really hope someone can help me on this on because I really don’t know where to look.
Hope this is clear enough and don’t hesitate to ask if you need more code lines.
Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:58:39+00:00Added an answer on May 27, 2026 at 12:58 am

    You haven’t filled the arrat myAttack with any objects. When you initialize the array, it is created with null references. You need to add these lines before the line you get the error on:

    myAttack[0]=new Attack();
    myAttack[1]=new Attack();
    myAttack[2]=new Attack();
    myAttack[3]=new Attack();
    

    This initializes each array entry with a new Attack object so that you can now set properties on them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my background: Decent experience with PHP/MySql. Beginner's experience with OOP Why I want
As a beginner/intermediate developer one problem I run into as my projects get bigger
c#/oop noob here. I am creating a datatable from the schema of a sql
Ok this is a really weird problem. I wanna start off by saying that
this is a simple OOP QT question. my app consists of main window (QMainWindow)
I am currently a beginner in PHP OOP programming and was wondering if someone
I'm just getting started with OOP PHP with PHP Object-Oriented Solutions by David Powers,
OOP usually requires instantiation (creating an instance of class before using) like this: var
I am beginner in C# programming. I have encountered small problem during building application
I was explaining OOP to my friend. I was unable to answer this question.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.