0

I have read through many posts on this type of situation and I can't figure out the solution or what I am doing wrong.

I have a code that references a DLL. It is added as a reference in Visual Studio and it is copied locally along with the target exe file. It runs for me, but when I deploy it to another computer it causes a NullReference exception at the below line of code (see comment):

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.

at CheckUpdate.Program.checkUpdate() in Program.cs:line 28

at CheckUpdate.Program.Main(String[] args) in Program.cs:line 22

Here is my code, see comments where the error is reported on the stack trace:

using System;
using System.Collections.Generic;
using System.Text;
using Com.Idiominc.Webservices.Client;
using System.Diagnostics;
using System.IO;
using System.Net;


namespace CheckUpdate
{
    class Program 
    {
        private static WSContext _UpdateWScontext { get; set; }
        private static string wsUrl = "https://www.myWorldServer.com/ws";
        private static string[] myArgs;

        static int Main(string[] args)
        {
            myArgs=args;
            if (myArgs.GetUpperBound(0) != 1) return 9;
            return checkUpdate();
        }

        public static int checkUpdate()
        {
                string testStr = "password1";

                while (_UpdateWScontext == null && testStr != "")
                {
                    try
                    {
                        _UpdateWScontext = new WSContext("user", testStr, wsUrl + "/services");
                    }
                    catch
                    {
                        if (_UpdateWScontext == null)
                        {
                            if (testStr == "password1") testStr = "password2";
                            else if (testStr == "password2") testStr = "password3";
                            else testStr = "";
                        }
                    }
                }
                if (_UpdateWScontext.token == string.Empty) return 0;

                string currentversion = myArgs[0];

                string latestver = getLatestVersion();

            if (latestver == null)
            {
               return 0;
            }
            else if (!currentversion.Equals(latestver))
            {
                if (getLatestDownload()) return 1;
                else return 0;
            }
            else
            {
                _UpdateWScontext.logout(_UpdateWScontext.token);
                return 2;
            }
        }       
    }
}

The DLL (WSWebServices.dll) is copied locally along with the executable. It runs on my own station, but when I port it to another station, it causes the exception.

Any advice or clue would be appreciated, thanks.

Edit:

Based on comments the NullReference exception was on the line

if (_UpdateWScontext.token == string.Empty) return 0;

I have now modified this to

if (_UpdateWScontext == null || _UpdateWScontext.token == string.Empty) return 0;

With this no more NullReference Exception, however the exit code now is always 0, meaning the variable is still null.

Any suggestion as to why the connection is not working to the URL? It does work from the browser on the same computer.

Update - solved

I traced the error by outputting the Exception stack trace to the console on the station where the app was not working, and it turned out that the WSWebServices.dll is referencing the Microsoft.Web.Services2.dll (part of the WSE package) and for some reason this was missing from the installation on the misbehaving station.

I added it as a reference and made sure that the dll is copied to the application folder and it now works as intended. Documented here for others in the same mystery why the WorldServer web services would not work.

ib11
  • 2,530
  • 3
  • 22
  • 55
  • Have you give reference of that DLL ? – Unknown_Coder Oct 28 '17 at 05:45
  • 2
    It is not plausible that the program statement `while (_UpdateWScontext == null && testStr != "")` could throw `NullReferenceException`. There are no dereferencing expressions in that statement. Frankly, your question seems most likely suited as a duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/q/4660142), but also suffers from the lack of a good [mcve] that would reliably reproduce the problem. You need to improve the question significantly if you want useful help from the Stack Overflow community. – Peter Duniho Oct 28 '17 at 05:48
  • 1
    It is possible that within while loop, firstly you try with Password1, you get exception on creating WSContext, then in catch block you set testStr to Password2. With Password2 you again get exception, and this time in catch block you set testStr to empty string, and you leave out of while loop without creation of _UpdateWScontext. I think you null ref exception exactly at this point of code `if (_UpdateWScontext.token == string.Empty) return 0;`. You should log exception details in the catch block within the while loop, it can give clue about the error. – Nazim Oct 28 '17 at 06:07
  • @PeterDuniho The stack trace identifies line 28 of the code as the location of the exception. I thought the problem is `_UpdateWScontext` but now I have no idea. – ib11 Oct 28 '17 at 06:12
  • 1
    Line 28 may or may not be the line you refer to by your comment, and assuming it is, it may or may not be where the exception actually occurs. In particular, if you're running an optimized ("Release") build, the executing code often no longer matches up line-for-line with the original C# and reported line #'s in exceptions may be off. In any case, there's not anything here for us to go on. You'll need to either follow the advice in the Q&A I mentioned earlier, or improve the post so it includes a good [mcve]. – Peter Duniho Oct 28 '17 at 06:15
  • @PeterDuniho Thank you. I fixed the post, as that exception was on the dereferencing line later in the code. It does not resolve however the issue that the DLL seems to be "not working" on that computer. Any suggestion? – ib11 Oct 28 '17 at 06:38
  • If it works when calling from browser but not from application, it is possible that on browser you are using proxy to connect the internet. Check `WSContext` class if you can pass proxy address from constructor. – Nazim Oct 28 '17 at 06:48

1 Answers1

1

Within while loop, firstly you try with Password1, you get exception on creating WSContext, then in catch block you set testStr to Password2. With Password2 you again get an exception, and this time in catch block you set testStr to an empty string, and you leave out of while loop without creation of _UpdateWScontext.

I think you null ref exception exactly at this point of code if (_UpdateWScontext.token == string.Empty) return 0;.

You should log exception details in the catch block within the while loop, it can give clue about the error.

ib11
  • 2,530
  • 3
  • 22
  • 55
Nazim
  • 639
  • 2
  • 10
  • 26
  • Thanks for the advices, and I did that and was able to trace the error to another missing dll on the station. Freak. See update on my original post and if you don't mind, please upvote the question as I think it is valuable and don't want to delete it (I normally don't want to keep negative voted questions) thanks. – ib11 Oct 29 '17 at 22:04