Note that there are some explanatory texts on larger screens.

plurals
  1. POStreamWriter disposed when accessing method
    text
    copied!<p>I'm facing a problem where my declared StreamWriter gets disposed when I try to use a method to write on my log file. Everything is working as expected, except when I run <em>AttachPink</em> or <em>AttachBlue</em>, from another class. Then the StreamWriter is disposed and I get a nullPointerException</p> <pre><code>class Logs : IDisposable { //other declarations private StreamWriter HistoryWriter; private int ReportInterval = 0; public void NewHistory() { HistoryWriter = new StreamWriter(HistoryLocation + HistoryName + HistoryExtension); PrepareHistory(); } private void PrepareHistory() { HistoryWriter.WriteLine("&lt;html&gt;&lt;body bgcolor='#000000'&gt;"); /* * Insert initial HTML tags */ } public void SendHistory() { HistoryWriter.WriteLine("&lt;/body&gt;&lt;/html&gt;"); /* * Final HTML tags */ HistoryWriter.Close(); if (ReportInterval &gt; 0) { /* * Upload File */ } else { Debug.WriteLine("ERROR: Report Interval for History has not been set"); } NewHistory(); } public void AttachPink(String message, StreamWriter writer) { writer.Write( "&lt;font color='DA1BE0'&gt;" + message + "&lt;/font&gt;"); } public void AttachBlue(String message, StreamWriter writer) { writer.Write( "&lt;font color='0C93ED'&gt;" + message + "&lt;/font&gt;"); } public StreamWriter getHistoryWriter() { return HistoryWriter; } public void SetHistoryInterval(int interval) { ReportInterval = interval; } public void Dispose() { if (HistoryWriter != null) { HistoryWriter.Close(); HistoryWriter.Dispose(); HistoryWriter = null; } } } </code></pre> <p>To use the methods I simply declare an instance of Logs class inside another class, like so:</p> <pre><code>class UsingLogs { Logs logs = new Logs(); logs.NewHistory(); logs.AttachBlue("my message", logs.getHistoryWriter()); } </code></pre> <p>I don't know how should I go for preserving classes variables state when accessing multiple methods.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload