nullreferenceexception

How did I get this NullReferenceException error here right after the constructor?

╄→гoц情女王★ 提交于 2019-11-27 07:23:09
I've had an asp.net website running live on our intranet for a couple of weeks now. I just got an email from my application_error emailer method with an unhandled exception. Here it is (I've cleaned up some of the paths to make it better displayed) Exception : Object reference not set to an instance of an object. Stack Trace : at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value) at TimesheetDomain.DataMappers.StaffMemberData.ReadStaff(SqlDataReader reader) in TimesheetDomain\DataMappers

ReSharper: how to remove “Possible 'System.NullReferenceException'” warning

独自空忆成欢 提交于 2019-11-27 06:43:23
问题 Here is a piece of code: IUser user = managerUser.GetUserById(UserId); if ( user==null ) throw new Exception(...); Quote quote = new Quote(user.FullName, user.Email); Everything is fine here. But if I replace "if" line with the following one: ComponentException<MyUserManagerException>.FailIfTrue(user == null, "Can't find user with Id=" + UserId); where function implementation is following: public abstract class ComponentException<T> : ComponentException where T : ComponentException, new() {

Checking if an object is null in C#

孤人 提交于 2019-11-27 06:03:19
I would like to prevent further processing on an object if it is null. In the following code I check if the object is null by either: if (!data.Equals(null)) and if (data != null) However, I receive a NullReferenceException at dataList.Add(data) . If the object was null, it should never have even entered the if -statement! Thus, I'm asking if this is proper way of checking if an object is null: public List<Object> dataList; public bool AddData(ref Object data) bool success = false; try { // I've also used "if (data != null)" which hasn't worked either if (!data.Equals(null)) { /

avoiding null reference exceptions

痴心易碎 提交于 2019-11-27 03:51:12
Apparently the vast majority of errors in code are null reference exceptions. Are there any general techniques to avoid encountering null reference errors? Unless I am mistaken, I am aware that in languages such as F# is it not possible to have a null value. But thats not the question, I'm asking how to avoid null reference errors in languages such as C#. David Allen When a null reference exception is displayed to the user, this indicates a defect in the code resulting from an error on the part of the developer. Here are some ideas on how to prevent these errors. My top recommendation for

How do I enforce null checking? [duplicate]

你离开我真会死。 提交于 2019-11-27 02:41:07
问题 This question already has answers here : avoiding null reference exceptions (15 answers) Closed 20 days ago . I'm working on a large project where, even with 10s of 1000s of automated tests and 100% code coverage, we're getting a ridiculous number of errors. About 95% of errors we get are NullReferenceExceptions. Is there any way to enforce null-checking at compile time? Barring that, is there any way to automagically enforce null-checking in unit tests without having to write the tests for

How Can a Stack Trace Point to the Wrong Line (the “return” Statement) - 40 Lines Off

蹲街弑〆低调 提交于 2019-11-27 01:34:15
问题 I have twice now seen a NullReferenceException logged from a Production ASP.NET MVC 4 web application - and logged on the wrong line. Not wrong by a line or two (like you would get with a PDB mismatch), but wrong by the length of the entire controller action. Example: public ActionResult Index() { var someObject = GetObjectFromService(); if (someObject.SomeProperty == "X") { // NullReferenceException here if someObject == null // do something } // about 40 more lines of code return View(); //

Why is casting a dynamic of type object to object throwing a null reference exception?

雨燕双飞 提交于 2019-11-27 00:05:38
问题 I have the following function: public static T TryGetArrayValue<T>(object[] array_, int index_) { ... //some checking goes up here not relevant to question dynamic boxed = array_[index_]; return (T)boxed; } When I call it in the following way, object a = new object(); object v = TUtils.TryGetArrayValue<object>(new object[] { a }, 0); (T)boxed throws a null reference exception. Any other type I put in there other than "object", it works perfectly fine. Any ideas what this is, and why it's

httpcontext.current.server.mappath Object reference not set to an instance of an object

只愿长相守 提交于 2019-11-26 22:49:38
问题 I am using the following code within a class: string filePath = HttpContext.Current.Server.MapPath("~/email/teste.html"); The file teste.html is in the folder But when it will open the file the following error is being generated: Object reference not set to an instance of an object. 回答1: Don't use Server.MapPath. It's slow. Use this instead, HttpRuntime.AppDomainAppPath . As long as your web site is running, this property is always available to you. Then use it like this: string filePath =

LINQ InsertOnSubmit: NullReferenceException

我是研究僧i 提交于 2019-11-26 21:47:03
问题 I have this code: using DC = MV6DataContext; using MV6; // Business Logic Layer // ... public DC.MV6DataContext dc = new DC.MV6DataContext(ConnectionString); IP ip = new IP(Request.UserHostAddress); dc.IPs.InsertOnSubmit(ip); dc.SubmitChanges(); // in Business Logic layer: public class IP : DC.IP { public IP(string address) { ... } } Upon attempting to InsertOnSubmit(ip), I get a NullReferenceException (Object reference not set to an instance of an object). dc is not null; ip and all

Error checking for NULL in VBScript

若如初见. 提交于 2019-11-26 20:49:12
问题 I have the following VBScript in a Classic ASP page: function getMagicLink(fromWhere, provider) dim url url = "magic.asp?fromwhere=" & fromWhere If Not provider is Nothing Then ' Error occurs here url = url & "&provider=" & provider End if getMagicLink = "<a target='_blank' href='" & url & "'>" & number & "</a>" end function I keep getting an "Object Required" error messager on the line that says If Not provider Is Nothing Then . Either the value is NULL, or it's not NULL, so why am I getting