How to remove null reference problem in this code

56 Views Asked by At

Help me for remove null reference getting in line - (player = new player()) or you can also suggest your code for making a RTSP player in c# window form application using flyleaflib.

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();
      }
    }
  }
}

I mention all details in my question .... Please teach me how to do this or you can provide another way to do it but only using flyleaflib...

2

There are 2 best solutions below

0
On

When the Player object is first declared, it also needs to be initialized. If you change Player player; to Player player = new(); and remove player = new Player(), that should resolve your null exception.

Remember to also set your access modifiers on the object (private, public, etc).

0
On

No changing declaration syntax of Player player wont do nothing at all.

Actually you answered your own question with:

MessageBox.Show("The 'player' instance is null. Make sure it's properly initialized.");

Well you instanciated the actual player but not his libraries ex:

MyFlyLeafLib libCOOL = something something;
MyFlyLeafWrapperForThePlayer mediaplayer_ = blabla(libCOOL);
player = mediaplayer_;

Its just an example but you see what i mean.loll