class bank
{
    int nUserNum = 3;
    user[] userGroup;  // 作为类成员    public bank() // 在构造函数中对userGroup进行初始化
    {
        userGroup = new user[nUserNum];
        for (int i = 0; i < nUserNum; i++)
        {
            userGroup[i] = new user();
        }        userGroup[0].strAccount = "0001";
        userGroup[0].strUserName = "富二代";
        userGroup[0].strPassWord = "1234";
        userGroup[0].nMoney = 20000;        userGroup[1].strAccount = "0002";
        userGroup[1].strUserName = "技术宅";
        userGroup[1].strPassWord = "1234";
        userGroup[1].nMoney = 70000;        userGroup[2].strAccount = "0003";
        userGroup[2].strUserName = "老板";
        userGroup[2].strPassWord = "1234";
        userGroup[2].nMoney = 90000;
    }    public string UserLogin(string nCard)
    {
        // 使用已经初始化过的userGroup
        for (int i = 0; i < nUserNum; i++)
        {
            if (userGroup[i].strAccount == nCard)
            {
                return nCard;
            }
        }
        return null;
    }
    ...

解决方案 »

  1.   

    看下面的代码
    user[] userGroup = new user[5];
    这只是定义了一个指针,指向了个表 .这个表可容纳5个user如下,标号从0开始.
    user0
    user1
    user2
    user3
    user4
    注意,只是可以容纳,不代表里面就有五个user.为NULLReferrence的原因就是
    你只是声明了一个可以纳5个user的表.但实际上表是空的.按照上楼的代码再new出来几个用户放进去,就不
    会报"空引用了"
      

  2.   

    除了1楼指出的错误,在方法InMoney()中也有错误,里面竟然再次实例化一个bank...        public class bank
            {
                private int nUserNum;
                private user[] userGroup;            public bank()
                {
                    nUserNum = 3;
                    userGroup = new user[nUserNum];
                }            public void GetUsers()
                {
                    for (int i = 0; i < nUserNum; i++) {
                        userGroup[i] = new user();
                    }                userGroup[0].strAccount = "0001";
                    userGroup[0].strUserName = "富二代";
                    userGroup[0].strPassWord = "1234";
                    userGroup[0].nMoney = 20000;                userGroup[1].strAccount = "0002";
                    userGroup[1].strUserName = "技术宅";
                    userGroup[1].strPassWord = "1234";
                    userGroup[1].nMoney = 70000;                userGroup[2].strAccount = "0003";
                    userGroup[2].strUserName = "老板";
                    userGroup[2].strPassWord = "1234";
                    userGroup[2].nMoney = 90000;
                }            public string UserLogin(string nCard)
                {
                    for (int i = 0; i < nUserNum; i++) {
                        if (userGroup[i].strAccount == nCard) {
                            return nCard;
                        }
                    }
                    return null;
                }            public void InMoney()
                {
                    Console.WriteLine("请输入您的卡号:");
                    string nCard = Console.ReadLine();
                    string bIsLogin = this.UserLogin(nCard);
                    if (bIsLogin != null) {
                        Console.WriteLine("登陆成功!");
                    } else {
                        error(2);
                    }
                }            public void error(int nErrorInfo)
                {
                    if (nErrorInfo == 1) {
                        Console.WriteLine("您输入的参数有误!");
                    } else if (nErrorInfo == 2) {
                        Console.WriteLine("账号输入有误!");
                    }
                }
            }
      

  3.   


    试了一下。。 依然是相同的提示。 看看你是在那调用UserLogin()这个方法的?
      

  4.   

     参考代码
     class Program {
            static void Main(string[] args) {
                User[] users = new User[5]; //new 了一个可容纳5个user的表
              for(int i=0;i<users.Length; i++)
                     users[i]= new User { Name = i.ToString() }; //分别new出来5User个
            }
            class User { 
                public string Name{get;set;}
            }
    }
      

  5.   

    我操,刚才太大意了目测了一下,就以为找到错误,其实没找到,里面有好几处错误,分别在
    52,85,108行
    下面是修改后的代码
    using System;
    using System.Collections.Generic;
    using System.Text;
    namespace BankSystem {
        class Program {
            static void Main(string[] args) {
                bank Obj_Bank = new bank();
                Obj_Bank.GetUsers();
                while (true) {
                    Console.WriteLine("============ 欢迎使用有敌存取款系统 ============");
                    Console.WriteLine("\t1.存款  2.取款  3.转账  4.查询余额  5.退出");
                    Console.WriteLine("================================================");
                    string strMenu = Console.ReadLine(); switch (strMenu) {
                        case "1":
                            Obj_Bank.InMoney(); break;
                        default: Obj_Bank.error(1); break;
                    }
                }
            }
        }
        class bank {         /*         * 定义用户         */
            int nUserNum = 3;
            user[] userGroup;
            public void GetUsers() {
                userGroup = new user[nUserNum];
                for (int i = 0; i < nUserNum; i++) { userGroup[i] = new user(); }
                userGroup[0].strAccount = "0001"; userGroup[0].strUserName = "富二代";
                userGroup[0].strPassWord = "1234"; userGroup[0].nMoney = 20000;
                userGroup[1].strAccount = "0002"; userGroup[1].strUserName = "技术宅";
                userGroup[1].strPassWord = "1234"; userGroup[1].nMoney = 70000;
                userGroup[2].strAccount = "0003"; userGroup[2].strUserName = "老板";
                userGroup[2].strPassWord = "1234"; userGroup[2].nMoney = 90000;
            }
            /*         * 检测用户         */
            public string UserLogin(string nCard) {
                //user[] userGroup = new user[nUserNum];
                for (int i = 0; i < nUserNum; i++) 
                { if (userGroup[i].strAccount == nCard) { return nCard; } } return null;
            }
            /*         * 存款         */
            public void InMoney() {
             //  bank Obj_Bank = new bank();
                Console.WriteLine("请输入您的卡号:"); string nCard = Console.ReadLine();
                string bIsLogin = this.UserLogin(nCard);
                if (bIsLogin != null) { Console.WriteLine("登陆成功!"); } else { error(2); }
            }
            /*         * 错误提示         */
            public void error(int nErrorInfo) {
                if (nErrorInfo == 1) { Console.WriteLine("您输入的参数有误!"); } else if (nErrorInfo == 2) { Console.WriteLine("账号输入有误!"); }
            }
        }
        class user { public string strAccount; public string strUserName; public string strPassWord; public double nMoney;     }
    }