[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

I Need help with error handling

Posted on 2011-09-07
5
Medium Priority
?
190 Views
Last Modified: 2012-05-12
I get the following error in my C#.Net app

Object reference not set to an instance of an object

How can I handle this?
0
Comment
Question by:mainrotor
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36498965
You haven't initialized a variable. Read #1 in my article.
0
 
LVL 2

Expert Comment

by:nafigueiredo
ID: 36499403
Hi

You have to check if your object is null before doing anything with it.

Like:

  if (myobject == null){
. .. do something
} else {
 do what you're supposed to do
}
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 2000 total points
ID: 36500340
You need to call new on your variable before you can use it. Not knowing what type of object you use, I cannot give you a precise example, but here is how it would look for a form:
Form1 frm;
// And somewhere before using the form:
frm = new Form1 ( );

Open in new window

Or if you prefer:
Form1 frm = new Form1 ( );
// Use it when you want

Open in new window

0
 
LVL 15

Expert Comment

by:Navneet
ID: 36500802
Hi!

Your object is not Static and Needs Initialization before using it.
Also read this
http://www.csharp411.com/c-object-initialization/

Thanks!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36502266
Your object is not Static
A static object can still be uninitialized  : )
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question