-3

I have an object defined as below:

public class MappingObject {
        public SelectList cSelectlist { get; set; }
        public SelectList sSelectlist { get; set; }
}

This object is passed to a view successfully. I get a null pointer error when I try to read data in my post method as below.

public ActionResult Edit(MappingObject mo)  {
    foreach(SelectListItem cc in mo.cSelectlist){
        Debug.WriteLine("Selected value is " +cc.Text);
    }
    return View();
}

View code is :

@using myProject.Models;

@model myProject.Models.MappingObject
@{ 
    SelectList consList = Model.cSelectlist;
    SelectList scopeList = Model.sSelectlist;
}

And I am using MVC framework.

Aluan Haddad
  • 29,886
  • 8
  • 72
  • 84
anand
  • 3
  • 4
  • 1
    check if mo object in Edit action has a value and if yes, Check cc object in your foreach loop – G_S Feb 18 '18 at 04:39
  • 1
    Do you mean `NullReferenceException`? – Erik Philips Feb 18 '18 at 04:44
  • 1
    When you say *I get an error*, the very next thing you should provide in your question is the **exact error message** you're seeing. It's on the screen right in front of you, and there is absolutely no reason you should not provide it so we have that information as well. – Ken White Feb 18 '18 at 04:48
  • You don't even say what framework you are using. How would anyone know what is going on? – Aluan Haddad Feb 18 '18 at 04:54
  • 2
    definitely the object you are passing is null thats why you are getting 'NullReferencException' check your object that you are passing. – Hafiz Hamza Feb 18 '18 at 04:58
  • 2
    Check your object you are passing in method. Run you IDE in debugging mode or provide adequate information. – Hafiz Hamza Feb 18 '18 at 05:00
  • I am using MVC framework. – anand Feb 18 '18 at 05:14
  • @anand which version of that framework? How are your routes setup? And don't say it in comments, say it in the question! – Aluan Haddad Feb 18 '18 at 05:16

1 Answers1

-3

To avoid unexpected errors as a quick fix, you can do below.

public ActionResult Edit(MappingObject mo)  {
  if(mo != null && mo.cSelectlist != null){
    foreach(SelectListItem cc in mo.cSelectlist){
       if(cc != null){
        Debug.WriteLine("Selected value is " +cc.Text);
       }  
    }
   }
  return View();
}

Based on your code, we can check if something is causing an issue. I am pretty sure there is something else that's causing the issue. Make sure to fix that aswell without leaving your code if it works with the above fix.

G_S
  • 7,068
  • 2
  • 21
  • 51
  • 3
    solution looks to be hiding some issue at one end and making it more nasty bug later on. I simply mean `(!=null)` check to fix null reference exception is not right solution. – rahulaga-msft Feb 18 '18 at 04:52
  • Agreed. And that's the reason I added a statement, "Based on your code, we can check if something is causing an issue." We have to identify the root cause rather than doing this quick fix. – G_S Feb 18 '18 at 04:53
  • Guys I didnt say this is the fix. I just said its a quick fix. Also I added a statement "Based on your code, we can check if something is causing an issue." – G_S Feb 18 '18 at 04:56
  • `mo` can also be null, right ? how quick fix will catch that ? – rahulaga-msft Feb 18 '18 at 04:58
  • That condition is already added as the first check – G_S Feb 18 '18 at 04:58
  • Hi All, the object is coming from the request posted. how do I make sure the object is not null. Here is my edit view for more details. using myProject.Models; model myProject.Models.MappingObject { SelectList consList = Model.cSelectlist; SelectList scopeList = Model.sSelectlist; } – anand Feb 18 '18 at 05:11
  • @anand add any information to your question, not in a comment on an answer. – Aluan Haddad Feb 18 '18 at 05:14