Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am working with C#, I have code written and no errors show. However, when I go

ID: 3647975 • Letter: I

Question

I am working with C#, I have code written and no errors show. However, when I go to debug the program, I receive an "ArgumentNullException was unhandled" Value cannot be null. Parameter name: stream This exception shows up when it gets to the line:

this.streamWriter = new System.IO.StreamWriter(inputFileStream);

Is there a way to simplify the code, make it more efficient, and correct the error that I am receiving?

My code is as follows:

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;

namespace Email_Contact_Manager
{
public partial class Form1 : Form
{
private System.IO.FileStream outputFileStream;
private System.IO.StreamWriter streamWriter;
private System.IO.FileStream inputFileStream;
private System.IO.StreamReader streamReader;

public Form1()
{
InitializeComponent();
this.textBox1.Enabled = false;
this.button1.Enabled = false;
this.outputFileStream = new System.IO.FileStream(@"contacts.txt", System.IO.FileMode.Append);
this.streamWriter = new System.IO.StreamWriter(inputFileStream);
this.inputFileStream = new System.IO.FileStream(@"contacts.txt", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read);
this.streamReader = new System.IO.StreamReader(inputFileStream);
this.updateContactList();
}

private void button1_Click(object sender, EventArgs e)
{
this.updateContactFile();
this.updateContactList();
}

private void updateContactFile()
{
string name = this.textBox1.Text;
string email = this.textBox2.Text;
try
{
streamWriter.WriteLine(name);
streamWriter.WriteLine(email);
}
catch (Exception exp)
{
Console.WriteLine(exp.Message);
}
this.textBox1.Clear();
this.textBox2.Clear();
}

private void updateContactList()
{
this.textBox3.Clear();
try
{
while (streamReader.EndOfStream == false)
{
string name = streamReader.ReadLine();
string email = streamReader.ReadLine();
string contact = name + Environment.NewLine + email + Environment.NewLine + Environment.NewLine;
this.textBox3.AppendText(contact);
}
}
catch (Exception exp)
{
Console.WriteLine(exp.Message);
}
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
if (this.textBox1.Text.Length > 0)
this.textBox2.Enabled = true;
else
this.textBox2.Enabled = false;
}

private void textBox2_TextChanged(object sender, EventArgs e)
{
if (this.textBox2.Text.Length > 0)
this.button1.Enabled = true;
else
this.button1.Enabled = false;
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
this.streamReader.Close();
this.streamWriter.Close();
this.inputFileStream.Close();
this.outputFileStream.Close();
}
}
}

Explanation / Answer

can you tell what were you trying to make?

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote