hey maniac84,
it seemed your code is working fine; the error message "Object reference not set to an instance of an object" is presumably the Exception (error) caught by your catch statement.
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
to show your user defined error message instead, simple replace the "ex.Message" parameter of the MessageBox.Show() method with a string of your defined error message.
that is,
catch (Exception ex)
{
MessageBox.Show("you defined error message", "Error");
}
Hope this is helpful? Don't hesitate to ask for clarifications if you are still confused!