0

I do not understand eventhou I can show the values by console.writeline but it throws null exception error.

   class Collection{

    static string[] user_array;
    static string[] user_password;

    static string username;
    static string password;

    public void register(){
            Console.WriteLine("Enter member's username");
            username = Console.ReadLine();

            Console.WriteLine("Enter member's password?(4 digits)");
            password = Console.ReadLine();
    }

    public string[] getUsername() {

        for (int i = 0; i < counter; i++) {
            user_array[i] = Member.Username[i]; //////I get null exception
        }

        return user_array;

    }  

    public string[] getUserpassword() {

        for (int i = 0; i < counter; i++)
        {
            user_array[i] = Member.Password[i]; //////I get null exception
        }
        return user_password;
    }


    public void showMember()
    {
        for (int i = 0; i < counter; i++) {

            Console.WriteLine("");
            Console.WriteLine(Member.Username[i]);
            Console.WriteLine(Member.Password[i]);
            Console.WriteLine(Member.Phonenum[i]);


        }
     }
    }

Even though I can show values of Member.username[i] and Member.password[i] using console.writeline, I keep getting null exception when I try to store those value into a new array And this is my Member class

 class Member{


    static public string[] FirstName = new string[100];
    static public string[] LastName = new string[100];
    static public string[] Address = new string[100];
    static public string[] Phonenum = new string[100];
    static public string[] Password = new string[100];
    static public string[] Username = new string[100];
    static public int[] Borrowedmovie = new int[100];



    public Member(){

    }

 }
  • 3
    You're never assigning a new array to the `user_array` variable - so you're trying to set element `i` of `null`. – C.Evenhuis May 08 '20 at 13:09

1 Answers1

1

You have to initialize user_array & user_password like below.

As you didn't initialize these variables it is throwing exception not Member.username[i] or Member.password[i].

static string[] user_array = new string[100];
static string[] user_password = new string[100];
Karan
  • 12,059
  • 3
  • 24
  • 40