weird problem

Started by
8 comments, last by erissian 17 years, 5 months ago
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; }
Advertisement
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,
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
I've seen politer ways of asking.

//default constructor
private: Adventure * it;

//overloaded
Adventure * hug = new Adventure(txtMainText);

There error says undefined value. Again, this is only when I use the default constructor.
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.
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
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.

It's not that you asked for more information, it's how you asked. But that's not the point here.
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.
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
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.
Well, an uninitialized pointer points nowhere, and is null by definition.

Glad it works.

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

[Edited by - erissian on November 9, 2006 5:29:42 PM]
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
just beat it out of me, will you? jk. thank you erissian
You're welcome.
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)

This topic is closed to new replies.

Advertisement