Null reference error in licence key program

Tijn Rodrigo 21 Reputation points

I am making a license key system in Win-forms (with C# in Visual Studio) and I ran into an error when executing the code4

The Error pictures:


The project is inspired by an License key system with the .WPF framework (Works fine on .WPF). Can someone help me fixing the error?

The full source code is available on github: LicenseKey.NET-C#

The error is originating from the following line:

using (StreamReader reader = new StreamReader(stream))

Exeption Text:

System.Argument Null Exception: Value cannot be null. Parameter name: stream at System.I0.StreamReader.ctor(Stream stream, Encoding encoding. Boolean dete at System. IO.StreamReader..ctor(Stream stream) at QP _Helpers.QP_Helpers. IsLicensed (String key, String resourceName) in C: \User at LicenseKey.NET buy btn_register _Click (Object sender, Event Args e)

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,818 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,180 questions
{count} votes

Accepted answer
  1. AgaveJoe 26,181 Reputation points

    According to the error the line of code causing the null reference is; using (Stream stream = assembly.GetManifestResourceStream(resourceName)). I assume you are missing the embedded resource in your project.

            public static bool IsLicensed(string key, string resourceName)
                string[] licenseKeys;
                var assembly = Assembly.GetExecutingAssembly();
                using (Stream stream = assembly.GetManifestResourceStream(resourceName))
                using (StreamReader reader = new StreamReader(stream))
                    licenseKeys = reader.ReadToEnd().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                using (MD5 md5Hash = MD5.Create())
                    if (licenseKeys.Contains(GetMd5Hash(md5Hash, key)))
                        return true;
                        return false;

    You really should learn how to user the Visual Studio debugger to review your code. the next step is to review the WPF application to find the resource and use the same in your project. You can do a Google search if you are unsure how to use resources in Web Forms.

1 additional answer

Sort by: Most helpful
  1. Tijn Rodrigo 21 Reputation points

    Thanks @AgaveJoe , @Karen Payne MVP

    I found the solutions and it works now.

    0 comments No comments