-1

I have an error when running a system made in C # by VS 2019. What happens is the following, by IDE everything works fine, but there is one function that is activated with a click of a button, this function creates a .TXT with two lines ( a mini database), but after generating the installer for the system, by VS2019 itself, and installing the program on the computer, this function presents the following error.

What do I do?

     private void SaveTXT(int _fontSize, bool _autoRefresh) {
        string path = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
        //string path = Path.GetDirectoryName(Application.ExecutablePath);

        File.Delete(@path + "data.txt");

        FileStream fs = new FileStream(@path + "data.txt", FileMode.Append);
        StreamWriter sw = new StreamWriter(fs);

        sw.WriteLine(_fontSize);
        sw.WriteLine(_autoRefresh);

        sw.Flush();
        sw.Close();
        fs.Close();
    }

Error:

Consulte o final desta mensagem para obter detalhes sobre como chamar a 
depuração just-in-time (JIT) em vez desta caixa de diálogo.

************** Texto de Exceção **************
System.NullReferenceException: Referência de objeto não definida para uma instância de um objeto.
   em Spotics.frmMain.switchSongOff_SwitchStateChanged(Object sender, EventArgs e)
   em XanderUI.XUISwitch.OnSwitchStateChanged()
   em XanderUI.XUISwitch.OnMouseDown(MouseEventArgs e)
   em System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)
   em System.Windows.Forms.Control.WndProc(Message& m)
   em System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   em System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   em System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Assemblies Carregados **************
mscorlib
    Versão do Assembly: 4.0.0.0
    Versão do Win32: 4.8.4121.0 built by: NET48REL1LAST_C
    Base de Código: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
Spotics
    Versão do Assembly: 2.0.0.0
    Versão do Win32: 2.0.0.0
    Base de Código: file:///D:/Program%20Files%20(x86)/RBalconi/Spotics/Spotics.exe
----------------------------------------
System.Windows.Forms
    Versão do Assembly: 4.0.0.0
    Versão do Win32: 4.8.4121.0 built by: NET48REL1LAST_C
    Base de Código: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
    Versão do Assembly: 4.0.0.0
    Versão do Win32: 4.8.4001.0 built by: NET48REL1LAST_C
    Base de Código: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
    Versão do Assembly: 4.0.0.0
    Versão do Win32: 4.8.3752.0 built by: NET48REL1
    Base de Código: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
    Versão do Assembly: 4.0.0.0
    Versão do Win32: 4.8.3752.0 built by: NET48REL1
    Base de Código: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Core
    Versão do Assembly: 4.0.0.0
    Versão do Win32: 4.8.4121.0 built by: NET48REL1LAST_C
    Base de Código: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Xml
    Versão do Assembly: 4.0.0.0
    Versão do Win32: 4.8.3752.0 built by: NET48REL1
    Base de Código: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
XanderUI
    Versão do Assembly: 1.0.0.0
    Versão do Win32: 1.0.0.0
    Base de Código: file:///D:/Program%20Files%20(x86)/RBalconi/Spotics/XanderUI.DLL
----------------------------------------
mscorlib.resources
    Versão do Assembly: 4.0.0.0
    Versão do Win32: 4.8.3752.0 built by: NET48REL1
    Base de Código: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/mscorlib.resources/v4.0_4.0.0.0_pt-BR_b77a5c561934e089/mscorlib.resources.dll
----------------------------------------
System.Windows.Forms.resources
    Versão do Assembly: 4.0.0.0
    Versão do Win32: 4.8.3752.0 built by: NET48REL1
    Base de Código: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms.resources/v4.0_4.0.0.0_pt-BR_b77a5c561934e089/System.Windows.Forms.resources.dll
----------------------------------------

************** Depuração JIT **************
Para habilitar a depuração just-in-time (JIT), o arquivo .config deste
aplicativo ou computador (machine.config) deve ter o valor
jitDebugging definido na seção system.windows.forms.
O aplicativo também deve ser compilado com a depuração
habilitada.

Por exemplo:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

Quando a depuração JIT está habilitada, qualquer exceção sem tratamento
será enviada ao depurador JIT registrado no computador,
em vez de ser tratada nesta caixa de diálogo.



Thank you

RBalconi
  • 47
  • 8
  • 2
    Read [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/) and debug your program. If you still have problems you need to [post your error message](https://idownvotedbecau.se/imageofanexception/), not a screenshot of the message. – Dour High Arch Mar 29 '20 at 22:12
  • @DourHighArch I did not understand very well about this article you sent, I am a beginner in the language. Could you explain it to me in another way? Thank you very much – RBalconi Mar 29 '20 at 22:24
  • @DourHighArch I added the whole error as well, as you mentioned. If you can help I am grateful. Thank you. – RBalconi Mar 29 '20 at 22:29
  • 1
    The exception is `System.NullReferenceException` and don't point to any line in your function. Vs 2019 can help in spotting the NRE. – M.Hassan Mar 29 '20 at 22:38
  • @M.Hassan So, when I run VS2019 it doesn't generate any errors, it only generates after installing using the installer that VS2019 itself generates. If so how do I do it? – RBalconi Mar 29 '20 at 22:44
  • I think you can't find the path that the .exe is in or something like that to generate the .txt based on its path. – RBalconi Mar 29 '20 at 22:45
  • 1
    @RBalconi, add debugging information to your project, [read](https://learn.microsoft.com/en-us/visualstudio/debugger/how-to-set-debug-and-release-configurations?view=vs-2019) – M.Hassan Mar 29 '20 at 22:48
  • @M.Hassan I think I had a breakthrough, being able to see the error in VS2019, is an exception even as you said. But now how can I solve it? Print: https://imgur.com/a/Kttkr3M – RBalconi Mar 29 '20 at 23:12
  • Guys I managed to solve it, I just needed to define a value for the combobox ... Thank you all very much. – RBalconi Mar 29 '20 at 23:27

2 Answers2

0

It doesn't look like the error is coming from this function. From the error, it seems that the function that is erroring is frmMain.switchSongOff_SwitchStateChanged.

Another note, seems that you're not setting the correct path for the file. You're missing the '\' before "data.txt". Currently what you have would look like:

C:\ExecutableRootFolderdata.txt

instead of

C:\ExecutableRootFolder\data.txt

spider913
  • 102
  • 9
  • I added "\" before as you said, recompiled and installed, and it didn't work. :( – RBalconi Mar 29 '20 at 22:21
  • As mentioned, this is not the problem. That was just a separate issue that I noticed. But the main issue is coming from the `switchSongOff_SwitchStateChanged` event handler and not the `SaveTXT` function. Can you post the code for event handler function? – spider913 Mar 29 '20 at 22:42
0

Try this

string path = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
FileStream fileStream = new FileStream(@path + "data.txt", FileMode.Create, FileAccess.Write);
StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.UTF8);

streamWriter.WriteLine(_fontSize);
streamWriter.WriteLine(_autoRefresh);

streamWriter.Close();
Nulltiton
  • 25
  • 2
  • 3
  • 12