Solved

console

Posted on 2003-11-26
2
476 Views
Last Modified: 2012-05-07
Help me outttt



I'm gettin this error message when i tried to run my program :


Unhandled Exception: System.NullReferenceException: Object reference not set to
an instance of an object.
   at ConLib.SetBackgroundColor(ConLib* , UInt16 Color) in d:\documents and sett
ings\tejo\desktop\codes\test conlib\onlib.cpp:line 22
   at main() in d:\documents and settings\tejo\desktop\codes\test conlib\main.cp
p:line 12
Press any key to continue


what is this error telling me?





here is my code from main.cpp:


      ConLib* test;
      test->SetBackgroundColor(0);

and here is my code in my function SetBackGroundColor(WORD Color)

void ConLib::SetBackgroundColor(WORD Color)
{
      m_BackgroundColor = 0 ;

      //combining the colors;
      if(Color & ConRed)
      {
            m_BackgroundColor |= BACKGROUND_RED;
      }

      if(Color & ConGreen)
      {
            m_BackgroundColor |= BACKGROUND_GREEN;
      }

      if(Color & ConBlue)
      {
            m_BackgroundColor |= BACKGROUND_BLUE;
      }

      SetConsoleTextAttribute (m_Screen, m_TextColor | m_BackgroundColor);

}


pls help me out. i'm a begginner in using this console thing.
My platform is windows and i'm using Visual C++ .NET

Thank you.
0
Comment
Question by:wilslm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Accepted Solution

by:
PlanetCpp earned 500 total points
ID: 9829690
it means you tried to access an object from a pointer that wasnt pointing to anything.
you need to use
ConLib *test = new ConLib;
or
ConLib *test;
then before you use it
test = new ConLib;
if there are paramters for the constructor just add them at the end
= new ConLib(param1,param2,etc);
then you can access it using -> or (*test).memberhere
when youre done using it you need to use
delete test;
then to be safe you should use test = NULL;
this way you can test if its there if needed later
0
 
LVL 24

Expert Comment

by:shivsa
ID: 9829691
if an object isn't initialized it will give that sort of error. eg:
if you declare a Form like this....

code:
--------------------------------------------------------------------------------
Dim frm As Form1
frm.Show()
'/// this will erro
r--------------------------------------------------------------------------------

you would get that error because you didnt declare it as a NEW form , or as a CAST of a Form.
the correct ways , depending on what you are doing would be like these...

code:--------------------------------------------------------------------------------
Dim frm As New Form1()
frm.Show()
'///OR
Dim frm As Form1 = DirectCast(Me.Owner , Form1 ) '/// if form1 was the owner of the Form you call from.
frm.Show()
--------------------------------------------------------------------------------

the same applies to all stuff, like Objects etc... , you must initialize them
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question