Results 1 to 5 of 5

Thread: Object reference not set to an instance of an object error c#

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2009
    Location
    Manila, Phillippines
    Posts
    54

    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,

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    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.

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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:
    1. {
    2.  
    3.             betPosCol = new string[5]; // gives you elements 0-4...
    4.  
    5.             betPosCol[0] = "Red";
    6.             betPosCol[1] = "Black";
    7.             betPosCol[2] = "Red";
    8.             betPosCol[3] = "Black";
    9.             betPosCol[4] = "Red";
    10.         }

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    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:
    1. 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
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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
  •  



Click Here to Expand Forum to Full Width