nullreferenceexception

NullReferenceException when calling InsertOnSubmit in Linq to Sql

和自甴很熟 提交于 2019-12-19 10:25:49
问题 I'm trying to insert a new object into my database using LINQ to SQL but get a NullReferenceException when I call InsertOnSubmit() in the code snippet below. I'm passing in a derived class called FileUploadAudit, and all properties on the object are set. public void Save(Audit audit) { try { using (ULNDataClassesDataContext dataContext = this.Connection.GetContext()) { if (audit.AuditID > 0) { throw new RepositoryException(RepositoryExceptionCode.EntityAlreadyExists, string.Format("An audit

How to check for null in nested references

断了今生、忘了曾经 提交于 2019-12-19 02:26:07
问题 Looking for some best-practice guidance. Let's say I have a line of code like this: Color color = someOrder.Customer.LastOrder.Product.Color; where Customer, LastOrder, Product, and Color could be null under normal conditions. I'd like color to be null if any one of the objects in the path is null, however; in order to avoid null reference exceptions, I'd need to check for the null condition for each one of the objects, e.g. Color color = someOrder == null || someOrder.Customer == null ||

Can't detect whether Session variable exists

落花浮王杯 提交于 2019-12-18 20:05:15
问题 I'm trying to determine if a Session variable exists, but I'm getting the error: System.NullReferenceException: Object reference not set to an instance of an object. Code: // Check if the "company_path" exists in the Session context if (System.Web.HttpContext.Current.Session["company_path"].ToString() != null) { // Session exists, set it company_path = System.Web.HttpContext.Current.Session["company_path"].ToString(); } else { // Session doesn't exist, set it to the default company_path = "

Why would finding a type's initializer throw a NullReferenceException?

大城市里の小女人 提交于 2019-12-18 09:57:09
问题 This has got me stumped. I was trying to optimize some tests for Noda Time, where we have some type initializer checking. I thought I'd find out whether a type has a type initializer (static constructor or static variables with initializers) before loading everything into a new AppDomain . To my surprise, a small test of this threw NullReferenceException - despite there being no null values in my code. It only throws the exception when compiled with no debug information. Here's a short but

Null Reference Exception when calling an Object Array

房东的猫 提交于 2019-12-18 05:26:07
问题 I tried to get some help earlier and I don't think I provided enough information though I appreciate all the advice. The goal is simply to add a new instance of the Object Room to an array and print to a list box. When a user attempts to enter a room name that is already in existence it should simply display in the specs for the room that is already in the array. I keep getting a null reference exception. Here is my code: using System; using System.Collections.Generic; using System

VS2012: Property Pages isn't opening: Object reference not set to an instance of an object

雨燕双飞 提交于 2019-12-18 04:30:16
问题 I'm on Visual Studio Premium 2012, Version 11.0.50727.1 RTMREL, Windows 7 Enterprise (SP1). When I right click on the Solution, and click Properties, I get a popup window showing the the null reference exception (Object reference not set to an instance of an object). When I try the same in 2010, it works fine, opening up the normal solution properties pages, allowing me to set the startup order etc. Also, in 2012, View -> Property Pages, gives the same result. Please advise how to solve this,

Can Visual Studio tell me which reference threw a NullReferenceException?

喜你入骨 提交于 2019-12-18 04:05:49
问题 I'm writing unit tests for an MVC web app, and I've been getting null reference exceptions because the mocked-up test objects are only partly initialized. I know which line is throwing the exceptions, and it looks something like this: return Supervisor.RegistrationInformation.Registrations .Any(r => r.RegistrationCountry.IsUSAOrCandada() && (!DatesWorked.Start.HasValue || r.RegistrationDate <= DatesWorked.Start.Value) && (!DatesWorked.End.HasValue || r.RegistrationExpirationDate >=

When would SqlCommand.ExecuteReader() return null?

穿精又带淫゛_ 提交于 2019-12-17 20:44:21
问题 When using calling the SqlCommand.ExecuteReader() method, ReSharper tells me I have a possible NullReference exception when I use the SqlDataReader object afterwards. So with the following code: using (SqlConnection connection = GetConnection()) { using (SqlCommand cmd = connection.CreateCommand()) { cmd.CommandText = ; //snip using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { //snip } } } } The while (reader.Read()) line is underlined. My question is when would the

How to use global var across files in a package?

℡╲_俬逩灬. 提交于 2019-12-17 07:23:40
问题 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

Javascript nested dictionary null values

*爱你&永不变心* 提交于 2019-12-14 03:16:01
问题 Below I have declared a 'class' in JS and am writing methods for it. The class represents a widget (more info class structure). The problem I'm having is that when I print 'this' inside the setAttr ibutes method, the output is: And when I print 'this.opts' in the very next line: NOTE : The values of 'status' and 'power' are shown as 'null' in the second output before expanding it. This can be the only possible reason behind getting 'null' while trying to print 'this.opts.status'. CODE