0

So I am working on an MVC n-tier layer application in C# and I am getting a NullReferenceExeption. What the program should do in short: The user can type a character kind like for example Defensive, Offensive, etc. This string is passed into the controller and made a parameter. Next, this parameter should be passed to the logic layer. Here it should pass the parameter to the DAL. In the DAL the parameter is used in the query to get certain data from the database. This data should send back to the logic layer and from the logic layer, it should be displayed on the screen of the user.

I get the error in my Logic layer (I commented in the logic layer where the error happens precisely.

The passing of the parameter is not the problem, because I know through breakpoints that the value of inputKarakterSoort is the given value what the user typed.

My DAL should return data, so I don't know why I get a NullReferenceExeption. My controller gets a string from the user, makes the given string into a parameter and this parameter is used in the DAL for the execution of the query.

This is my DAL:

public IEnumerable<IKarakter> GetSortedKarakters(string inputKarakterSoort)            {
            using (SqlConnection connection = GetConnection())
            {
                connection.Open();
                var command = new SqlCommand("SELECT TOP 2 * FROM Karakter WHERE KarakterSoort = @inputKarakterSoort ORDER BY NEWID();", connection);
                command.Parameters.Add("@inputKarakterSoort", SqlDbType.VarChar).Value = inputKarakterSoort;
                var reader = command.ExecuteReader();
                var sortedKarakters = new List<IKarakter>();
                while (reader.Read())
                {
                    var karakter = new KarakterDTO
                    {
                        KarakterId = (int)reader["KarakterId"],
                        KarakterSoort = reader["KarakterSoort"]?.ToString(),
                        KarakterNaam = reader["KarakterNaam"]?.ToString()
                    };
                    sortedKarakters.Add(karakter);
                }
                return sortedKarakters;
            }
        }

My Logic Layer (With the error in it):

public class SortedKarakterLogic : ISortedKarakterLogic
    {
        private ISortedKarakterContext sortedKarakterContext { get; }
        private string _inputKarakterSoort { get; set; }
        public SortedKarakterLogic(ISortedKarakterContext sortedContext)
    {
        sortedKarakterContext = sortedContext;
    }

        public SortedKarakterLogic(string inputKarakterSoort)
        {
            _inputKarakterSoort = inputKarakterSoort;
        }

        public IEnumerable<IKarakter> GetSortedKarakters(string inputKarakterSoort)
        {
            
            return sortedKarakterContext.GetSortedKarakters(inputKarakterSoort);
//The error is on the line above, sortedKarakterContext is equal to null, but I don't know why or how.
        }
    }

My Controller:

public ActionResult SortedKarakter()
        {
            return View();
        }

        [HttpPost]
        public ActionResult SortedKarakter(string karakterSoort)
        {
            SortedKarakterLogic karakterLogic = new SortedKarakterLogic(karakterSoort);
            karakterLogic.GetSortedKarakters(karakterSoort);
            ViewBag.Name = string.Format("{0}", karakterSoort);
            var sortedKarakters = _sortedKarakterLogic.GetSortedKarakters(karakterSoort);
            var _sortedKarakters = new List<KarakterViewModel>();
            foreach (var karakter in sortedKarakters)
            {
                _sortedKarakters.Add(new KarakterViewModel
                {
                    KarakterId = karakter.KarakterId,
                    KarakterNaam = karakter.KarakterNaam,
                    KarakterSoort = karakterSoort
                });
            }
            
            return View(_sortedKarakters);
        }

I get the string via the ViewBag,

If you need to see an interface or some other class, just ask in the comments and I will upload the file to the question. Can anyone help me? Thanks in advance!

Note: I can't use breakpoints in my DAL, because it does not even go to there. It just errors when it runs through the logic layer.

  • 1
    "My DAL should return data, so I don't know why I get a NullReferenceExeption." - so have you stepped through the code in a debugger, checking the results at each step? Where does the code not behave as you'd expect it to? Where is the exception being thrown? – Jon Skeet Nov 10 '20 at 11:39
  • @JonSkeet I edited the question so now is clear where the error is. I expect it to return the data from the SortedKarakterContext.GetSortedKarakters method. –  Nov 10 '20 at 11:46
  • Hint: when you call the `SortedKarakterLogic(string)` constructor, how do you expect that to populate the `sortedKarakterContext` field? – Jon Skeet Nov 10 '20 at 12:03
  • @JonSkeet I understand now why it does not work, but can't figure out what to do. Can you help me (or link a site where my problem is described)? –  Nov 10 '20 at 12:20
  • Not really, because we don't know why you've set up your `SortedKarakterLogic` that way. It looks like it's sort of trying to do two different jobs, hence two different constructors. I suspect you should redesign it - I'd expect your controller constructor to accept an `ISortedKarakterLogic` as a dependency in the constructor, rather than creating it directly itself. – Jon Skeet Nov 10 '20 at 13:31

0 Answers0