6 Replies - 1412 Views - Last Post: 20 January 2013 - 08:59 AM Rate Topic: -----

#1 teyrebaz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 14-January 12

Object reference not set to an instance of an object

Posted 20 January 2013 - 08:22 AM

Hello all,
I am jumping into issue directly.. I have two forms;
First one is saving packages for rooms.. (its not neccessary for what, just describing)
Second one is listing rooms in datagridview ..
I have two constructor for First Form ;
        public PackageForm()
        {
            InitializeComponent();
            OdalarListele();
        }
        public PackageForm(int roomid)
        {
            roomID = roomid;
            cmbRoomNo.SelectedValue = roomID ;

        }


the variable "roomid" delivering from Second Forms datagridview.. (when user double-click to datagridview);

int roomID = Convert.ToInt32(dgvRooms.CurrentRow.Cells[0].Value);
PackageForm pf = new PackageForm(roomID);
this.Close();

at the first form when i assign "roomID to cmbRoomNo.SelectedValue" an error throws;

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

Please, help me!

This post has been edited by tlhIn`toq: 20 January 2013 - 08:28 AM
Reason for edit:: [code] your code here [/code] tags added


Is This A Good Question/Topic? 0
  • +

Replies To: Object reference not set to an instance of an object

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5441
  • View blog
  • Posts: 11,674
  • Joined: 02-June 10

Re: Object reference not set to an instance of an object

Posted 20 January 2013 - 08:28 AM

First error covered in the "what is does this error mean" tutorial.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Jan 2013
Spoiler



Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5441
  • View blog
  • Posts: 11,674
  • Joined: 02-June 10

Re: Object reference not set to an instance of an object

Posted 20 January 2013 - 08:37 AM

Quote

public PackageForm(int roomid)
{
roomID = roomid;
cmbRoomNo.SelectedValue = roomID ;

}


If you have a property of roomID, then you should use it to set your other items. Rather than setting the roomID and then having to set the combobox value - each and every time you set the roomID - you should have the roomID property set the combobox. That way you have a single point of code to change if you want to change the behavior for when the roomID is set

int roomID
{
    {get return _roomID;}
    set
    {
        cboRoomID.SelectedIndex = cboRoomID.IndexOf(value);
        RaiseLogThis("Room changed to: " + value);
        SetStatusBar("Room: " + value);
        AdjustRoomRates(value);
     }
}


If you aren't familiar with properties there is a tutorial linked in my signature block

You have a big misunderstanding here

Quote

the variable "roomid" delivering from Second Forms datagridview.. (when user double-click to datagridview);

int roomID = Convert.ToInt32(dgvRooms.CurrentRow.Cells[0].Value);
PackageForm pf = new PackageForm(roomID);
this.Close();


You're making a NEW PackageForm with that roomId. Then doing nothing with it. Its lost. That's probably why the object is null later.

Seems like you don't understand the idea of instances of classes or don't realize that you can have numerous copies of that form but they are all different instances.

You might want to read through the Objects and Classes tutorial linked in my signature block.
Was This Post Helpful? 1
  • +
  • -

#4 teyrebaz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 14-January 12

Re: Object reference not set to an instance of an object

Posted 20 January 2013 - 08:47 AM

roomID hasnt property, its just global variable that i can use everywhere in class. I could use "roomid" which is delivering from constructor parameter. I tried to do assigning roomNo to textbox text field but i got same error.
Was This Post Helpful? 0
  • +
  • -

#5 teyrebaz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 14-January 12

Re: Object reference not set to an instance of an object

Posted 20 January 2013 - 08:52 AM

Thank you. You are helpful guy. Yes, i did nothing with instance of First form. But i dont know how to pass value :(.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5441
  • View blog
  • Posts: 11,674
  • Joined: 02-June 10

Re: Object reference not set to an instance of an object

Posted 20 January 2013 - 08:54 AM

Quote

roomID hasnt property, its just global variable

Then take 5 seconds and fix it. That was the point of the suggestion.

Quote

But i dont know how to pass value

Read back a couple replies to that long FAQ list I gave you and read FAQ 3 and do those tutorials. They are there to teach you how to have your two forms talk to each other.
Was This Post Helpful? 1
  • +
  • -

#7 teyrebaz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 14-January 12

Re: Object reference not set to an instance of an object

Posted 20 January 2013 - 08:59 AM

Ok, i am reading .. thank you very much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1