system.web.mvc.dll中发生类型'system.nullreferenceexception'的异常,但未在用户代码中处理 [英] An exception of type 'system.nullreferenceexception' occurred in system.web.mvc.dll but was not handled in user code

查看:163
本文介绍了system.web.mvc.dll中发生类型'system.nullreferenceexception'的异常,但未在用户代码中处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用devxscheduler mvc和iam调用一个视图,在该视图中我在getschedulersettings()方法的模型方法中有一些html代码,我不知道为什么我在这里得到null引用异常.有人可以澄清一下这是编程新手吗

我尝试过的事情:

Iam using devxscheduler mvc and iam calling a view where i having some html code in model method in getschedulersettings() method and i dont know why iam getting null reference exception here.Can someone please clarify this iam new to programming

What I have tried:

public static SchedulerSettings GetSchedulerSettings()
            {

                return GetSchedulerSettings(null);
            }

        public static SchedulerSettings GetSchedulerSettings(this System.Web.Mvc.HtmlHelper customHtml)
        {
            SchedulerSettings settings = new SchedulerSettings();
            settings.Name = "scheduler";
            settings.CallbackRouteValues = new { Controller = "Home", Action = "SchedulerPartial" };
            settings.EditAppointmentRouteValues = new { Controller = "Home", Action = "EditAppointment" };
            settings.CustomActionRouteValues = new { Controller = "Home", Action = "CustomCallBackAction" };

            settings.Storage.Appointments.Assign(SchedulerDataHelper.DefaultAppointmentStorage);
            settings.Storage.Resources.Assign(SchedulerDataHelper.DefaultResourceStorage);

            settings.Storage.EnableReminders = true;
            settings.GroupType = SchedulerGroupType.None;
            settings.Views.DayView.Styles.ScrollAreaHeight = 400;
            settings.Start = DateTime.Now;
            settings.SetToolbarViewSelectorTemplateContent(container =>
            {
                customHtml.Partial("ViewSelectorPartial");
            });

            return settings;
        }

推荐答案

由于您将参数传递为null
Since you are passing the argument as null
GetSchedulerSettings(null);


然后您尝试访问该对象的成员,


and you try to access the members of the object,

customHtml.Partial("ViewSelectorPartial");

显然会引发空引用异常 [ ^ ].

将相关参数传递给方法并尝试,它应该可以工作.

签名看起来像是扩展方法 [ ^ ]

which will obviously throw the null reference exception[^].

Pass the relevant parameter to the method and try, it should work.

The Signature looks like an extension method[^]

public static SchedulerSettings GetSchedulerSettings(this System.Web.Mvc.HtmlHelper customHtml)


因此很可能应该从HtmlHelper对象调用方法目录,例如


so most probably you should call the method directory from the HtmlHelper object, like

objHtmlHelper.GetSchedulerSettings();


这是我们遇到的最常见的问题之一,也是我们遇到的一个问题最没有能力回答,但您最有能力回答自己.

让我来解释一下错误的含义:您试图使用变量,属性或方法的返回值,但其中包含null-这意味着变量中没有类的实例.
它有点像一个口袋:您的衬衫上有一个口袋,用来握笔.如果您伸手去拿口袋,发现那里没有笔,那您就不能在纸上签名了-如果尝试,您会得到非常有趣的表情!空的口袋为您提供了一个空值(这里没有笔!),因此您不能做任何通常在找回笔时会做的事情.为什么它是空的?这就是问题所在-可能是您今天早上离开家时忘记拿起笔,或者昨晚脱掉笔时把它留在了昨天衬衫的口袋里.

我们无法分辨,因为我们不在那,更重要的是,我们甚至看不到您的衬衫,更不用说口袋里的东西了!

回到计算机,您就以某种方式做了同样的事情-我们看不到您的代码,更不用说运行它了,找出不应该包含null的内容.
但是您可以-Visual Studio将在这里为您提供帮助.在调试器中运行程序,当程序失败时,VS将向您显示发现问题所在的行.然后,您可以开始查看它的各个部分,以了解什么是空值,然后开始回顾您的代码以找出原因.因此,在包含错误行的方法的开头放置一个断点,然后从头开始再次运行程序.这次,VS将在错误发生之前停止运行,并让您通过逐步查看代码来检查正在发生的情况.

但是我们无法做到这一点-我们没有您的代码,如果我们有它,我们也不知道如何使用它,我们没有您的数据.因此,请尝试一下-看看您能找到多少信息!
This is one of the most common problems we get asked, and it''s also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It''s a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn''t a pen there, you can''t sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can''t do anything that you would normally do once you retrieved your pen. Why is it empty? That''s the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can''t tell, because we weren''t there, and even more importantly, we can''t even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can''t see your code, much less run it and find out what contains null when it shouldn''t.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can''t do that - we don''t have your code, we don''t know how to use it if we did have it, we don''t have your data. So try it - and see how much information you can find out!


这篇关于system.web.mvc.dll中发生类型'system.nullreferenceexception'的异常,但未在用户代码中处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