1

The app is designed to allow the user to enter a an IP address for a local machine and and it will then return the HDD information for that machine. It starts out with a default value already in the TextAreaFor box and performs the query for that value. This part works with no problem. But when a user tries to enter in their own value and hit the Refresh button, it keeps coming up with the error Object reference not set to an instance of an object.

I'm not sure why this is happening. It seems to me that clicking the button submits a POST action, which should kick off the second method in the controller. The current model is then passed to the controller with the values in the TextAreaFor attached and the mainCode() method is run on the new values.

Edit: According to What is a NullReferenceException, and how do I fix it? I am pretty sure that I am returning an empty model from my controller. I just don't see how. The form field should be sending the controller everything contained in TextAreaFor so the model should not be empty.

Edit2: I did some testing and the model is getting returned alright, but the values from TextAreaFor are not. When the mainCode() tries to do some logic to startDrives.startingDrives, it can't because that variable is empty for some reason.

Model:

namespace RelengAdmin.Models
{
    public class DriveInfo
    {
        public class DriveHolder
        {
            public string startingDrives {get; set;}
        }

        public DriveHolder startDrives = new DriveHolder();

        public void mainCode()
        {
            /****Code to return the HDD size omitted****/
        }
    }
}

View:

@using (Html.BeginForm())
{
    <input type="submit" value="Refresh" />

    @Html.TextAreaFor(model => model.startDrives.startingDrives, new {@class = "HDDTextBox"})
}

Controller:

namespace RelengAdmin.Controllers
{
    public class HDDCheckerController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            DriveInfo myDrive = new DriveInfo();
            myDrive.startDrives.startingDrives = "148.136.148.53"

            myDrive.mainCode();
            return View(myDrive);
        }

        [HttpPost]
        public ActionResult Index(DriveInfo model)
        {
            model.mainCode();
            return View(model);
        }
    }
}
Community
  • 1
  • 1
Taylor Liss
  • 593
  • 4
  • 27
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Igor Aug 30 '16 at 14:43
  • 2
    **I am pretty sure that I am returning an empty model** ... well you have to be sure. This error could be caused by either `model` being null, or something inside the `mainCode()` method. Have you tried to check if `model` is null in your HttpPost action method? – ekad Aug 30 '16 at 14:59
  • Ok, so the model is not empty, but the values inside it are. During some of the `mainCode()`, it attempts to do some logic to `startDrives.startingDrives`, but that variable is empty when it shouldn't be. – Taylor Liss Aug 30 '16 at 15:05

2 Answers2

4

The issue is that your model's startDrives property is not actually declared as a property with getters and setters, so the model binder won't bind to it. I was able to duplicate the issue locally, and solve it by declaring the startDrives as a property and initializing it in the constructor.

public class DriveInfo
{

   public class DriveHolder
   {
      public string startingDrives { get; set; }
   }


   public DriveHolder startDrives { get; set; }

   public DriveInfo()
   {
      startDrives = new DriveHolder();
   }

   public void mainCode()
   {
      /****Code to return the HDD size omitted****/
   }    
}
stephen.vakil
  • 3,492
  • 1
  • 18
  • 23
1

Your question is a bit unclear of where the model is actually null.. but I would assume that when you hit your button, it goes to the correct action, but there is nothing in model because you haven't passed any specific values..

so try this:

CSHTML

@using (Html.BeginForm())
{
    <input type="submit" value="Refresh" />

    @Html.TextArea("startingDrive", "148.136.148.53", new {@class = "HDDTextBox"})
}

Controller

[HttpPost]
public ActionResult Index(string startingDrive)
{
    DriveInfo searchThisDrive = new DriveInfo();

    searchThisDrive.startDrives.startingDrives = startingDrive;
    searchThisDrive.mainCode();

    return View(searchThisDrive);
}

Let me know if this helps!

Grizzly
  • 5,873
  • 8
  • 56
  • 109
  • That worked! The only thing is since you removed `model => model.startDrives.startingDrives`, the textarea is empty at the start (even though the logic is being performed on it correctly). Is there anyway to keep the default IP displayed within it? – Taylor Liss Aug 30 '16 at 15:13
  • 1
    @TaylorLiss just a question.. even though the textarea is empty at the start.. is it still pulling the information for `148.136.148.53`? – Grizzly Aug 30 '16 at 15:20
  • Yes, it is. Which is great! It just doesn't show the `148.136.148.53` in the box, even though it is using it correctly. The box loads empty. – Taylor Liss Aug 30 '16 at 15:21
  • Yes! That did it! Thank you again! Awesome help! – Taylor Liss Aug 30 '16 at 15:24
  • 1
    @TaylorLiss you're welcome. glad I could help! happy coding! – Grizzly Aug 30 '16 at 15:25
  • 2
    This is a work-around to the underlying issue with public fields vs properties and model binding. The code is just manually performing model binding tasks that will work automatically if the field is declared correctly. With the given solution, any additional field added to the form will need to become another parameter to the post method and have corresponding manual model binding. I feel that, though the problem goes away with this method, it is not truly informative about the underlying problem. – stephen.vakil Aug 30 '16 at 15:47
  • 1
    @stephen.vakil I agree. I was just simply using the code that the OP provided and honestly after looking at code all day I tend to forget the basics, especially getters and setters – Grizzly Aug 30 '16 at 15:51