0

I'm having an issue. I've developed a simple CMS using ASP.NET Web Pages 3.2 Framework, it's developed in Razor C#. The CMS works fine when I'm testing using the system, but I've now extracted it to a website which will be using this CMS, so I generated the tables and all, and it should all work. Everything seems to work, except for the code below. It works completely fine in the CMS itself, but when using it on the other website, it comes up with an error saying "Object reference not set to an instance of an object", and it highlights my array.

I am somehow suspecting it has something to do with the way I generated the scripts etc., but I don't see how I could have done that wrong. I've been trying to fix the issue, but I just can't seem to figure out what's wrong, and it's driving me mad.

<table class="accounts-table">
    <tr>
        <th>Username</th>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Role</th>
        <th>Actions</th>
    </tr>
    @foreach (var row in db.Users)
    {
        string[] roleName = Roles.GetRolesForUser(row.UserName);
        <tr>
            <td>@row.UserName</td>
            <td>@row.FirstName</td>
            <td>@row.LastName</td>
            <td>@foreach (var role in roleName) { @role } </td>
            <td>
                <a href="~/Admin/Pages/Accounts/View?UserID=@row.UserId" title="View"><span class="fa fa-user"></span></a>
                <a href="~/Admin/Pages/Accounts/Edit?UserID=@row.UserId" title="Edit"><span class="fa fa-edit"></span></a>
                <a href="~/Admin/Pages/Accounts/Delete?UserID=@row.UserId" title="Delete"><span class="fa fa-remove"></span></a>
                <a href="~/Admin/Pages/Accounts/Lock?UserID=@row.UserId" title="Lock"><span class="fa fa-lock"></span></a>
            </td>
        </tr>
    }
</table>
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Mikkel
  • 1,853
  • 1
  • 15
  • 31

1 Answers1

0

Object reference not set to an instance of an object exception is raised when you are trying to use a class member that is null.

Since your array is highlighted, suggest you make sure that any elements of the array are initialized before you use them.

Bee Dev
  • 69
  • 1
  • 4
  • I see. The class member shouldn't be null though, considering roles does exist? Also, how come it works in the original CMS files, but doesn't work after uploading the CMS to a site and integrating it with it? Also - how would I properly initialize the array before using it? – Mikkel Oct 13 '14 at 23:11
  • I'm not able to pin the problem specifically but the problem as I stated is that u r using an object that is NULL for some reason. – Bee Dev Oct 13 '14 at 23:37