View正在抛出“NullReferenceException”,请帮助!!! [英] View is throwing "NullReferenceException", please help!!!

查看:86
本文介绍了View正在抛出“NullReferenceException”,请帮助!!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发生此错误是因为我设置了输入字段值!!!



我的型号:

This error is occuring because i set input field value!!!

my model :

public class Manufractururer
   {
       public int Id { set; get; }
       public string Name { set; get; }
       public string Email { set; get; }
       public string MobilePhone { set; get; }
       public string DL1 { set; get; }
       public string DL2 { set; get; }
       public string BankName { set; get; }
       public string AccountNumber { set; get; }
       public string IfscCode { set; get; }
       public string Tin { set; get; }
       public string PAN { set; get; }
       public string ShortDescription { set; get; }
       public string Description { set; get; }
       public string mLogo { set; get; }
       public string MetaTitle { set; get; }
       public string MetaDescription { set; get; }
       public string MetaKeywords { set; get; }
       public int IsActive { set; get; }
       public int IsDelete { set; get; }
       public ManufractururerAddress address { set; get; }
   }



我的行动:


My Actions:

public ActionResult AddManufractururer()
      {
          ViewBag.error = "";
          ViewBag.States = helper.GetAllStates();
          return View(new Manufractururer());
      }
      [HttpPost,ValidateInput(false)]
      public ActionResult AddManufractururer(Manufractururer m)
      {
          ViewBag.States = helper.GetAllStates();
          string  result = "";
         result=  ManufacturerHelper.AddManufractururer(m);
         if (result == "Success")
          {
             return RedirectToAction("Index");
          }
         ViewBag.error = "Company Already Exists!!";
          return View(m);
      }





我的观点:





My view:

@if(ViewBag.error != null)
{
    <h4 style="height:30px;color:red">@ViewBag.error</h4>
}

