winforms 如何消除此代码中的空引用问题

rdrgkggo  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(117)

帮助我删除空引用在线-(player = new player()),或者你也可以建议你的代码在c#窗口窗体应用程序中使用flyleaflib制作一个RTSP播放器。

using System;
using System.Windows.Forms;
using FlyleafLib;
using FlyleafLib.MediaFramework.MediaPlaylist;
using FlyleafLib.MediaPlayer;

namespace MultiPagesWInForm. {
  public partial class Form1: Form {
    Player player;
    public Form1() {
      InitializeComponent();
      player = new Player();
    }

    private void Form1_Load(object sender, EventArgs e) {

    }

    private void btnPlay_Click(object sender, EventArgs e) {
      try {
        if (player != null) {

          String videoUrl = "http://pendelcam.kip.uni-heidelberg.de/mjpg/video.mjpg";
          player.Open(videoUrl);
          player.Play();
        } else {
          MessageBox.Show("The 'player' instance is null. Make sure it's properly initialized.");
        }
      } catch (Exception ex) {
        MessageBox.Show("An exception occurred: " + ex.Message);
      }
    }
    private void btnStop_Click(object sender, EventArgs e) {
      if (player != null) {
        player.Stop();
      }
    }
  }
}

字符串
我在我的问题中提到了所有的细节.

tkclm6bt

tkclm6bt1#

Player对象第一次声明时,它也需要初始化。如果将Player player;更改为Player player = new();并删除player = new Player(),应该可以解决null异常。
记住还要在对象上设置访问修饰符(private、public等)。

相关问题