0

| Framework: .NET Framework 4.5.2 | OS: Windows 10 | IDE: Visual Studio |


I have 4 lines of code in the middle of a C# function.

Hashtable A = new Hashtable();    
Hashtable B = new Hashtable();
int C = 0;
string D = "";

On launching the code in a debugger in Visual Studio, I get an unusual error on the forth line:

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

What is even more stranger is that if I were to remove the line where string D is declared/initialized, the error now shifts to the line above for variable C. The error keeps on shifting to the line before If I now were to remove the lines for variable C then B and eventually A.

What I have already tried.

  1. I tried to replace the string declaration with String.Empty, it still gave the error.
  2. Tried declaring and initializing the variable in separate lines, still got the error.

What I need help on?

  1. Are there any hard limits on the number of variables you can declare in a C# function ?
  2. How to go by solving this issue ! (Of Course)

Edits

I am limited to sharing the screenshot of the part of the code, not the whole function. enter image description here

Dilnoor Singh
  • 518
  • 5
  • 16
  • Could you post the whole function? Also, the whole error? – Juan Medina Mar 12 '21 at 12:51
  • @OP I think with more information about the enviroment (are there maybe interceptors etc.) Noone can help you. I'm quite sure that `string D = "";` by itsself will NEVER cause a runtime error. – Dominik Mar 12 '21 at 12:52
  • 2
    The debugger sometimes displays execeptions at wrong locations. Especially when build optimazations are turned on. None of the lines displayed here can cause a NullReferenceException – derpirscher Mar 12 '21 at 12:53
  • Read the duplicate and debug your code to find out what's going on. It's not `string D = "";`. If that was the case, nobody would be able to use strings. Where is the exception thrown? What is the *actual, full* exception text? An exception includes the file name and line where it was thrown, and the stack trace that led to it. Where is the exception actually thrown? – Panagiotis Kanavos Mar 12 '21 at 12:54
  • Follow the call stack or paste it here to review. These declarations won't throw the error. – Blagojce Mar 12 '21 at 12:55
  • I suggest rearranging the lines to see if the error follows one of them. You can also comment out some lines to see it it goes away. Also, dig into the Hashtable constructor. – n8wrl Mar 12 '21 at 12:57
  • Just uploaded the Error Screenshot. – Dilnoor Singh Mar 12 '21 at 12:57
  • @Panagiotis, I have gone through similar questions for NullReferenceException. None relate to what I asked. – Dilnoor Singh Mar 12 '21 at 12:58
  • `overallHRAScoreCurrent` - is it null? – Fildor Mar 12 '21 at 13:00
  • I see this question was closed because someone felt it already has an answer in another question posted before. I have been stuck on this for a complete day now, and I had already gone through that question before. Can that "Someone" tell me where in that linked question the answer is ? – Dilnoor Singh Mar 12 '21 at 13:02
  • @Fildor, Its not, I hovered over it in Visual Studio, it has a value and so does other variables. – Dilnoor Singh Mar 12 '21 at 13:03
  • 2
    @DilnoorSingh: the duplicate question doesn't tell exactly the answer to your question. But it gives you all tools necessary to find the reason of the error yourself. Also, sometimes it helps to simply rebuild the solution to see the real line that caused the exception. – Tim Schmelter Mar 12 '21 at 13:05
  • @DilnoorSingh post *the exception text*, not screenshots of your debugger. That image proves nothing at all, especially if the exception was thrown in a different line. Click on `View Details` to see the full exception details. Click on `Copy Details` to copy the full text and post it in your question. ` – Panagiotis Kanavos Mar 12 '21 at 13:20
  • I did try to rebuild the solution to no avail. But I was to solve the problem based on @derpirscher comment before. The NullReferenceException was actually coming from somewhere else. Thanks. – Dilnoor Singh Mar 12 '21 at 13:21
  • For anyone bumping into this question in the future, based on @derpirscher suggestions. I turned off Code Optimizations by going to Project Properties > Build > Optimize Code. Relaunched the project and discovered that the line underneath the string declaration(not shown in the screenshot) was legitimately throwing the NullReferenceException error. Fixed it. – Dilnoor Singh Mar 12 '21 at 13:26

0 Answers0