473,395 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Handling Object Null Reference Errors

Instead of .Net showing an Object reference not set to an instance of
an object error message. Is their a method of handling these messages
and displaying a more user friendly message and recording this
information for further investigation?

Mar 22 '07 #1
4 1398
<ro*******@hotmail.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
Instead of .Net showing an Object reference not set to an instance of
an object error message. Is their a method of handling these messages
and displaying a more user friendly message and recording this
information for further investigation?
if (MyObject == null)
{
// do something
}
Mar 22 '07 #2
You could also look into custom error pages to catch all your unexpected
errors and log them to a database while showing a friendly message.

However you should also do what Mark alluded to, and try and write more
robust code. Never assume a method is going to return an object, always
check that it does.

<ro*******@hotmail.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
Instead of .Net showing an Object reference not set to an instance of
an object error message. Is their a method of handling these messages
and displaying a more user friendly message and recording this
information for further investigation?

Mar 22 '07 #3
See : http://support.microsoft.com/kb/306355/en-us
(HOW TO: Create Custom Error Reporting Pages in ASP.NET by Using Visual C#
..NET), applies as well to VB.NET...
---
Patrice

<ro*******@hotmail.coma écrit dans le message de news:
11**********************@y80g2000hsf.googlegroups. com...
Instead of .Net showing an Object reference not set to an instance of
an object error message. Is their a method of handling these messages
and displaying a more user friendly message and recording this
information for further investigation?

Mar 22 '07 #4

Not really.

As others have already said:

Write better/more robust code.
Employee e = Something.GetEmployee();
if(null!=e)
{
//do something with e
}

OR

Employee e = Something.GetEmployee();
if(null==e)
{
throw new ArgumentException ("Employee was expected to be found, but was
not");
}

ArgumentException can/should be replaced with your own custom exception,
like
EmployeeNotFoundException : ApplicationException

...

The better you code, the better your debugging time and maintenance efforts
will be.

...

I just had this happen at work.
I had coded up some xpath/xml code.

I was told "yeah, the xyz attribute will always be there".

I coded to search for the attribute (and its value).
However, I coded it to throw an exception if this attribute was NOT found.

2 days later.
"Your code is blowing up"
"Oh really?"

I check the code, and its working perfectly.
I was like "You said it would always have the xyz attribute".
"It does have the xyz attribute.

We check the Xml.

He had
Xyz in the xml.
Xml is case sensitive.

"Oh , I didn't know xml was case sensitive".

So it was a quick fix. Mainly because I had anticipated the situation, and
wrote the code to handle (or throw an exception) if something was wrong.

It didn't take hours of debugging because I got some "object was null"
exception.

Better code always means faster debugging and maintenance times.

<ro*******@hotmail.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
Instead of .Net showing an Object reference not set to an instance of
an object error message. Is their a method of handling these messages
and displaying a more user friendly message and recording this
information for further investigation?

Mar 22 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Christian Christmann | last post by:
Hi, assert and error handling can be used for similar purposes. When should one use assert instead of try/catch and in which cases the error handling is preferable? I've read somewhere that...
2
by: xAvailx | last post by:
I have a requirement that requires detection of rows deleted/updated by other processes. My business objects call stored procedures to create, read, update, delete data in a SQL Server 2000 data...
2
by: Steve Jorgensen | last post by:
When writing VB or VBA code that works with databases or other external libraries that cannot be trusted to automatically do the right thing when references to their objects are arbitrarily...
34
by: rawCoder | last post by:
I have read that Exception Handling is expensive performance wise ( other than Throw ) , but exactly how ? Please consider the following example ... ////////////////// Code Block 1...
5
by: Michael | last post by:
Hello, I have a separate Database class that handles any database work that all my asp.net pages can use. My problem is, many times I use try/catch to catch errors, and I want to output these...
6
by: RonL | last post by:
What is the recommended best technique for handling errors/exceptions in ASP.Net. I've read about the following techniques: 1. Try/Catch 2. Page_Error 3. Application_Error in the...
4
by: Rob | last post by:
Hey all, So.. a simple FormView/SqlDataSource to handle inserting records into a table. The table has a primary key that the user enters (eg DiscountCode). If the user enters a duplicate the...
1
by: metsys | last post by:
We have an ASP.NET 2.0 (C#) application that is divided into multiple layers. The multiple layers come from having a web project and 2 different class library projects in the same solution. I'm...
8
by: RichardOnRails | last post by:
I have a Stack class that works fine. In particular, when it encounters an error, it cout's a msg and exits. However, I'd like to change it to report the error and continue with dummy data as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.