• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

I Need help with error handling

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
mainrotor
Asked:
mainrotor
1 Solution
 
käµfm³d 👽Commented:
You haven't initialized a variable. Read #1 in my article.
0
 
nafigueiredoCommented:
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
 
Jacques Bourgeois (James Burger)PresidentCommented:
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
 
NavneetCommented:
Hi!

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

Thanks!
0
 
käµfm³d 👽Commented:
Your object is not Static
A static object can still be uninitialized  : )
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now