Questions tagged [nullreferenceexception]

The .NET exception that is thrown when there is an attempt to reference (or use) a null or uninitialized object.

A NullReferenceException occurs when you try to reference an object in your code that does not exist. For example, you may have tried to use an object without using the New keyword first, or tried to use an object whose value is set to null (Nothing in Visual Basic).

Troubleshooting Exceptions: System.NullReferenceException

Also, see "What is a NullReferenceException in .NET and how do I fix it?" for some hints.

2780 questions
5
votes
1 answer

How to locate the method that throws "NullReferenceException: 'Object reference not set to an instance of an object'?

I've already checked every similar question on Stackoverflow. None of them either answer or really ask the same question. Is there a way to find the method that attempts to reference said null object? I understand it can't say what object since it…
5
votes
2 answers

Returning null for generic type extension in c#

I have created an extension function, which takes the string as input and check for the value and based on the Generic Type, cast it to the destination type and return it, it's working well. Now the problem is if i pass the input value as empty it…
Abbas
  • 4,948
  • 31
  • 95
  • 161
5
votes
6 answers

Firing an event in C# with no attatched delegate methods?

I've just encountered a bug in the program I'm writing where an exception was thrown stating an "object reference must be set to an instance of an object". Upon investigation, I found that this exception was thrown when trying to fire an event BUT…
James Bedford
  • 28,702
  • 8
  • 57
  • 64
5
votes
1 answer

WPF: NullReferenceException at Automation.Peers.ItemAutomationPeer.GetNameCore

I have a vexed problem and have spent a whole day for it, but still can't fix it. I have a button on WPF UI,it is used to add a item to treeview, the code in xaml is below:
5
votes
2 answers

What is the correct pattern to protect against NullReferenceExceptions in ASP.NET MVC

UPDATE The issue was a syntax issue. @awrigley shows the correct way to write this in Razor. The following works: @if(Model.Thing.Prop != null) { Html.RenderPartial("SomePartialView", Model.Thing.Prop); } You have a requirement to show…
Greg B
  • 14,597
  • 18
  • 87
  • 141
5
votes
1 answer

Sitecore "random" Object reference not set to an instance of an object

I was working fine with sitecore 6.5 when suddenly this morning I came to work, did a get latest version and then this error started [NullReferenceException: Object reference not set to an instance of an object.] …
Yatiac
  • 1,820
  • 3
  • 15
  • 25
5
votes
2 answers

What does "Attempt to Invoke Virtual Method" really mean?

I am a beginner level Android developer and every day is a fight with a lot of errors and their solutions. I have noticed that I quite many times get java.lang.NullPointerException: Attempt to invoke virtual method 'xxx' on a null object reference.…
user1460340
5
votes
3 answers

Why do I get this System.NullReferenceException?

I'm getting a weird error. I have the following front-end code: <%@ Page Title="" Language="C#" MasterPageFile="~/nokernok/MasterPages/nokernok.Master" AutoEventWireup="true" CodeBehind="articleList.aspx.cs"…
Steven
  • 19,224
  • 47
  • 152
  • 257
5
votes
3 answers

Correct syntax to initialize static array

I have following code definig an array public class PalphabetsDic { public static string[] PAlphCodes = new string[3] { PAlphCodes[0] = "1593", PAlphCodes[1] = "1604", PAlphCodes[2] = "1740", }; } When I use…
Ali Padida
  • 1,763
  • 1
  • 16
  • 34
5
votes
1 answer

Properly handling possible System.NullReferenceException in lambda expressions

Here's the query in question return _projectDetail.ExpenditureDetails .Where(detail => detail.ProgramFund == _programFund && detail.Expenditure.User == _creditCardHolder) .Sum(detail => detail.ExpenditureAmounts.FirstOrDefault( …
taco
  • 811
  • 9
  • 16
5
votes
4 answers

Optional Design Pattern, Advantages

So, it's pretty well known that the infamous NullReferenceException is the most common exception in software products. I've been reading some articles, and found myself with the Optional approach. Its aim is to create some kind of encapsulation…
5
votes
2 answers

Member references and assignment order of evaluation

Why does the following crash with a NullReferenceException on the statement a.b.c = LazyInitBAndReturnValue(a);? class A { public B b; } class B { public int c; public int other, various, fields; } class Program { private static…
Matt Kline
  • 10,149
  • 7
  • 50
  • 87
5
votes
3 answers

System.NullReference Exception occurred in App_Web_XXX.dll - Error in Partial View

I'm getting this error, with a random string in the dll name, when landing on one of my MVC pages. A first chance exception of type System.NullReferenceException occurred in App_Web_cjmhrrqn.dll. Additional information: Object reference not…
5
votes
3 answers

How to implement null-safe operator in F#

I was wondering if you can add something like a "null-safe"-operator in F#. I know that there might be plans for such an operator in C# for the next bigger release. If there is no way to achieve such a behaviour, is there a way to wrap a statement…
5
votes
1 answer

EF - Navigation property is null, even after reloading entity, but works when program is restarted

EDIT I've done some testing and found out, that the Item navigation property only works when the context is disposed/a new context is created. DataContext context = new DataContext(); Order ord = context.Orders.FirstOrDefault(); …
Lahey
  • 429
  • 1
  • 6
  • 16