nullreferenceexception

Using VB.NET IIF I get NullReferenceException

大憨熊 提交于 2019-11-26 20:30:45
问题 I am doing a little debugging, and so I want to log the eventArgs value I have a simple line that basically does: logLine = "e.Value: " + IIf(e.Value Is Nothing, "", e.Value.ToString()) The way I understand the IIF function, if the e.Value is Nothing (null) then it should return the empty string, if not it should return the .ToString of the value. I am, however getting a NullReferenceException. This doesn't make sense to me. Any idea's? 回答1: IIf is an actual function, so all arguments get

Why does this extension method throw a NullReferenceException in VB.NET?

三世轮回 提交于 2019-11-26 20:25:30
问题 From previous experience I had been under the impression that it's perfectly legal (though perhaps not advisable) to call extension methods on a null instance. So in C#, this code compiles and runs: // code in static class static bool IsNull(this object obj) { return obj == null; } // code elsewhere object x = null; bool exists = !x.IsNull(); However, I was just putting together a little suite of example code for the other members of my development team (we just upgraded to .NET 3.5 and I've

Consuming JSON in WCF service method

好久不见. 提交于 2019-11-26 20:16:27
问题 In a larger project I am having trouble getting a WCF service method to consume a JSON parameter. So I produced a smaller test case and the behaviour is echoed. If I debug the service I can see the parameter value is null at the service call. Fiddler confirms that the JSON is being sent and JsonLint confirms it is valid. Code below with annotations from debugging. [ServiceContract] public interface IWCFService { [OperationContract] [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest,

How to use global var across files in a package?

≯℡__Kan透↙ 提交于 2019-11-26 18:53:08
I have the following file structure: models/db.go type DB struct { *sql.DB } var db *DB func init() { dbinfo := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", DB_USER, DB_PASSWORD, DB_NAME) db, err := NewDB(dbinfo) checkErr(err) rows, err := db.Query("SELECT * FROM profile") checkErr(err) fmt.Println(rows) } func NewDB(dataSourceName string) (*DB, error) { db, err := sql.Open("postgres", dataSourceName) if err != nil { return nil, err } if err = db.Ping(); err != nil { return nil, err } return &DB{db}, nil } models/db_util.go func (p *Profile) InsertProfile() { if db != nil { _,

C# elegant way to check if a property's property is null

不打扰是莪最后的温柔 提交于 2019-11-26 15:14:21
In C#, say that you want to pull a value off of PropertyC in this example and ObjectA, PropertyA and PropertyB can all be null. ObjectA.PropertyA.PropertyB.PropertyC How can I get PropertyC safely with the least amount of code? Right now I would check: if(ObjectA != null && ObjectA.PropertyA !=null && ObjectA.PropertyA.PropertyB != null) { // safely pull off the value int value = objectA.PropertyA.PropertyB.PropertyC; } It would be nice to do something more like this (pseudo-code). int value = ObjectA.PropertyA.PropertyB ? ObjectA.PropertyA.PropertyB : defaultVal; Possibly even further

Initializing list property without “new List” causes NullReferenceException

邮差的信 提交于 2019-11-26 14:48:22
问题 using System; using System.Collections.Generic; class Parent { public Child Child { get; set; } } class Child { public List<string> Strings { get; set; } } static class Program { static void Main() { // bad object initialization var parent = new Parent() { Child = { Strings = { "hello", "world" } } }; } } The above program compiles fine, but crashes at runtime with Object reference not set to an instance of the object . If you notice in the above snippet, I have omitted new while initializing

Checking if an object is null in C#

十年热恋 提交于 2019-11-26 11:49:40
问题 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 { //

avoiding null reference exceptions

拈花ヽ惹草 提交于 2019-11-26 09:34:42
问题 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#. 回答1: 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

C# elegant way to check if a property&#39;s property is null

放肆的年华 提交于 2019-11-26 03:09:18
问题 In C#, say that you want to pull a value off of PropertyC in this example and ObjectA, PropertyA and PropertyB can all be null. ObjectA.PropertyA.PropertyB.PropertyC How can I get PropertyC safely with the least amount of code? Right now I would check: if(ObjectA != null && ObjectA.PropertyA !=null && ObjectA.PropertyA.PropertyB != null) { // safely pull off the value int value = objectA.PropertyA.PropertyB.PropertyC; } It would be nice to do something more like this (pseudo-code). int value

What is a NullReferenceException, and how do I fix it?

丶灬走出姿态 提交于 2019-11-25 22:50:45
问题 This post is a Community Wiki . Edit existing answers to improve this post. It is not currently accepting new answers. I have some code and when it executes, it throws a NullReferenceException , saying: Object reference not set to an instance of an object. What does this mean, and what can I do to fix this error? 回答1: What is the cause? Bottom Line You are trying to use something that is null (or Nothing in VB.NET). This means you either set it to null , or you never set it to anything at all