Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用抛出捕获OOWPF进行简单修复_C#_Wpf_Oop_Try Catch - Fatal编程技术网

C# 使用抛出捕获OOWPF进行简单修复

C# 使用抛出捕获OOWPF进行简单修复,c#,wpf,oop,try-catch,C#,Wpf,Oop,Try Catch,形势 我正在开发一个OO WPF应用程序,该应用程序允许不同类型的成员加入竞赛,但我遇到了一个try-catch问题 问题 用外行的话来说,我的问题是,我试图让一名会员加入一场具有以下代码的比赛: race.joinJunior((JuniorMember)hillracing.searchMembers(Username)); 因此,JuniorMember(谁的对象是通过searchMember方法和预先存在的用户名参数找到的)将通过joinJunior方法作为初级成员加入比赛 这段代码本

形势

我正在开发一个OO WPF应用程序,该应用程序允许不同类型的成员加入竞赛,但我遇到了一个try-catch问题

问题

用外行的话来说,我的问题是,我试图让一名会员加入一场具有以下代码的比赛:

race.joinJunior((JuniorMember)hillracing.searchMembers(Username));
因此,JuniorMember(谁的对象是通过searchMember方法和预先存在的用户名参数找到的)将通过joinJunior方法作为初级成员加入比赛

这段代码本身不是问题所在,但它确实导致了问题,因为它调用了joinJunior方法,而我的问题就在这里

public override void joinJunior(JuniorMember jm)
        {
            if (junior != null)
            {
                //Increment the current members on the race by 1.
                currentRunners++;

                if (currentRunners > limitRace)
                {

                    currentRunners = limitRace;

                    throw new Exception(junior.FirstName + " would be this races: " + limitRace + 1 + "th runner. This race can only take: " + limitRace);


                }
            }
            //if the members type doesnt equal the member join permission then they shouldn't be able to join.
            else if (junior.memType != permRace)
            {
                //throws an exception if the member doesn't have the correct type junior
                throw new Exception(senior.FirstName + " does not meet the requirements to join this race, must be type: " + permRace);
            }
            //if the members gender isn't equal to the races gender requirement they shouldn't be able to join that race.
            else if (junior.gender != genRace)
            {
                //throws an exception if the member doesn't have the correct gender type.
                throw new Exception(junior.FirstName + " does not meet the requirements to join this race, must be a: " + genRace);
            }

            else
            {
                //if all other conditions are met, and the member meets requirements, let the member join the race and add a return date of 21 days.
                junior = jm;

                returnDate = DateTime.Today.AddDays(21);
            }
        }
所以,这条线出现的问题就是这个问题

else if (junior.memType != permRace)
            {
                //throws an exception if the member doesn't have the correct type junior
                throw new Exception(senior.FirstName + " does not meet the requirements to join this race, must be type: " + permRace);
            }
当抛出错误时,它会给出一个异常消息“Object reference not set to a instance of a Object”,而不是我要求它抛出的异常。你看,我希望它抛出这个异常,它这样做是正确的,但它没有提供正确的消息


我做错了什么?

始终检查主设备和辅助设备是否存在空情况,例如

if ((junior == null) || (junior.memType == null))

然后根据
junior
verses
junior处理错误情况。对于
memType
而言,memType
可能是一个真正的调试/开发错误,而已知的缺失
junior

senior可能为空,与引发异常无关,您正在尝试对不存在的对象调用FirstName。我猜应该是
junior。FirstName
,而不是senior。哇,我甚至没有注意到那个senior,直到你说出来,我一定跳过了它,认为它是更伟大的东西,把这个作为一个答案,我不敢相信我这么愚蠢。而且,我相信你的
joinJunior
方法中的块是不正确的。第一个(
if(junior!=null)
)应该涵盖更多的情况,因为您正在执行
junior.memType!=permRace
在else if中-如果您到达那里,则可以保证
junior
null
(因此,NullReferenceException)