0

In child form I have

public Print()
        {
            InitializeComponent();
        }

public Print(int param)
        {
            this.se.EditValue = param;
        }

and I call it from another form :

private void function
{
     int value = GetValue();//it gets an int value
            if (value > 0)
            {
                using (Print printForm = new Print(value))
                ...
}

I am getting error: System.NullReferenceException: 'Reference to an object not set on an object instance.' How can I fix it?

ati
  • 9
  • 3
  • 2
    Only one constructor is ever called (directly) via `new`. If you want to initialize the UI components, then you also need to call `InitializeComponent()`. Perhaps you could call the parameterless constructor from the `int param` one using `this`? Something like `public Print(int param) : this()` ? – ProgrammingLlama Jul 09 '21 at 08:16
  • This isn't a duplicate of that question imo. The issue here is to do with constructors as pointed out by Llama – Martin Jul 09 '21 at 08:18
  • 2
    @Martin it is a duplicate because literally every null reference exception is solved the same way; by debugging your code and finding what is null, and then fixing that. In this case it is indeed because the parameterless constructor was not called. – Jamiec Jul 09 '21 at 08:31
  • @Jamiec I've seen a lot of quetions with same error, but I couldn't figure it out ... – ati Jul 09 '21 at 08:40
  • @ati It's a skill you'll learn as time goes on. But for the most part theyre impossible to answer without stepping through your code (This was a much simpler example) – Jamiec Jul 09 '21 at 08:47

0 Answers0