Thread: "Object reference not set to an instance of an object"

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    13

    Unhappy "Object reference not set to an instance of an object"

    Ok, those words have haunted me all weekend. I thought I was doing pretty good with things until I hit this thing. Unfortunately the code is on my laptop at home so I can't show you, but I'm hoping it's just something simple anyway that I'm just missing/forgetting/unaware of.

    I've got this class, and some of the members are arrays. I've got arrays of ints that are old style and I've got arrays of Strings that are __gc. At first it started giving me the error and pointing to one of the old school arrays and I realized I hadn't initialized or whatever (sorry, the vocab is still growing on me.) any of the arrays. So I went back and did the ints, looked up the proper way to do the Strings and did them, then I ran again. This time it got all the way to the last part where I'm trying to assign values to the elements of the String array. I've tried commenting out that assignment and I still get the error and it points me to the closing bracket of the function which makes me think there must be something wholly different wrong with things. I read the help file description of the error and played around with it for quite a chunk of time and I still don't see where I'm going wrong.

    I understand I've not given you much to work with but any help or insight would be appreciated.
    "Why not go mad?"

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    based solely on the compiler's message, i would assume its something like this:

    class T {
    ....
    };


    T &T_ref; // T is some object

    you might have declared a reference to an object without giving the reference something to refer to...

    T myT;
    T & T_ref = myT; // here T_ref has something to refer to

    this is just a guess...

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    13
    Ok, I spent another couple hours at the library last night playing around with this and I have a couple other bits of info:

    After commenting out various parts and running it, the problem seems to center on an array of String*s (I may have suspected this before, but I'm sure of it now). I declared it in the class as:

    String* names[];

    Now I've tried instantiating/initializing (not sure what the proper word is) in several places like so:

    String* names[] = new String*[4];

    and I've found that if I do that in every function/method that uses the array, I don't get any errors, BUT that doesn't help me since the purpose of the array is to store a person's first name, last name, and hometown. Doesn't help me much if I have to start a new one every time I run something. I mean, even my GetFname() method, which I just want to return names[0], gives me a problem if I don't create a new one.

    Originally I had that line in the class constructor, and I watched the program run through breakpoints and the array seemed to disappear after the constructor finished.

    If I don't get a solution today, I'm thinking of just rewriting the thing using individual String*s. Maybe that's the better way to do it anyway, but I'd really like to know what I've done wrong here.
    "Why not go mad?"

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    its kinda hard to figure out what you are trying to do from that declaration,but the declaration itself doesnt seem right.

    to declare a string you would obviously do

    string s;

    so, to declare an array of strings you would do

    string s[<number>];

    to declare a pointer to a(n array of) string(s):

    string *s;

    ... an array of pointers to strings...

    string *s[<number>];

    or a pointer to a pointer to a string...

    stirng **s;

    with the last one you can dynamically allocate an array of string pointers, which seems to most closely match what you are trying to do

    string **s = new string *[200];

    s[0] = new string("bob");
    cout << *(s[0]) << endl; // not sure of precedence
    delete s[0];
    delete[] s;

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Originally I had that line in the class constructor, and I watched the program run through breakpoints and the array seemed to disappear after the constructor finished.
    If I understand correctly, you declared 'names' in the constructor? If you did so, it is no doubt going to disappear, because when the constructor ends, 'names' goes out of scope! To keep it from disappearing, you can do this:

    Code:
    #include <iostream>
    
    class T
    {
    public:
         T();
    protected:
         String **names;
    };
    
    T::T()
    {
         names = new String*[4];
    }
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    13
    ClownPimp:
    The Strings I'm dealing with are these .net Strings. I went by the book (well, by the book I have) as far as my syntax goes. I'm about ready to just go back and do this thing with plain old C++ Strings just out of frustration, but then the trouble becomes how do I get one of those into the Text part of a .net TextBox.

    Hunter2:
    Sorry I didn't explain it that well, but what your describing is what I did.

    I don't mean to be difficult but I swear it seems like I've done everything as I should. I think I'm just going to hit the books again and retry this thing with MFC.

    Thanks though. I appreciate the replies!
    "Why not go mad?"

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    No prob compiler errors were never my specialty anyways.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 04:54 PM
  2. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2002, 07:40 PM
  3. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM
  4. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM