English 中文(简体)
单一行动要求即刻存放处或收取费用。
原标题:Single action requiring instantiation of repository or NullReferenceException occurs

目前,我有一位控制员,他们采取了多项行动,但我已陷入了一个问题,采取单一行动,似乎要求在行动本身内对存放处进行即时处理,否则,在日常行动期间,我会得到一种“不尊重”,而行动本身似乎与控制者的其他行动不同。 这是我迄今为止所做的:

    public class PatentController : Controller
    {
        IRepositoryExtension patentRepository;

        public PatentController()
        {
            PatentRepository patentRepository = new Proj.Data.PatentRepository();
        }

        //Constructor for unit test project
        public PatentController(IRepositoryExtension repository)
        {
            patentRepository = repository;
        }

        public ActionResult Index()
        {
            return View();
        }

        //Other actions removed for brevity

        public ActionResult DetailsPartial(string id)
        {
            //If this PatentRepository is removed, NullReferenceException occurs
            PatentRepository patentRepository = new Proj.Data.PatentRepository();
            USPTOPatent usptoPatent = patentRepository.GetPatent(id);
            return PartialView("DetailsPartial", usptoPatent);
        }

是否有特别理由,为什么我需要在行动中立即储存库,以便其工作? 如果我说:

反对不指标的。

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

资料来源:

Line 155: //PatentRepository patentRepository = new Proj.Data.PatentRepository(); Line 156: USPTOPatent usptoPatent = patentRepository.GetPatent(id); Line 157:
return PartialView("DetailsPartial", usptoPatent); Line 158: }

最佳回答

您的违约建筑商将<代码>new的结果分配到一个地方变量上,这优先于在班级范围上宣布的数值。 因此,当控制器以这种方式创建时,该成员的变数代码(<>)即刻存留代码/代码(>)尚未启动。

改变违约方:

 public PatentController()
    {
        /*PatentRepository*/ patentRepository = new Proj.Data.PatentRepository();
    }
问题回答

GetPatent(> 回归USPTOPatent的静态方法? 这种方法像固定不变。

If the method isn t static the object will need to be instantiated to be utilized.

See: Static and Instance Members.

如果该方法是静态的,则确保该方法在所有编码道路上回归物体。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签