0

I am creating a simple media player app and I am encountering a strange issue...

Second Window

If you look to the top right I have a close button, and to the bottom right , a cancel button. Both serve the same function ( to close this window ) .

The code handling this is as follows ( I added both button's click event to the exact same event handler )

        private void ExitApp_Click(object sender, EventArgs e) {
            Close();
        }

EDIT : Adding entire code

using System;
using System.IO;
using System.Windows.Forms;

namespace AnimePlayer {
    public partial class AddAnime : Form {
        public AddAnime() {
            InitializeComponent();
        }

        private void ExitApp_Click(object sender, EventArgs e) {
            this.Close();
        }

        private void bunifuThinButton21_Click(object sender, EventArgs e) {
            if (AnimeLocation.ShowDialog() == DialogResult.OK) {
                AnimeLocationLabel.Text = AnimeLocation.SelectedPath;
            }
        }

        private void bunifuThinButton22_Click(object sender, EventArgs e) {
            if (PictureLocation.ShowDialog() == DialogResult.OK) {
                PictureLocationLabel.Text = Path.GetFullPath(PictureLocation.FileName);
            }
        }

        private void bunifuFlatButton1_Click(object sender, EventArgs e) {
            // Create and save anime object
        }
    }
}

All works perfectly as intended when clicking the top right cross, the window closes and returns to the main window.

This is not the case when using the bottom right button, which instead throws a NullReferenceException on Program.cs here :

        Application.Run(new Main());

I have tried using seperate event handlers, Dispose() instead of Close() but the result stays the same.

Any help will be greatly appreciated ( I'm pretty sure this is just something I am not aware of )

Kazuto Kirigaya
  • 83
  • 1
  • 1
  • 13
  • Do you have some event handler in your form related to the form closing? Some thread running in the background? Too little context to understand what's happening here. – Steve May 16 '17 at 18:00
  • 2
    Somewhere, you've got a null reference. You're trying to do something with one of its members, but it's null, so you get this exception. Find out where. Put in a breakpoint. Look at the stack trace from the exception. Use the tools at your disposal to gather information and find out where the exception is being thrown. Then fix the issue. – 15ee8f99-57ff-4f92-890c-b56153 May 16 '17 at 18:00
  • @Steve Please see updated post, I'm not sure if this answers your question fully but hopefully adds more context to the question. – Kazuto Kirigaya May 16 '17 at 18:07
  • You aren't providing any code that shows us what would cause your null exception. – LarsTech May 16 '17 at 18:11
  • 2
    set visual studio to break when null ref exception is thrown – pm100 May 16 '17 at 18:18
  • You probably subscribe cancelButton to another click event somewhere in the code and it does the wrong thing. However, I fully agree with @pm100 on how to fix it. – Ivan Ičin May 16 '17 at 18:46

0 Answers0