Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using System;
namespace po {
  public class small {
    public string nem;
    public bool av = true;
    public void set_nem() {
      Console.WriteLine("enter name smaller");
      nem = Console.ReadLine();
    }
  }
  public class normal {
    public string nem;
    small[] _small = new small[10];
    public void add_small() {
      for (int i = 0; i < 10; i++)
        _small[i] = new small();

      for (int i = 0; i < 10; i++) {
        if (_small[i].av == true){
          _small[i].set_nem();
          i = 11;
          }
      }

    }
    public void set_nem() {
      Console.WriteLine("enter name normal");
      nem = Console.ReadLine();
    }
  }

  public class big {
    normal[] _normal = new normal[10];
    public void setup_big() {
      for (int i = 0; i < 10; i++)
        _normal[i] = new normal();
    }
  }

  class MainClass {
    public static void Main(string[] args) {
      var _big = new big();
      big._normal[0]._small[0].set_nem();

    }
  }

}


What I have tried:

the code explains itself, I have 3 classes : big, normal and small. I have an array of 10 normal in big and 10 small in each normal (so in total I have 100 small) well,the problem is that there are some protection Issues ,this is the error that I get when I compile:


9): error CS0120: An object reference is required to access non-static member `po.big._normal'
Posted
Updated 5-Jan-21 16:41pm

1 solution

Your
var _big = new big();
      big._normal[0]._small[0].set_nem();

better be
var _big = new big();
      _big._normal[0]._small[0].set_nem();


hint: the compiler normally gives a (filename and) linenumber with each info/warning/error it emits; use that to your advantage!

:)
 
Share this answer
 
Comments
Maciej Los 6-Jan-21 6:00am    
5ed!
thatraja 6-Jan-21 8:21am    
5! It took me couple of seconds to see the difference, I missed at first.
Einar Kreiger 6-Jan-21 10:15am    
using System;
namespace po {
public class small {
public string nem;
public bool av = true;
public void set_nem() {
Console.WriteLine("enter name smaller");
nem = Console.ReadLine();
}
}
public class normal {
public string nem;
public small[] _small = new small[10];
public void add_small() {
for (int i = 0; i < 10; i++)
_small[i] = new small();

for (int i = 0; i < 10; i++) {
if (_small[i].av == true){
_small[i].set_nem();
i = 11;
}
}

}
public void set_nem() {
Console.WriteLine("enter name normal");
nem = Console.ReadLine();
}
}

public class big {
public normal[] _normal = new normal[10];
public void setup_big() {
for (int i = 0; i < 10; i++)
_normal[i] = new normal();
}
}

class MainClass {
public static void Main(string[] args) {
var _big = new big();
_big._normal[0]._small[0].set_nem();

}
}

}


this is the code with _big._normal[0]..... and I get this error now, there is no line

Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
at po.MainClass.Main (System.String[] args) [0x00006] in <8b4fdd67dd6a460e82f4b95ccaf411c3>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
at po.MainClass.Main (System.String[] args) [0x00006] in <8b4fdd67dd6a460e82f4b95ccaf411c3>:0
Luc Pattyn 6-Jan-21 12:11pm    
Your instance of big class holds an array of type normal, whoch gets dimensioned to 10 (i.e. 10 times a null pointer) but not initialized to real objects as you did not call setup_big() at all.

You must either call setup_big() inside your Main method, or better call it inside a constructor of the big class.

:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900