-2

I have already visited this question on StackOverflow but I didn't get any solution to my problem.

In my Windows Form Application, I have included a feature which allows a user to double click a row in the DataGridView and the values from that specific row are shown in text boxes in a child form. The user can now edit these values i.e. can edit quantity etc.

Now, I want to assign this value (the edited quantity) back to that specific cell of the current row of the same DataGridView. I have these values from the child form back on my main form but as I am re-assigning this edited quantity value to the DataGridView in a public method, so I am getting a Nulled Reference Exception on the DataGridView.

Here is my code where I am getting the NulledReferenceException.

//A public method to which I have passed the customQuantity from my child form
public void GetSoldItemQuantity(string customQuantity)
{
    //assign this customQuantity to an int variable
    int globalQuantity = Convert.ToInt32(customQuantity);
    if (globalQuantity > 1)
    {
     //dgvAllSoldItems is the DataGridView from where I have passed 
     //the currentRow data to the child form. At this stage,
     //I have the updated value returned from my child form and is
     //currently stored in the globalQuantity variable but I cannot assign it
     //back to the grid.

        dgvAllSoldItems.CurrentRow.Cells[1].Value = globalQuantity;

    }
}

And here is the button code on my child form which passes the custom quantity value to the above public method on the parent form.

private void btnProceedWithNewItemQuantity_Click(object sender, EventArgs e)
{
    string newItemQuantity = txtChooseSaleQuantity.Text.Trim();

    frmNewSale parentSale = this.Parent as frmNewSale();//now I am getting error the null reference exception on this line in debugger.
    parentSale.GetSoldItemQuantity(newItemQuantity);

    this.Close();
}

Although I was creating a new instance of the parent class on the button click event on child form, which was not the right approach. So, I used this.Parent instead, but now I am getting the same exception on this line.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Noor
  • 185
  • 2
  • 18
  • @RandRandom I have mentioned the same question on the top. It didn't helped me! – Noor Mar 03 '19 at 10:35
  • Please @Noor the duplicate explain practically every possible scenario. You just need to start your debugger, put a breakpoint in the first line of your function, press f10 and check what variable is null. Then start to investigate why that variable is null. It is easy – Steve Mar 03 '19 at 10:38
  • 2
    Noticed, but it's still a duplicate for the NRE - sorry, but you don't even mention what is null on the line that is throwing the exception - it seems to me you lack the knowledge to read an error. So what is null on that line `dgvAllSoldItems.CurrentRow.Cells[1].Value` is it the `dgvAllSoldItems` ? is it the `CurrentRow` ? is it `Cells` is the `[1]` returning null? - first figure out what is null and than think why it could be in your situation – Rand Random Mar 03 '19 at 10:38
  • I have inspected my problem, and in debugger, the dgvAllSoldItems.CurrentRow is null – Noor Mar 03 '19 at 10:40
  • By the way, if the problem is the grid itself then it is an example of a bad design of your function. Why don't you return the calculated value and let the caller of this function decide what to do with the result and with the objects they have responsability to handle (creation, destroy, changing etc...) – Steve Mar 03 '19 at 10:42
  • I need access back to my DataGridView in this public method. I will then find back my current row and will assign this value to that specific cell. I simply need access to dgvAllSoldItems – Noor Mar 03 '19 at 10:42
  • 1
    And now try to understand why the `CurrentRow` of a `DataGrid` could be null - here is a good starting Point https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview.currentrow - `The DataGridViewRow that represents the row containing the current cell, or null if there is no current cell.` Maybe you would prefer `SelectedRows` - https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview.selectedrows – Rand Random Mar 03 '19 at 10:45
  • Deleting one bad question only to ask the exact same thing again really doesn't help. I'm glad you've now taken a bit of time to find out what's null. I suggest you edit the question to make it clear what you still don't know: why `dgvAllSoldItems.CurrentRow` is null. The `NullReferenceException` itself isn't a mystery - you're dereferencing a null value. – Jon Skeet Mar 03 '19 at 12:52
  • @JonSkeet I have updated my code – Noor Mar 03 '19 at 13:29
  • 1
    I'm confused - are you now saying that the `GetSoldItemQuantity` method is irrelevant? What do you see in the debugger? What is the value of `this.Parent?` To be honest, the code you've posted - `this.Parent as frmNewSale()` - wouldn't even compile - so I'm pretty dubious about it... – Jon Skeet Mar 03 '19 at 14:15
  • Can only agree with @JonSkeet your current code won't compile - so its impossible you face a `NullReferenceException` on that line – Rand Random Mar 04 '19 at 08:39

1 Answers1

-1

The reason you are seeing the error is that you have not defined dvgAllSoldItems, or is not visible, inside the function GetSoldItemQuantity.

Since I am not sure of the context of your function GetSoldItemQuantity. I can think of one solution for you to reference your GridView object. That is to pass the GridView object into the function such as below:

GetSoldItemQuantity(string customQuantity,  DataGridView dgvAllSoldItems)

or if you have a view model populating the form, you can return the customQuantity value and assign it into the model as the new quantity value.

If dvgAllSoldItems is a global variable (or found within the same class as your GetSoldItemQuantity function), then you can use this.dvgAllSoldItems to reference the globally defined object.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
Ian Preglo
  • 421
  • 2
  • 10
  • `dvgAllSoldItems` must be visible in `GetSoldItemQuantity` for the code to compile, run, and then give the exception. If `dvgAllSoldItems` is found in the same class as `GetSoldItemQuantity` then adding `this.` does nothing (unless there was ambiguity with a local variable, which there isn't). – Enigmativity Mar 03 '19 at 11:01