Loading multiple files Exception error C# - c#
I am making the game minesweeper and I am trying to implement a highScores feature. I am trying to load 3 different files (each one holds the high scores for each of the 3 difficulty settings) into 3 different richTextBox's. When I run the app and click the 'high scores' tab from the menu strip it works the first time. However if I play a game and then try to access the high scores form I get an Exception error -
An unhandled exception of type 'System.IO.IOException' occurred in
mscorlib.dll
Additional information: The process cannot access the file
'C:\Users\jzcon_000\Copy\Visual
Studio\Projects\Assignment1\Assignment1\bin\Debug\highScoresMed.txt'
because it is being used by another process
This is where the call is made
private void highScoresToolStripMenuItem_Click(object sender, EventArgs e)
{
Minesweeper.HighSc highScore = new Minesweeper.HighSc();
highScore.read();
highScore.Show();
}
This is the method in my HighSc class
public void read()
{
StreamReader readerE = File.OpenText("highScoresEasy.txt");
StreamReader readerM = File.OpenText("highScoresMed.txt");
StreamReader readerH = File.OpenText("highScoresHard.txt");
if (readerE != null)
{
string readEasy = File.ReadAllText("highScoresEasy.txt");
richTextBox1.Text = readEasy;
}
readerE.Close();
if (readerM != null)
{
string readMed = File.ReadAllText("highScoresMed.txt");
richTextBox2.Text = readMed;
}
readerM.Close();
if (readerH != null)
{
string readHard = File.ReadAllText("highScoresHard.txt");
richTextBox3.Text = readHard;
}
readerH.Close();
}
Heres the save high scores class
namespace Minesweeper
{
class Save
{
int diff, hr, min, sec;
string player;
public Save(int difficulty, int hour, int minute, int second, string playerN)
{
diff = difficulty;
hr = hour;
min = minute;
sec = second;
player = playerN;
}
public void save()
{
StreamWriter writerEasy = new StreamWriter("highScoresEasy.txt", true);
StreamWriter writerMed = new StreamWriter("highScoresMed.txt", true);
StreamWriter writerHard = new StreamWriter("highScoresHard.txt", true);
if (diff == 1)
{
writerEasy.WriteLine("Time: " + hr + ":" + min + ":" + sec + " " + "Name: " + player);
writerEasy.Close();
}
else if (diff == 2)
{
writerMed.WriteLine("Time: " + hr + ":" + min + ":" + sec + " " + "Name: " + player);
writerMed.Close();
}
else if (diff == 3)
{
writerHard.WriteLine("Time: " + hr + ":" + min + ":" + sec + " " + "Name: " + player);
writerHard.Close();
}
}
}
}
So when you have the difficulty level set to 1 and then save, you open the StreamWriters for both the level2 and level3 but never close them.
This could only mean that when you try to load the highscore for these two levels you will find your files locked by your previous save.
You should change your Save method to open only the required file
public void save()
{
if (diff == 1)
{
using(StreamWriter writerEasy = new StreamWriter("highScoresEasy.txt", true))
{
writerEasy.WriteLine("Time: " + hr + ":" + min + ":" + sec + " " + "Name: " + player);
}
}
else if (diff == 2)
....
else if (diff == 3)
....
I suggest also to use the using statement in your reading method to be sure that also in case of exceptions the stream are correctly disposed
Related
Why does the next button take so long to be pressed?
Ok quite simply I have a selenium script in C# and you choose a name from the dropdown box, choose the dates and counts the number of results page by page. Now can someone help me speed this process because it almost takes 5 seconds for the next button to be clicked to go on the next page. Here is my code: int entityCount = 0; int totalCount = 0; try { System.Threading.Thread.Sleep(5000); while (_AuditRep.nextButton.Enabled && _AuditRep.nextButton.Displayed) { System.Threading.Thread.Sleep(5000); _AuditRep.nextButton.Click(); entityCount += _AuditRep.AuditResultsByEntity("Users").Count; totalCount += _AuditRep.AuditTotalResults.Count; } if (entityCount != totalCount) { Assert.Fail("The count of Drivers is " + entityCount + " whereas " + "the total count is " + totalCount); } else { Console.WriteLine("The count of entity Drivers is " + entityCount + " and " + "the total count is " + totalCount); } } catch { if (_AuditRep.AuditResultsByEntity("Users").Count != _AuditRep.AuditTotalResults.Count) { Assert.Fail("The count of Drivers is " + _AuditRep.AuditResultsByEntity("Users").Count + " whereas " + "the total count is " + _AuditRep.AuditTotalResults.Count); } else { Console.WriteLine("The count of entity Drivers is " + _AuditRep.AuditResultsByEntity("Users").Count + " and " + "the total count is " + _AuditRep.AuditTotalResults.Count); } }
Discord.NET Users playing the same game
I am trying to create a command that returns the number of guild members that are currently playing a specified game. Example (! is my prefix): !playing League of Legends. If there are 5 members playing League of Legends, output: There are 5 users currently playing League of Legends. I set up the following, from debugging I was able to pick up that v.Game.toString() returns the correct string but for some reason the if statement does not trigger. It also catches an exception that is thrown whenever members are not playing a game (I assumed it's null?), is there a workaround for that? Why does this not count how many members are playing a certain game? [Command("playing")] public async Task playingGame(params string[] s) { string gameName = ""; int count = 0; for (int i = 0; i < s.Length; i++) { gameName += s[i] + " "; } await Context.Channel.SendMessageAsync("Looking for: " + gameName); var u = Context.Guild.Users; foreach (var v in u) { await Context.Channel.SendMessageAsync("v = " + v.ToString()); try { await Context.Channel.SendMessageAsync(v.Game.ToString()); if (v.Game.ToString().ToLower().Equals(gameName.ToLower())) { count++; await Context.Channel.SendMessageAsync("Found match, count = " + count); } } catch (Exception x) { await Context.Channel.SendMessageAsync("Exception throw caught"); } } if (count > 1) { await Context.Channel.SendMessageAsync("There are " + count + " users currently playing " + gameName + "."); } else if (count == 1) { await Context.Channel.SendMessageAsync("There is " + count + " user currently playing " + gameName + "."); } else if (count == 0) { await Context.Channel.SendMessageAsync("No one is currently playing " + gameName + "."); } } This is the exception: System.ArgumentException: Argument cannot be blank Parameter name: Content at Discord.Preconditions.NotNullOrEmpty(String obj, String name, String msg) at Discord.API.DiscordRestApiClient.<CreateMessageAsync>d__77.MoveNext() --- End of stack trace from previous location where exception was thrown --- Pic of where if statement should trigger (username blocked for privacy reasons):
There is no need to have params string[] s for the game parameter. Just use the Remainder attribute. I have also simplified this code alot : [Command("playing")] public async Task GetUsersPlaying([Remainder]string game) { await Context.Message.DeleteAsync(); var users = Context.Guild.Users.Where(x => x.Game.ToString() == game).Distinct().Select(x => x.Username); var count = users.Count(); var SeparatedList = string.Join(", ", users); string message; if (count > 1) message = $"There are {count} users playing {game}. [{SeparatedList}]"; else if (count == 1) message = $"There is {count} user playing {game}. [{SeparatedList}]"; else message = $"There is no one playing {game}."; await Context.Channel.SendMessageAsync(message); }
System.IO.File.WriteAllText outputs gibberish C#
I have a simple program in C# that is supposed to output a text file containing a sequence of character separated by commas. I also output the resulting sequence on the console and it looks fine, however, the text file is full of weird character and no commas. This is the output : 㔳㌬ⰵⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰴⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰴⰴⰰⰰⰰⰰⰳⰰⰰⰰⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰰⰰⰰⰰⰴⰰⰰⰰⰳⰰⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰰⰰⰰⰰⰴⰴⰴⰰⰰⰰⰰⰰⰰⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰴⰴⰴⰰⰰⰰⰰⰰⰰⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰰⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰲⰱⰱⰱⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰱⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰲⰰⰳⰱⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰱⰱⰱⰱⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰰⰰⰰⰰⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰰⰰⰳⰰⰴⰴⰴⰰⰰⰰⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰰⰰⰰⰰⰴⰴⰴⰰⰰⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰱⰱⰱⰱⰱⰱⰲⰰⰲⰱⰱⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰱⰰⰰⰰⰰⰰⰰⰰⰰⰰⰱⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰱⰰⰰⰰⰰⰰⰵⰰⰵⰰⰱⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰲⰰⰰⰰⰰⰰⰰⰰⰰⰰⰱⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰵⰰⰵⰰⰱⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰲⰰⰰⰵⰰⰰⰰⰰⰰⰰⰱⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰰⰰⰰⰱⰰⰰⰰⰰⰰⰰⰰⰰⰰⰱⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰵⰰⰰⰰⰴⰴⰰⰰⰰⰰⰱⰱⰱⰱⰱⰱⰰⰰⰱⰱⰱⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰵⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰰⰰⰴⰴⰴⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰵⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰵⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰴⰴⰴⰰⰰⰰⰰⰰⰴⰴⰴⰴⰴⰴⰴⰰⰰⰵⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰴⰴⰴⰴⰴⰰⰰⰳⰰⰴⰴⰴⰴⰴⰴⰴⰰⰰⰰⰰⰰⰵⰰⰰⰰⰴⰴⰴⰴⰰⰰⰴⰴⰴⰴⰴⰴⰴⰴⰰⰰⰰⰰⰰⰴⰴⰴⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰴⰰⰰⰴⰴⰴⰴⰴⰴⰴⰴⰴⰰⰰⰰⰴⰴⰴⰴⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴ This is what is supposed to be outputted 35,35,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,4,4,4,4,4,4,4,4,4,4,0,0,0,0,0,0,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,4,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,4,4,4,4,4,4,0,0,0,0,3,0,0,0,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,4, 4,0,0,0,0,4,0,0,0,3,0,4,4,4,0,0,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, As I said, what is supposed to be outputted is the string that is directly passed into File.WriteAllText. Am I missing something ? I don't understand what i'm doing wrong. It seems to be working sometimes, but with other string it does not. I can find no relation between the sequences that works and those that do not. Here is the whole code, it's a simple program that read a bitmap and output the sequence depending on the pixel. It's badly & quickly written but it's supposed to work, i see no reason it shouldn't. class Program { static void Main(string[] args) { Console.WriteLine(args[0]); Bitmap Map = new Bitmap(args[0]); var MapRGB = Map.Clone(new Rectangle(0, 0, Map.Width, Map.Height), PixelFormat.Format32bppRgb); int height = Map.Height; int widht = Map.Width; string MapText = height.ToString() + "," + widht.ToString() + ","; for (int h = 0; h < height; h++) { for (int w = 0; w < widht; w++) { Color currentPixel = MapRGB.GetPixel(w ,h ); if (currentPixel.ToArgb() == Color.White.ToArgb()) { MapText = MapText + "0,"; } if (currentPixel.ToArgb() == Color.Black.ToArgb()) { MapText = MapText + "1,"; } if (currentPixel.ToArgb() == Color.Gray.ToArgb()) { MapText = MapText + "2,"; } if (currentPixel.ToArgb() == Color.Yellow.ToArgb()) { MapText = MapText + "3,"; } if (currentPixel.ToArgb() == Color.Green.ToArgb()) { MapText = MapText + "4,"; } if (currentPixel.ToArgb() == Color.Red.ToArgb()) { MapText = MapText + "5,"; } Console.Clear(); Console.WriteLine(args[0] + " WROKING " + w.ToString() + " OF " + widht.ToString() + " IN " + h.ToString() + " OF " + height.ToString()); } } Console.Clear(); Console.Write(MapText); System.IO.File.WriteAllText(Environment.CurrentDirectory + "/Map.bwn", MapText); Console.ReadKey(); } }
Not absolutely sure but could be an encoding issue. Use the other overload File.WriteAllText(String, String, Encoding) which takes a encoding type as argument like File.WriteAllText(filePath, stringMessage, Encoding.UTF8);
Compare time between a log file and an image file
I have 1000's of images and more than 100,000 lines of log files. I need to check if there exists an image that is associated with each unix time in log file. To do this, I first read thru all the images and stored time information in an array. Then I read thru all the lines of the log file, split each information (lat, long, time) and stored them in an array. Finally, I am taking one time element at a time and checking if it matches with image time array. If no match is found, I get the time from log, get lat and long from the same array and write it to a text file. But the overall process takes very long time. I am looking into efficiencies on how to make this process faster. var fileList = Directory.GetFiles(imageLocation, "*.jpg"); //Array that will store all the time information obtained from image property double[] imgTimeInfo = new double[fileList.Length]; int imgTimeCounter =0; foreach (var fileName in fileList) { x++; string fileNameShort = fileName.Substring(fileName.LastIndexOf('\\') + 1); richTextBox1.AppendText("Getting time information from image " + x + " of " + fileList.Length + " : " + fileNameShort + Environment.NewLine); richTextBox1.Refresh(); using (var fs = File.OpenRead(fileName)) { //create an instance of a bitmap image var image = new Bitmap(fs); //get the date/time image property of the image PropertyItem property = image.GetPropertyItem(36867); System.Text.Encoding encoding = new System.Text.ASCIIEncoding(); string valueFrmProperty = encoding.GetString(property.Value); //Format the value obtained to convert it into unix equivalent for comparison string valueCorrected = valueFrmProperty.Split(' ')[0].Replace(":", "/") + " " + valueFrmProperty.Split(' ')[1]; var unixTime = ConvertToUnixTimeStamp(DateTime.Parse(valueCorrected)); imgTimeInfo[imgTimeCounter] = unixTime; imgTimeCounter++; //It is very important to dispose the image resource before trying to read the property of another image. image.dispose frees the resources or else we get //outofmemoryexception. image.Dispose(); } } MessageBox.Show("Images done."); richTextBox1.AppendText("Fetching time information from log files..."+Environment.NewLine); richTextBox1.Refresh(); int counter4Time = contentBathy.Length / 6; //assign counter for lat,long and time int timeCounter = 3; for (int i = 0; i < counter4Time; i++) { richTextBox1.AppendText("Searching time match with image files..." + Environment.NewLine); richTextBox1.Refresh(); double timeValue = Int32.Parse(contentBathy[timeCounter]); //Looks for values that is +- 3 seconds different in the image file. if (Array.Exists(imgTimeInfo, a => a == timeValue || a == timeValue + 1 || a == timeValue + 2|| a == timeValue+3 ||a == timeValue-1|| a== timeValue-2||a == timeValue-3)) { File.AppendAllText(#"c:\temp\matched.txt", "Lat : " + contentBathy[timeCounter - 3] + " Log : " + contentBathy[timeCounter - 2] + Environment.NewLine); richTextBox1.AppendText("Image with same time information found. Looking for another match."+ Environment.NewLine); } else { //richTextBox1.AppendText("Time did not match...Writing GPX cordinates..." + Environment.NewLine); //richTextBox1.Refresh(); File.AppendAllText(gpxLocation, "Lat : " + contentBathy[timeCounter - 3] + " Log : " + contentBathy[timeCounter - 2] + Environment.NewLine); } if(timeCounter < contentBathy.Length-3) timeCounter += 6; } }
Null Refrence Exception Unhandled in my Microprocessor simulator [duplicate]
This question already has answers here: What is a NullReferenceException, and how do I fix it? (27 answers) Closed 8 years ago. So I have my memory class which looks like this: namespace GeminiCore { public class Memory { public static int[] memory = new int[256]; public string nextInstruction; public static int cacheSize = 8; public CPU myCPU; public struct frame { public bool dirtyBit; public int isEmpty; public int value; public int tag; public frame(int cacheSize) { dirtyBit = false; isEmpty = 1; value = 0; tag = 0; } } public int solveMemory(int value, int instr, frame[] myCache) { Console.WriteLine("I reached the solveMemory!!!"); int block = value % cacheSize; if(instr == 127 || instr == 125 || instr == 124 || instr == 123 || instr == 122 || instr == 121|| instr == 120) { Console.WriteLine("I reached the read section!!!"); if(myCache[block].isEmpty == 0) //Read hit if(myCache[block].tag == value) return myCache[block].value; else { myCache[block].value = memory[value]; //Read Miss myCache[block].tag = value; myCache[block].isEmpty = 0; Console.WriteLine("Read Miss --- The Cache is as follows: block = " + block + " the value at this block is: " + myCache[block].value + " the tag at this block is: " + myCache[block].tag); return myCache[block].value; } } else { Console.WriteLine("I reached the write section!!!"); if (myCache[block].isEmpty == 1) //Write Miss { Console.WriteLine("Write Miss --- The Cache is as follows: block = " + block + " the value at this block is: " + myCache[block].value + " the tag at this block is: " + myCache[block].tag); memory[value] = myCPU.ACC; } else { if (myCache[block].dirtyBit == false) { if (myCache[block].tag != value) { myCache[block].value = myCPU.ACC; //Write Hit myCache[block].dirtyBit = true; myCache[block].tag = value; Console.WriteLine("Write Hit --- The Cache is as follows: block = " + block + " the value at this block is: " + myCache[block].value + " the tag at this block is: " + myCache[block].tag); } } else { memory[myCache[block].tag] = myCache[block].value; myCache[block].value = myCPU.ACC; myCache[block].tag = value; myCache[block].dirtyBit = false; Console.WriteLine("Write Hit --- The Cache is as follows: block = " + block + " the value at this block is: " + myCache[block].value + " the tag at this block is: " + myCache[block].tag); } } } return value; } } } and then I have my CPU class, and I will just post a small snippet of where the error is occurring: public Memory myMemory; public static Memory.frame[] myCache = new Memory.frame[Memory.cacheSize]; public void doInstruction() { var instr = (finalCodes[i] >> 9) & 127; //Parses op code to find instruction var immed = (finalCodes[i] >> 8) & 1; //Parses op code to find immediate value var value = (finalCodes[i] & 255); //Parse op code to find value foreach (Memory.frame x in myCache) { Console.WriteLine("Dirtybit: " + x.dirtyBit + " isEmpty: " + x.isEmpty + " tag: " + x.tag + " value: " + x.value); } switch (instr) { case (127): //LDA instruction if (immed == 1) ACC = value; else if (immed == 0) //ACC = Memory.memory[value]; ACC = myMemory.solveMemory(value, instr, myCache); break; case (126): //STA instruction if (immed == 0) { Console.WriteLine("The value is: " + value + " The instruction is: " + instr); foreach (Memory.frame x in myCache) { Console.WriteLine("Dirtybit: " + x.dirtyBit + " isEmpty: " + x.isEmpty + " tag: " + x.tag + " value: " + x.value); } //Memory.memory[value] = ACC; myMemory.solveMemory(value, instr, myCache); } So here is my problem, when I run my test which takes in assembly code and translates it to binary then runs through the code, when I get to the second command "sta" it should go to the line: myMemory.solveMemory(value, instr, myCache); It then gives me a Null Reference Exception when it reaches that point. As you can see I have some command line output to try and see where it is going. It prints out the contents of myCache right before that line. It does not reach any of the debug statements in the solveMemory function however, not even the first one that says: Console.WriteLine("I reached the solveMemory!!!"); I'm not really sure what is causing this error and I've been looking at it for quite some time. Hopefully one of you will be able to find where exactly I am messing up. Thanks for the time.
public Memory myMemory; should be: public Memory myMemory = new Memory();