0

I have two forms, one inherited from the first, with mostly the same content except for one combobox called cboMedioPago that is unique to the child form. There´s a method that, any time a value changes on the form, changes a final value. In the child form I need to override this method as i need to take into consideration the value of the combobox.

    protected override void ActualizarPrecio()
    {
        float precio = Producto.productos[this.cboListaProductos.SelectedIndex].Precio * (int)numCantidad.Value;
        if (cboMedioPago.Text == "Crédito")
        {
            precio *= (float)1.1;
        }
        this.lblTotal.Text = "$ " + precio.ToString();
    }

The problem is that whenever i call this child form, i get the mentioned error when trying to fetch the value of cboMedioPago.

Im getting this error: 'Object reference not set to an instance of an object.'

How can I fix this?

Paulo
  • 79
  • 8
  • Do you know how to debug your code? If so, put a breakpoint on the first line of `ActualizarPrecio` (if this is the method, where the error occurs) and when it's hit inspect all variables/properties, which are followed by a `.` and check which one has a value of `null` (probably `cboMedioPago`). – Jürgen Röhr Oct 01 '22 at 19:56
  • 1
    @JürgenRöhr Very likelyy, OP said: "_I get the mentioned error when trying to fetch the value of cboMedioPago_" – Cleptus Oct 01 '22 at 20:18
  • @Cleptus You're right. I should read more throroughly. But some truth remains: inspect `this` (which is followed by a `.`). What's the type? Are you in the correct instance? Maybe you can add the definition aof both forms (at least the parts, which are used in the method). – Jürgen Röhr Oct 01 '22 at 21:53
  • @JürgenRöhr yes, it´s actually null at that point. The issue is that this is the only method in which I cannot access the value of cboMedioPago.Text. I think it has to do with the fact that it´s an override method? Let me know if I can share any more code to help solving this. – Paulo Oct 01 '22 at 23:17
  • @Paulo No, to use protected methods is a feature of C# and will not result in the given behaviour. As `ActualizarPrecio` in the derived form is overridden, there is a virtual counterpart in the base form. Is the error really occurring "whenever" you call the method or does it occur, when the virtual method on the base class is called maybe from construtor? To provide some mvp, I'd suggest, you reduce your app so that only the discussed forms and their usages are contained (even remove controls from the forms). If it still is too much code to post here, you could create and link a github repo. – Jürgen Röhr Oct 02 '22 at 07:31

0 Answers0