|
-
Jan 14th, 2013, 07:31 AM
#1
Thread Starter
Member
Object reference not set to an instance of an object error c#
Hi Expert,
I am a beginner in visual c#, i have a problem while running this code string
Code:
namespace sampleprocess
{
public partial class main : Form
{
public main()
{
InitializeComponent();
}
private string[] betPosCol;
private unsafe void setArrays()
{
betPosCol[1] = "Red";
betPosCol[2] = "Black";
betPosCol[3] = "Red";
betPosCol[4] = "Black";
betPosCol[5] = "Red";
}
}
}
the troubleshooter said that : use "new" keyword to create an object instance
Please help me what is wrong with my code
Regards,
-
Jan 14th, 2013, 07:36 AM
#2
Re: Object reference not set to an instance of an object error c#
this is the vb.net forum, the c# forum is at http://www.vbforums.com/forumdisplay.php?30-C
you're trying to use elements in an un dimensioned array.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 14th, 2013, 08:16 AM
#3
Re: Object reference not set to an instance of an object error c#
Next time just report your thread and ask that it be moved... now you have duplicate posts...
http://www.vbforums.com/showthread.p...object-error-c
The mods have been alerted to it, and will probably merge them at some point.
mean while... while you have declared an array, you haven't specified how big the array is to be... it's not initialized.
all this does:
private string[] betPosCol;
is to tell C# "Hey, I need a box"....
you never created the box.
Code:
betPosCol = new string[]; // while I think you can get away with that, it's a good idea to specify the size of the array if you can...
c# Code:
{ betPosCol = new string[5]; // gives you elements 0-4... betPosCol[0] = "Red"; betPosCol[1] = "Black"; betPosCol[2] = "Red"; betPosCol[3] = "Black"; betPosCol[4] = "Red"; }
-tg
-
Jan 14th, 2013, 09:15 AM
#4
Re: Object reference not set to an instance of an object error c#
When you have something like this:
Code:
private string[] betPosCol;
The error is here when you try to assign elements to the incidies.
Code:
betPosCol[1] = "Red";
betPosCol[2] = "Black";
betPosCol[3] = "Red";
betPosCol[4] = "Black";
betPosCol[5] = "Red";
You need to somehow show how much space you want to allocate on the heap (unless you're using stackalloc here which isn't the case), for the number of elements of that type you will be holding in the array. *Note: I'll assume 5 since you only have 5 strings.
csharp Code:
private string[] betPosCol = new string[5];
This is wrong though too:
Code:
betPosCol[1] = "Red";
Not syntax-wise, but arrays are based on a 0-based indexing system. First index you should be assigning should be 0, not 1. This means that since you have 5 strings, the last index should be 4 for the last element in the array, and you should be assigning indexes [0], [1], [2], [3], [4]. (Not 1, 2, 3, 4, 5).
Cheers 
Ace
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Jan 14th, 2013, 12:23 PM
#5
Re: Object reference not set to an instance of an object error c#
Thanks for the report tg.....Duplicate threads merged
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|