0

I was required to upgrade my OS to Windows 10. As a result I had re-install Visual Studio 2015. It was working fine before but now when I try and debug I get:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

if (ModelState.IsValid)
{
  login_model.IntoNetUser = new IntoNetUser(Person.GetByCsuid(login_model.LocalUserCsuid).Pidm);
}

There is a value in "LocalUserCsuid". Is there possibly something I need to run for a Windows 10 installation or an additional setting that needs set to get my previously working application, working now.

Dave Hogan
  • 3,201
  • 6
  • 29
  • 54
  • 1
    Please check this [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) – alipseight Mar 03 '17 at 20:07
  • 1
    It's unlikely that the re-install 'caused' the null reference exception you're encountering. The first step is to figure out what reference is null. Can you attach a debugger and peek into the object references and view their properties? – Dave Hogan Mar 03 '17 at 20:09
  • Inspect the GetByCsuid method and find out how it gets data. I don't think you will need extra installations. Can you locate your application's datasource? – G.Anıl Yalçın Mar 03 '17 at 20:13

2 Answers2

0

The NullReference Exception for Visual Basic is no different from the one in C#. After all, they are both reporting the same exception defined in the .NET Framework which they both use. Causes unique to Visual Basic are rare (perhaps only one).

This answer will use Visual Basic terms, syntax and context. The examples used come from a large number of past Stack Overflow questions. This is to maximize relevance by using the kinds of situations often seen in posts. A bit more explanation is also provided for those who might need it. An example similar to yours is very likely listed here.

Note:

This is concept-based: there is no code for you to paste into your project. It is intended to help you understand what causes a NullReferenceException (NRE), how to find it, how to fix it, and how to avoid it. An NRE can be caused many ways so this is unlikely to be your sole encounter.
The examples (from Stack Overflow posts) do not always show the best way to do something in the first place.
Typically, the simplest remedy is used.

Basic Meaning

The message "Object not set to instance of Object" means you are trying to use an object which has not been initialized. This boils down to one of these:

Your code declared an object variable, but it did not initialize it (create an instance or 'instantiate' it)
Something which your code assumed would initialize an object, did not
Possibly, other code prematurely invalidated an object still in use

Finding The Cause

Since the problem is an object reference which is Nothing, the answer is to examine them to find out which one. Then determine why it is not initialized. Hold the mouse over the various variables and Visual Studio (VS) will show their values - the culprit will be Nothing.

IDE debug display

You should also remove any Try/Catch blocks from the relevant code, especially ones where there is nothing in the Catch block. This will cause your code to crash when it tries to use an object which is Nothing. This is what you want, because it will identify the exact location of the problem, and allow you to identify the object causing it.

A MsgBox in the Catch which displays Error while... will be of little help. This method also leads to very bad Stack Overflow questions, because the you can't describe the actual exception, the object involved or even the line of code where it happens.

You can also use the Locals Window (Debug -> Windows -> Locals) to examine your objects.

Once you know what and where the problem is, it is usually fairly easy to fix and faster than posting a new question.

0

One of two things (or possibly even both) is happening:

  1. The login_model itself is null for some reason

  2. Your call to Person.GetByCsuid(login_model.LocalUserCsuid) is returning null for some reason

You'll have to dig deeper to figure out which scenario it is, and the underlying reason WHY you're now getting a null instead of the value that your code was getting before your upgrade.

Joe Irby
  • 649
  • 6
  • 7