Object reference not set to an instance of an object.

0 votes
asked May 9 by nicksullivan (200 points)

So I know this is a pretty common problem and but it's showing itself in a very unique way. I have built a program that basically retrieves emails via IMAP and then resends them. I build the program originally in test format under a windows console program. It works perfect. I built a windows service project and have installed it successfully and have it running the code starting fine. The issue is the exact same code in windows service gives me the Object ref not set. I have tried everything that i can think of. Added right, installed the dlls in the GAC, put the files with the EXE. Below is the sample code. Any help would be greatly appreciated. The error code I'm catching is "Object reference not set to an instance of an object"

using imap As New Rebex.Net.Imap()

    Dim mydb As New mySqlDB
        mydb.executeDMLSQL("insert into o365pocdb.poc_log (server, type, data, POC) values ('" & Server & "','Perf-Log','POC Started','" & Username & "')", sresult)

        Try
            mydb.executeDMLSQL("insert into o365pocdb.poc_log (server, type, data, poc) values ('" & Server & "','error','connected fine',' ')", sresult)
            imap.Connect(hostname, SslMode.Implicit)
            imap.Login(Username, Password)
            imap.SelectFolder("Inbox")

        Catch ex As Exception
        'mydb.executeDMLSQL("insert into o365pocdb.poc_log (server, type, data, poc) values ('" & Server & "','error','" & ex.Message & "',' ')", sresult)
        mydb.executeDMLSQL("insert into o365pocdb.poc_log (server, type, data, poc) values ('" & Server & "','error','Going to exit sub - cant connct','" & ex.Message & "')", sresult)
        Exit Sub
    End Try
Applies to: Rebex Secure Mail

1 Answer

0 votes
answered May 9 by Lukas Matyska (39,480 points)

Can you please post here the complete stack trace of the exception?

It can be simple done like this:

    Try
        ' do something
        ' ...
    Catch ex As Exception
        ' get complete details
        Dim details As String = ex.ToString()
        ' store it in file
        File.AppendAllText("c:\data\app.log", details)
    End Try
commented May 9 by nicksullivan (200 points)
'System.NullReferenceException: Object reference not set to an instance of an object.
   at Rebex.AY.R(Assembly J)
   at Rebex.AY.U(NM J, String M, Int32 L)
   at Rebex.Net.NetworkSession.IMX(NM J, String M, Int32 L)
   at Rebex.Net.Imap.PR(String J, Int32 M, TlsParameters L, SslMode I)
   at Rebex.Net.Imap.AG(String J, Int32 M, TlsParameters L, SslMode I)
   at Rebex.Net.Imap.Connect(String serverName, SslMode security)
   at O365POCServiceV1.PfptO365PocSVC.POCProces(Object poc)'
commented May 9 by nicksullivan (200 points)
here is the whole sub routine until it breaks.

 Public Sub POCProces(poc)
        Dim queryresult As String = ""
        Dim POCDetails() As String = poc.Split(",")
        Dim hostname = "outlook.office365.com"
        Dim Server = POCDetails(0)
        Dim Username = POCDetails(1)
        Dim Password = POCDetails(2)
        Dim smtpdomain = POCDetails(3)
        Dim PODAddress = POCDetails(4)
        Dim options As New ParallelOptions()
        Dim messagesperthread = My.Settings.messagesperthread
        options.MaxDegreeOfParallelism = My.Settings.TotalThreads
        Dim sresult As String = ""

        Using imap As New Rebex.Net.Imap()

            Dim mydb As New mySqlDB
            mydb.executeDMLSQL("insert into o365pocdb.poc_log (server, type, data, POC) values ('" & Server & "','Perf-Log','POC Started','" & Username & "')", sresult)

            Try
                mydb.executeDMLSQL("insert into o365pocdb.poc_log (server, type, data, poc) values ('" & Server & "','error','connected fine',' ')", sresult)
                imap.Connect(hostname, SslMode.Implicit)
                imap.Login(Username, Password)
                imap.SelectFolder("Inbox")

            Catch ex As Exception
                'mydb.executeDMLSQL("insert into o365pocdb.poc_log (server, type, data, poc) values ('" & Server & "','error','" & ex.Message & "',' ')", sresult)
                mydb.executeDMLSQL("insert into o365pocdb.poc_log (server, type, data, poc) values ('" & Server & "','error','Going to exit sub - cant connct','" & ex.ToString() & "')", sresult)
                Exit Sub
            End Try
commented May 9 by Lukas Matyska (39,480 points)
Thank you for the exception details. It seems to be caused by already known bug in trial checking code, which is fixed now. We are going to release new version very soon.  I will notify you when it is available to download.
commented May 9 by Lukas Matyska (39,480 points)
The new version 2017 R3 is released. You can download from http://www.rebex.net/secure-mail.net/download.aspx
commented May 9 by nicksullivan (200 points)
That did it.  Thanks!
commented May 24 by nicksullivan (200 points)
Ok that worked on Windows server 2012 but I just ported it over to Windows Server 2016 and having the same issue again.  Works via the console app but not as a service.  Can you confirm that this fix works on 2016?

Thanks
Nick
commented May 24 by Lukas Matyska (39,480 points)
It is very weird. Are you sure you are using the latest version?
Can you please share the exception.ToString() output?
Are you using full or trial version? If you are using trial, please set the trial key explicitly by:

Rebex.Licensing.Key = "==AbwRxXRp2dO3HwtzHytNuQ00ipkazJhYRBWquY+x/GjM==";
commented May 24 by nicksullivan (200 points)
versions are latest.  Double checked.  Now that I look it's not the exact same error but similar.  I'm able to write to the log file fine and not using files anywhere else.  This is the error from the event log.

Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.FileNotFoundException
   at O365POCServiceV1.PfptO365PocSVC.POCProces(System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.ThreadHelper.ThreadStart(System.Object)

This is the portion of the code where it dies.  

  Try
                    Dim theadname As New Threading.Thread(AddressOf POCProces)
                    theadname.Start(POC)
                Catch ex As Exception
                    WriteToErrorLog("error threatstart:" & ex.ToString)

                End Try

However it's not dumping the error to the log.  Again prior to this I am writing to the log fine.
commented May 24 by nicksullivan (200 points)
Nevermind.  Found the issue.  Couldn't find the rebex dll's.  If I copy them to the install folder it works fine.
commented May 25 by Lukas Matyska (39,480 points)
Great, that you were able to find the issue.
...