Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

I Need help with error handling

Posted on 2011-09-07
5
Medium Priority
?
189 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: 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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

609 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