Hello everyone, I'm rather new at C# and programming in general, I have some experience with Java, PERL, and Python but I wouldn't consider myself an expert on any of them. I'm using C# because it is very similar to Java and it seems to be even easier to make GUIs with it.
That being said I'm currently stuck with my program. This is my third C# program and so far I've been able to iron out most of the bugs except for one. I'm getting the runtime error: System.NullReferenceException: Object reference not set to an instance of an object. I've tried everything I know of to try and fix it and I've looked up the error and I believe that I basically understand it but I still can't fix it. If you could lend any help it would be very much appreciated.
Here is the code that I believe to be the most relevant to the problem but it is not all of my code. If you would like to see all my code I would be happy to post it as well.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Media;
namespace MKV2MP4
{
public partial class MKV2MP4 : Form
{
public string mkvLoc;
public string vidLoc;
public string tcLoc;
public string audioLoc;
public string chapLoc;
public string outLoc;
public string currentDirectory = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().Location);
public MKV2MP4()
{
InitializeComponent();
}
//Method that calls method that gives error
private void button1_Click(object sender, EventArgs e)
{
string fileName;
string filePath;
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Select MKV Source";
ofd.CheckFileExists = true;
ofd.CheckPathExists = true;
ofd.Filter = "MKV File (*.mkv)|*.mkv|" + "All files (*.*)|*.*";
ofd.AddExtension = true;
ofd.DefaultExt = "mkv";
if (ofd.ShowDialog() == DialogResult.OK)
{
fileName = System.IO.Path.GetFileName(ofd.FileName);
filePath = System.IO.Path.GetDirectoryName(ofd.FileName);
mkvLoc = System.IO.Path.Combine(filePath, fileName);
readStreams(mkvLoc); //Actual call to method that gives error
mkvLocBox.Text = mkvLoc;
}
else
{
MessageBox.Show("Warning: No video file selected");
}
}
//Method that gives error
private void readStreams(string mkvLoc) //Gives error: Object reference not set to an instance of an object
{
try
{
Process read = new Process();
read.StartInfo.WorkingDirectory = currentDirectory + @"\MKVtoolnix\";
read.StartInfo.FileName = "mkvmerge.exe";
read.StartInfo.RedirectStandardOutput = true;
read.StartInfo.RedirectStandardInput = true;
read.StartInfo.UseShellExecute = false;
read.StartInfo.CreateNoWindow = true;
read.StartInfo.Arguments = String.Format("-i \"{0}\"", mkvLoc);
read.Start();
StreamReader myStreamReader = read.StandardOutput;
string[] streams = new string[1000];
string isMkvTest = myStreamReader.ReadLine();
string isMkv = isMkvTest.Substring(isMkvTest.LastIndexOf(':'));
if (isMkv == ": Matroska")
{
for (int i = 1; i < 1000; i++)
{
string s = myStreamReader.ReadLine();
if (s.Trim().Length == 0)
break;
streams[i] = s;
}
read.Close();
//Send streams[] to streamList
streamList.Items.AddRange(streams);
}
else
{
read.Close();
MessageBox.Show("File is not a valid Matroska file.");
}
}
catch(Exception e)
{
MessageBox.Show("Error: " + e.StackTrace);
}
}
}
}
Again thank you for any help you can give me. I am not a software developer it is just a hobby to help with my own computing needs, and therefore I don't have advanced knowledge in programming. Also if you could look at my for loop for reading another programs output and tell me if there is a better way to do it.