Results 1 to 23 of 23

Thread: [RESOLVED] Object reference not set to an instance of the object

  1. #1

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Resolved [RESOLVED] Object reference not set to an instance of the object

    Hi,

    I keep receiving "Object reference not set to an instance of the object" when trying to run a form. This is the code I am using:

    asp Code:
    1. <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcMusicStore.Models.Album>" %>
    2.  
    3. <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    4.     Edit
    5. </asp:Content>
    6.  
    7. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    8.  
    9.     <h2>Edit - <%: Model.Title%></h2>
    10.  
    11.     <% using (Html.BeginForm()) {%>
    12.         <%: Html.ValidationSummary(true) %>
    13.        
    14.         <fieldset>
    15.             <legend>Edit Album</legend>
    16.             <%Html.EditorForModel(); %>
    17.             <div class="editor-label">
    18.                 <%: Html.LabelFor(model => model.AlbumId) %>
    19.             </div>
    20.             <div class="editor-field">
    21.                 <%: Html.TextBoxFor(model => model.AlbumId) %>
    22.                 <%: Html.ValidationMessageFor(model => model.AlbumId) %>
    23.             </div>
    24.            
    25.             <div class="editor-label">
    26.                 <%: Html.LabelFor(model => model.GenreId) %>
    27.             </div>
    28.             <div class="editor-field">
    29.                 <%: Html.TextBoxFor(model => model.GenreId) %>
    30.                 <%: Html.ValidationMessageFor(model => model.GenreId) %>
    31.             </div>
    32.            
    33.             <div class="editor-label">
    34.                 <%: Html.LabelFor(model => model.ArtistId) %>
    35.             </div>
    36.             <div class="editor-field">
    37.                 <%: Html.TextBoxFor(model => model.ArtistId) %>
    38.                 <%: Html.ValidationMessageFor(model => model.ArtistId) %>
    39.             </div>
    40.            
    41.             <div class="editor-label">
    42.                 <%: Html.LabelFor(model => model.Title) %>
    43.             </div>
    44.             <div class="editor-field">
    45.                 <%: Html.TextBoxFor(model => model.Title) %>
    46.                 <%: Html.ValidationMessageFor(model => model.Title) %>
    47.             </div>
    48.            
    49.             <div class="editor-label">
    50.                 <%: Html.LabelFor(model => model.Price) %>
    51.             </div>
    52.             <div class="editor-field">
    53.                <%: Html.TextBoxFor(model => model.Price, String.Format("{0:F}", Model.Price)) %>
    54.                 <%: Html.ValidationMessageFor(model => model.Price) %>
    55.             </div>
    56.            
    57.             <div class="editor-label">
    58.                 <%: Html.LabelFor(model => model.AlbumArtUrl) %>
    59.             </div>
    60.             <div class="editor-field">
    61.                 <%: Html.TextBoxFor(model => model.AlbumArtUrl) %>
    62.                 <%: Html.ValidationMessageFor(model => model.AlbumArtUrl) %>
    63.             </div>
    64.            
    65.             <p>
    66.                 <input type="submit" value="Save" />
    67.             </p>
    68.         </fieldset>
    69.  
    70.     <% } %>
    71.  
    72.     <div>
    73.         <%: Html.ActionLink("Back to List", "Index") %>
    74.     </div>
    75.  
    76. </asp:Content>

    It is complaining about model some reason and I can't figure out why.

    The first error occurs on this line:

    asp Code:
    1. <h2>Edit - <%: Model.Title%></h2>

    However if I remove it the next error occurs on this line and so on:

    asp Code:
    1. <%: Html.TextBoxFor(model => model.Price, String.Format("{0:F}", Model.Price)) %>

    Have I missed a namespace somewhere?

    Thanks,


    Nightwalker
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Object reference not set to an instance of the object

    If you'd missed a namespace then it wouldn't compile. All that means is that your Model is null. Have a look at where you display the view, i.e. where you call View or the like in a controller action. You'll find that you aren't pass an object at that point. The object you pass when you call View is the Model for the view.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Object reference not set to an instance of the object

    Well, putting the cursor over the modal variable it says it is set to MvcMusicStore.Models.Album.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Object reference not set to an instance of the object

    Quote Originally Posted by Nightwalker83 View Post
    Well, putting the cursor over the modal variable it says it is set to MvcMusicStore.Models.Album.
    That would be it's type, but that doesn't mean that you have passed an instance of that type when creating the view. Can you show us the action where you create that view?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Object reference not set to an instance of the object

    I create the edit view by right-clicking "Edit" then add view this is the code I am creating it from:

    asp Code:
    1. // GET: /StoreManager/Edit/5
    2.  
    3.         public ActionResult Edit(int id)
    4.         {
    5.             var album = StoreDB.Albums.SingleOrDefault(a => a.AlbumId == id);
    6.            
    7.             return View(album);
    8.         }
    9.  
    10.         //
    11.         // POST: /StoreManager/Edit/5
    12.  
    13.         [HttpPost]
    14.         public ActionResult Edit(int id, FormCollection collection)
    15.         {
    16.             try
    17.             {
    18.                 // TODO: Add update logic here
    19.  
    20.                 return RedirectToAction("Index");
    21.             }
    22.             catch
    23.             {
    24.                 return View();
    25.             }
    26.         }
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Object reference not set to an instance of the object

    Have you confirmed that 'album' is not null when you call View?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Object reference not set to an instance of the object

    Yes, the only part that doesn't seem to be working is this line:

    asp Code:
    1. <%: Html.TextBoxFor(model => model.Price, String.Format("{0:F}", Model.Price)) %>
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Object reference not set to an instance of the object

    If you click on 'Model.Price' and hit F9 to add a breakpoint, then run the app, what is the value of Model.Price when the breakpoint is hit?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Object reference not set to an instance of the object

    Model.Price equals null although, I am not sure where I set model and Model in the first place.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Object reference not set to an instance of the object

    You're saying that Model.Price is null. What type is that Price property? I would have thought 'decimal' which can't be null, although maybe it's 'decimal?'. Assuming that Model.Price is null, what's the value of Model?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Object reference not set to an instance of the object

    I can't remember setting either model or Model in a generic sense.

    Under the column listing it has: Price numeric then in the value/property it has Price/Decimal.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Object reference not set to an instance of the object

    I'm not asking you about the database. I'm asking you about the view. In the view, the Model property is type MvcMusicStore.Models.Album which I know because the @Page directive says so:
    Code:
    Inherits="System.Web.Mvc.ViewPage<MvcMusicStore.Models.Album>"
    That type apparently has a Price property, which I would expect to by type 'decimal'. Is it indeed that type? If so then it can't possibly be null. What value does Model.Price have and, if it doesn't have a value, what value does Model have?

    As I said previously, you provide the model for the view when you call the View method in your action. The argument that you pass to the View method is the value of the Model property in the view.

    The 'model' that you're talking about isn't something you set. It's a range variable for a lambda expression. The first argument passed to TextBoxFor is a Expression. You could as easily use:
    Code:
    x => x.Price
    You simply telling MVC to create a TextBox for the Price property of the model of the view. 'model' is used for the range variable because it makes sense, but it could be called anything.

    You're getting a NullReferenceException. When that happens, the first step is always to determine what reference is null. That's what we're trying to do here. The most likely candidate is Model. If Model is null then that means that you aren't passing an object to View when you call it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Object reference not set to an instance of the object

    Well, the fact that it works for the other fields on the form suggests that I have setup the inherits part correctly. I think it has to do with the fact that Model.Price uses an uppercase "M" instead of a lowercase "m" like the rest of the code above.

    I will have to email my lecturer and ask for advice and see if he can figure out what I am doing wrong.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Object reference not set to an instance of the object

    Not having used it for a while, I had a look at the documentation for the TextBoxFor method and, basically, you're using it wrong. You are trying to pass the value to the method but you aren't supposed to. You specify the property of the Model to use in the first parameter and that is used to get the value. Any formatting should be handled by data annotations on the property. If you want to pass a formatted value directly then you should be calling the TextBox method rather than the TextBoxFor method. I suggest that you read the documentation for both.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Object reference not set to an instance of the object

    Quote Originally Posted by Nightwalker83 View Post
    Yes, the only part that doesn't seem to be working is this line:

    asp Code:
    1. <%: Html.TextBoxFor(model => model.Price, String.Format("{0:F}", Model.Price)) %>
    I realise that I am joining this thread late, but just noticed something, so I thought I would mention it...

    Is the above a copy and paste from Visual Studio? If so, did you mean to have both model and Model? Remember C# is case sensitive.

    Gary

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Object reference not set to an instance of the object

    Quote Originally Posted by gep13 View Post
    I realise that I am joining this thread late, but just noticed something, so I thought I would mention it...

    Is the above a copy and paste from Visual Studio? If so, did you mean to have both model and Model? Remember C# is case sensitive.

    Gary
    'Model' is a property of the view. 'model' is the lambda expression parameter.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  17. #17
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Object reference not set to an instance of the object

    Ah, of source, sorry my bad! Should have read the thread properly

    Gary

  18. #18

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Object reference not set to an instance of the object

    Quote Originally Posted by gep13 View Post
    I realise that I am joining this thread late, but just noticed something, so I thought I would mention it...

    Is the above a copy and paste from Visual Studio? If so, did you mean to have both model and Model? Remember C# is case sensitive.

    Gary
    That is how it was written in the notes I was given. I will check with my lecturer next week to make sure.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  19. #19
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Object reference not set to an instance of the object

    I've explained what the issue is. You are hybridising the TextBox and TextBoxFor methods and not calling either properly. If you want to call TextBoxFor then you don't specify a value. You specify an expression that describes the property of the view's Model you want to create a TextBox for and it does the rest. If you want to format the value then you have to use DataAnnotations on the model class itself. If you want to specify a value then you call TextBox rather than TextBoxFor.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  20. #20

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Object reference not set to an instance of the object

    Putting:

    asp Code:
    1. <div class="editor-field">
    2.                 <%: Html.TextBoxFor(model => model.Price) %>
    3.                 <%: Html.ValidationMessageFor(model => model.Price) %>
    4.             </div>

    Shows the form without errors! Although, the screenshot I was given has data in the fields which, probably lead to my confusing in the first place.
    Last edited by Nightwalker83; Jun 23rd, 2011 at 02:59 AM. Reason: Fixed spelling!
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  21. #21
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Object reference not set to an instance of the object

    The issue there is that the value will be displayed without currency formatting. If you want to use TextBoxFor and still get currency formatting then you would have to apply a DataTypeAttribute with a DataType of Currency to the Price property of the Album class.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  22. #22

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Object reference not set to an instance of the object

    I have finally finished typing up all the code I just need to fix up a couple of mistakes on Thursday so UI an get it working.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  23. #23

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Object reference not set to an instance of the object

    Well, I have put the original code back in to the project and it works although, I have no idea why.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width