c# - Filling the data variables in arrays of objects -


class customer {  public string name;  public sting nic;  public int age;   public void add_customer()     {         // code here assign values data types     } }  class main_menu {    customer[] cust = new customer[100];    // other data members     public void new_customer()    {       // console.writeline pritings       cust[0].add_customer();       // ------>> here in line error arrising says       unhandled exception of type 'system.nullreferenceexception' occurred in   assignment 2.exe        additional information: object reference not set instance of object.      } } 

now want fill data variables in arrays of objects 1 one in customers instances

kindly me because beginner

cust[0] null trying access 1 of properties or methods before assigning value cause exception.

you main misunderstanding - initializing cust didn't initialized 1 of objects in ( cust[i] null every i).

you need validate before using it:

class main_menu {    customer[] cust = new customer[100];    // other data members     public void new_customer()    {     cust[0] = new customer();        // when want use later on, validation.     if (cust[0] != null)     {               cust[0].add_customer();     }    } } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

html - Repeat image to extend header to fill screen -

javascript - Backbone.js getting target attribute -