<form action="/Manufacturer/AddManufractururer" method="post">
        <div class="editor-label">
            <label for="Name">Name</label>
        </div>
        <div class="editor-field">
            <input type="text" id="Name" name="Name"  value="@(Model.Name == null ? "" : Model.Name)"/>
            <span class="error">Please Enter  Name!</span>
        </div>
        <div class="editor-label">
            <label for="Email">Email</label>
        </div>
        <div class="editor-field">
            <input type="text" id="Email" name="Email" value="@(Model.Email == null ? "" : Model.Email)" />
            <span class="error">Please Enter  Email!</span>
        </div>
        <div class="editor-label">
            <label for="MobilePhone">Mobile Number</label>
        </div>
        <div class="editor-field">
            <input type="text" id="MobilePhone" name="MobilePhone" value="@(Model.MobilePhone == null ? "" : Model.MobilePhone)" class="isNum" />
            <span class="error">Please Enter Valid Mobile Number!</span>
        </div>
        <div class="editor-label">
            <label for="DL1">Drug Licence 1</label>
        </div>
        <div class="editor-field">
            <input type="text" id="DL1" name="DL1" value="@(Model.DL1 == null ? "" : Model.DL1)" />
            <span class="error">Please Enter your Drug Licence Number!</span>
        </div>
        <div class="editor-label">
            <label for="DL2">Drug Licence 2</label>
        </div>
        <div class="editor-field">
            <input type="text" id="DL2" name="DL2" value="@(Model.DL2 == null ? "" : Model.DL2)" />
            <span class="error">Please Enter your Drug Licence Number!</span>
        </div>
        <div class="editor-label">
            <label for="BankName">Bank Name</label>
        </div>
        <div class="editor-field">
            <input type="text" id="BankName" name="BankName" value="@(Model.BankName == null ? "" : Model.BankName)" />
            <span class="error">Please Enter your Bank Name!</span>
        </div>
        <div class="editor-label">
            <label for="AccountNumber">Account Number</label>
        </div>
        <div class="editor-field">
            <input type="text" id="AccountNumber" name="AccountNumber" value="@(Model.AccountNumber == null ? "" : Model.AccountNumber)" />
            <span class="error">Please Enter your Account Number!</span>
        </div>
        <div class="editor-label">
            <label for="IfscCode">Ifsc Code</label>
        </div>
        <div class="editor-field">
            <input type="text" id="IfscCode" name="IfscCode" value="@(Model.IfscCode == null ? "" : Model.IfscCode)" />
            <span class="error">Please Enter your Ifsc Code!</span>
        </div>
        <div class="editor-label">
            <label for="Tin">Tin #</label>
        </div>
        <div class="editor-field">
            <input type="text" id="Tin" name="Tin" value="@(Model.Tin == null ? "" : Model.Tin)" />
            <span class="error">Please Enter your Tin Number!</span>
        </div>
        <div class="editor-label">
            <label for="PAN" >PAN #</label>
        </div>
        <div class="editor-field">
            <input type="text" id="PAN" name="PAN" value="@(Model.PAN == null ? "" : Model.PAN)" />
            <span class="error">Please Enter your PAN Number!</span>
        </div>


        <div class="text-editer">
            <label for="Description">Description</label>
            <div class="timy-edit">
                <textarea id="Description" name="Description"></textarea><span class="error" >Please Enter your Description!</span>
            </div>
        </div>



        <div class="text-editer">
            <label for="ShortDescription">Short Description</label>
            <div class="timy-edit">
                <textarea id="ShortDescription" name="ShortDescription"></textarea>
            </div>
        </div>
        <div class="editor-label">
            <label for="mLogo">Logo</label>
        </div>
        <div class="editor-field">
            <input type="file" name="Logo" id="Logo" />
            <input type="hidden"id="mLogo" name="mLogo"/>
            <img src="" height="30" width="30"  id="manufracturer-logo" style="display:none">
        </div>

        <div class="editor-label">
            <label for="MetaTitle">MetaTitle</label>
        </div>
        <div class="editor-field">
            <input type="text" id="MetaTitle" name="MetaTitle" value="@(Model.MetaTitle == null ? "" : Model.MetaTitle)" />
        </div>

        <div class="editor-label">
            <label for="MetaDescription">Meta Description</label>
        </div>
        <div class="editor-field">
            <input type="text" id="MetaDescription" name="MetaDescription" value="@(Model.MetaDescription == null ? "" : Model.MetaDescription)" />
        </div>

        <div class="editor-label">
            <label for="MetaKeywords">Meta Keywords</label>
        </div>
        <div class="editor-field">
            <input type="text" id="MetaKeywords" name="MetaKeywords" value="@(Model.MetaKeywords == null ? "" : Model.MetaKeywords)" />

        </div>

        <div class="editor-label">
            <label for="IsActive">Enable</label>
        </div>
        <div class="editor-field">
            <div><input type="radio" id="IsActive" name="IsActive" value="1" checked />Yes</div>
            <div><input type="radio" id="IsActive" name="IsActive" value="0" />No</div>
        </div>
        <input type="hidden" id="IsDelete" name="IsDelete" value="0" />

        <div class="editor-label">
            <label for="FirstName" >First Name</label>
        </div>
        <div  class="editor-field">

            <input type="text" id="FirstName" name="address.FirstName" value="@(Model.address.FirstName == null ? "" : Model.address.FirstName)" /><span class="error">Please Enter  First Name!</span>
        </div>

        <div class="editor-label">
            <label for="LastName">Last Name</label>
        </div>
        <div class="editor-field">
            <input type="text" id="LastName" name="address.LastName" value="@(Model.address.LastName == null ? "" : Model.address.LastName)" /><span class="error">Please Enter  Last Name!</span>
        </div>

        <div class="editor-label">
            <label for="Address">Address</label>
        </div>
        <div class="editor-field">
            <input type="text" id="Address" name="address.Address" value="@(Model.address.Address == null ? "" : Model.address.Address)" /><span class="error">Please Enter  Address!</span>
        </div>

        <div class="editor-label">
            <label for="Address2">Address-2</label>
        </div>
        <div class="editor-field">
            <input type="text" id="Address2" name="address.Address2" value="@(Model.address.Address2 == null ? "" : Model.address.Address2)" />
        </div>

        <div class="editor-label">
            <label for="PostalCode">ZIP/Postal Code</label>
        </div>
        <div class="editor-field">
            <input type="text" id="PostalCode" name="address.PostalCode" class="isNum" value="@(Model.address.PostalCode == null ? 0 : Model.address.PostalCode)" /><span class="error">Please Enter valid ZIP/Postal Code!</span>
        ></div>

        <div class="editor-label">
            <label for="State">State</label>
        </div>

        <div class="editor-field" style="height:30px">
            <!-- <input type="text" id="State" name="State" />-->
            <select id="State" name="address.State">
                <option id="select">Select State</option>
                @if (ViewBag.States != null)
                {
                    foreach (var item in ViewBag.States)
                    {

                        <option id="@item">@item</option>

                    }
                }
            </select>
            <span class="error">Please Enter  State!</span>
        </div>

        <div class="editor-label">
            <label for="City">City</label>
        </div>
        <div class="editor-field" style="height:30px">
            <select id="City" name="address.City">
                <option id="Select">Select City</option>
            </select>
            <span class="error">Please Enter  City!</span>
        </div>



        <div class="editor-label">
            <label for="HomePhone">Home Phone</label>
        </div>
        <div class="editor-field">
            <input type="text" id="HomePhone" name="address.HomePhone" value="@(Model.address.HomePhone == null ? "" : Model.address.HomePhone)" class="isNum" /><span class="error">Please Enter valid Phone Number!</span>
        </div>

        <div class="editor-label">
            <label for="MobilePhone">Mobile Phone</label>

        </div>
        <div class="editor-field">
            <input type="text" id="MobilePhone" name="address.MobilePhone" value="@(Model.address.MobilePhone == null ? "" : Model.address.MobilePhone)" class="isNum" /><span class="error">Please Enter valid Phone Number!</span>
        </div>

        <div class="editor-label">
            <label for="Other">Other</label>
        </div>
        <div class="editor-field">
            <input type="text" id="Other" value="@(Model.address.Other==null?"":Model.address.Other)" name="address.Other" />
        </div>

        <p>
            <input type="submit" value="SAVE" />
            <a href="/Manufacturer/index" class="btn btn-default ">Back</a>
        </p>
    </form>

