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: 3647980 • 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?


The assignment is for It is an email contact manager. The actual assignment is to write a Windows application that gets names and e-mail address from the user and displays a list of all of the user's contacts. Each time the user enters a new contact, they should be able to see it added to a list of previously stored contacts.

We were given a sample pseudocode as well, which I will include. When designing the GUI, consider initially disabling the e-mail textbox and the button until all information is entered. Redisable them when the button is clicked. Note that you can also use the Tab Index property of GUI objects to set their order of use.

Pseudocode:

When the "Add Contact" button is clicked

Open an output file stream for appending

Write the contact's name and e-mail address to the file

Close the file stream

Clear the list

Open an input file stream for reading

Add each line of the file to the list

Close the file stream

When the form closes Close the input and output file streams


The code I have 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

Is 'projectName' the full assembly and namespace where the file sqlUpdates.txt lives? Is the code running your example above in the 'projectName' assembly, or somewhere else? If the sqlUpdates.txt file lives in a namespace below the projectName assembly, then you need to specify that in the call to GetManifiestResourceStream, ie: Stream resource = assem.GetManifestResourceStream("projectName.nameSpace.sqlUpdate

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