MVC: By SelectListItem I get “System.NullReferenceException: Object reference not set to an instance of an object.” error

By SelectListItem I get "System.NullReferenceException: Object reference not set to an instance of an object." error. What do I wrong?

This is my xml:

<?xml version="1.0" encoding="utf-8"?>
<Artiesten>
  <Item ID_Artiest="509998" Artiest="-" Product="-" Land="" Plaats="" Website="" Genre="Onbekend" />
  <Item ID_Artiest="970119" Artiest="(hed)p.e." Product="-" Land="" Plaats="" Website="" Genre="Onbekend" />
  <Item ID_Artiest="970080" Artiest="(urusei) yatsura" Product="-" Land="" Plaats="" Website="" Genre="Noise" />
  <Item ID_Artiest="1010302" Artiest="...and you will know us by the trail of dead" Product="-" Land="Verenigde Staten" Plaats="Austin (TX)" Website="" Genre="Noise" />
...
</Artiesten>

This is my Model

public class ArtiestInfoModel
    {
        [DisplayName("Selecteer artiest: ")]
        public int ID_Artiest { get; set; }
        public string SelectedArtiest { get; set; }
        public IEnumerable<SelectListItem> ArtiestenLijst { get; set; }
    }

The Controller:

 public ActionResult Index(int? AID_Artiest=810000)
        {
            var document = XDocument.Load(Server.MapPath("~/App_Data/Artiesten.xml"));
            var query = new ArtiestInfoModel
            {
                ArtiestenLijst =
                    (from artiest in document.Descendants("Artiesten").Elements("Item")
                    select new SelectListItem
                    {
                        Value = artiest.Attribute("ID_artiest").Value,
                        Text = artiest.Attribute("Artiest").Value,
                        Selected = false //  artiest.Attribute("ID_artiest").Value == AID_Artiest.ToString()
                    }).ToList()
            };

            ViewBag.ID_Artiest = AID_Artiest.Value;
            ViewBag.ArtiestenLijst = query.ArtiestenLijst;

            return View();
        }

The Controller is called by the next DropDownList:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">    
    <% using (Html.BeginForm("Index", "Hitdossier", FormMethod.Post, new { id = "frmArtiest" })) %>
    <% { %>
        <table><tr><th width="1000" align="left">Selecteer artiest:</th></tr>
            <tr>
                <td><%= Html.DropDownList("ID_Artiest",
                    new SelectList(ViewBag.ArtiestenLijst, "Value", "Text"),
                    "-- Selecteer artiest --",
                    new
                    {
                        @onchange = "document.getElementById('frmArtiest').submit();"
                    })%>
                </td>
            </tr>
        </table>
        <br />
        <br />
        <div id="divPartialView">
            <%= Html.Action("Detail_Hitdossier", new { AID_Artiest = ViewBag.ID_Artiest })%> <br />
        </div>
    <%} %> 
</asp:Content>

thanks

Answers


In your controller you are reading the node ID_artiest as a lowercase while the xml shows camelcase.

Change

Value = artiest.Attribute("ID_artiest").Value,

To

Value = artiest.Attribute("ID_Artiest").Value,

If artiest.Attribute("ID_artiest") result in a NULL value, then reading a property Value will throw the null-reference-exception.


Need Your Help

ELMAH.axd is browsable but will never log any of my errors?

asp.net-mvc elmah

I have it all set up and I can browse to /elmah.axd but I have been causing all these errors and it won't log any of them, it just says 'no errors logged'... what the heck did I do wrong?

Google maps API showing blank map when given LatLng cords from HTML5 Geolocation

javascript html5 google-maps google-maps-api-3

I'm trying to use HTML5's geolocation features. I have a function to get the coordinates and then another to use those coordinates in a call to the Google Maps API. If I manually specify the Latitu...