推荐答案

Looks like your model is NULL so you are getting null pointer exception when trying to access Model.Name property.



Please use below code:



Looks like your model is NULL so you are getting null pointer exception when trying to access Model.Name property.

Please use below code:

<input type="text" id="Name" name="Name" value="@((Model != null && Model.Name != null) ? Model.Name : " />


The problem comes here,



The problem comes here,

return View(new Manufractururer());





You are returning a new instance of the Manufractururer object (brrr), which at that instance does not have an instance (or the initial value) for the member Name.





You are returning a new instance of the Manufractururer object (brrr), which at that instance does not have an instance (or the initial value) for the member Name.

public string Name { set; get; }





This can be replaced with,





This can be replaced with,

public string Name = "";





But that isn’t a good way of doing this. I would recommend that you create a new instance of the object and also pass the initial values for the members for further processing rather than just passing a new instance constructor. Such as this,





But that isn't a good way of doing this. I would recommend that you create a new instance of the object and also pass the initial values for the members for further processing rather than just passing a new instance constructor. Such as this,

var obj = new Manufractururer() 
{  /* Initialize the members such as */ 
   Name = "Afzaal Ahmad Zeeshan",
   ...
};





Now when you will pass this obj object to the View function the View will have a Model that has at least non-null object.





Now when you will pass this obj object to the View function the View will have a Model that has at least non-null object.

return View(obj);

// OR

return View(
   new Manufractururer 
   {
      Name = "Afzaal Ahmad Zeeshan",
      ...
   }
);





Tip: A null exception is always raised when something does not exist in the memory, or a reference to that object is invalid. There is always something present in the memory, but casting it to an object doesn’t work; in case when data is invalid for an object type. Your case is same, the Name is null, and causes the problem in your case. :-)



A good solution is to initialize it with a value, rather than checking it to be a null or not. If you always pass an initial value (remember, empty string is not equal to null string) you will not have to bother checking for the values to be null; unless you mess up somewhere else.



Tip: A null exception is always raised when something does not exist in the memory, or a reference to that object is invalid. There is always something present in the memory, but casting it to an object doesn't work; in case when data is invalid for an object type. Your case is same, the Name is null, and causes the problem in your case. :-)

A good solution is to initialize it with a value, rather than checking it to be a null or not. If you always pass an initial value (remember, empty string is not equal to null string) you will not have to bother checking for the values to be null; unless you mess up somewhere else.


这篇关于View正在抛出“NullReferenceException”,请帮助!!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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