Public Group

# weird problem

This topic is 4266 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a weird problem, or at least it is to me but I'm new to managed C++. The problem occurs in the setTextBox function, some reason I can't assign txtBox to txtBoxForPrint if I use a default constructor but I have no problems with the overloaded constructor. Yet the code for assigning txtBox to txtBoxForPrint is exactly the same. any ideas? thanks. Adventure::Adventure(System::Windows::Forms::RichTextBox * txtBox) { time = 0; textToType = false; txtBoxForPrint = txtBox; } void Adventure::setTextBox(System::Windows::Forms::RichTextBox * txtBox) { txtBoxForPrint = txtBox; }

##### Share on other sites
Although I could try to guess what the actual error message is, and how you declare your variables, and other minutia, it would save us both time if you went ahead and posted it anyways,

##### Share on other sites
I've seen politer ways of asking.

//default constructor

There error says undefined value. Again, this is only when I use the default constructor.

##### Share on other sites
If you've seen if asked politely before, you should have known to do it. You'll still need to post more information.

What are the types for txtBoxForPrint and txtBox? What exactly is the error? Are you using a particular library for this code that needs mentioning?

Be thorough in your description of the problem. Or be vague, I don't care at this point. I'm sure that somebody willing to drag out every detail with a smile on their face will happen by. I, on the other hand, won't offend your sensibilities again with my mean-spirited 'need more information from people asking for help with vague questions' attitude.

##### Share on other sites
It should be obvious what type txtBox is, it's in the parameter System::Windows::Forms::RichTextBox *. And the same for txtBoxForPrint.
It's managed code, which I've never really dealt with. I'm still pretty new to C++.

The error says "An unhandled exception of type 'System.NullReferenceException' occurred in textAdventure.exe

Additional information: Object reference not set to an instance of an object."
and the value for txtBoxForPrint is undefined value. Oh, and txtBox for some odd reason is out of scope.

##### Share on other sites
Especially in the beginner's forum, I can't assume txtBoxForPrint is a RichTextBox. For all I know, you're trying to assign *txtBox to a float. It wouldn't be the first time I saw that.

Also, undefined value is a lot different than null reference.

Now that I know that this is a runtime error, and not a compile time error, we can look somewhere else. The problem is likely not with the code you've written, but how you're using it.

I need you to do two things:

1. Post the code where you call adventure. Not an example of the constructor, but the actual block of code that you call it from, as well as anything relevant to the variables you use to initialize it.

2. I need you to wrap your code in a try-catch block so you can get more information.
try {  // Instantiation goes here}catch (System.NullReferenceException e) {  Console.WriteLine ("Here's the problem: ",e);}

(Which may or may not be the proper way to do it in managed C++)

Also post the output from that line.

##### Share on other sites
aww shit, you figured it out by making me thinking about when the constructor is being called. When I originally wrote it I was thinking of just Adventure it; but then I made it managed for a reason that I don't need now and that made me change it into Adventure * it; yet some reason I kept thinking of it as Adventure it (compiler yelled, I made it happy, and didn't think about it again) and never allocated an instance of Adventure on the heap. Which mean the constructor never was called and IT indeed did point to an undefined value. I kept thinking the error had something to do with managed code that I wasn't familiar with.

I wonder why the error said System.NullReferenceException. Unless managed C++ initializes pointers to null. I've tried to assign one to null and it seemed to not like that. I don't know.

##### Share on other sites
Well, an uninitialized pointer points nowhere, and is null by definition.

Edit: No thank you? What happened to politeness? :)

[Edited by - erissian on November 9, 2006 5:29:42 PM]

##### Share on other sites
just beat it out of me, will you? jk. thank you erissian

You're welcome.

1. 1
2. 2
Rutin
19
3. 3
JoeJ
16
4. 4
5. 5

• 36
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631703
• Total Posts
3001814
×