Null reference error in licence key program

Tijn Rodrigo 21 Reputation points
2022-04-01T15:17:05.367+00:00

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:

x0f90.png
e2di2.png

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,821 questions
C#
C#
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,204 questions
{count} votes

Accepted answer
  1. AgaveJoe 26,186 Reputation points
    2022-04-02T14:57:49.257+00:00

    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;
                    }
                    else
                    {
                        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.

    https://www.aspsnippets.com/Articles/Read-and-Display-Text-File-and-Image-File-from-Embedded-Resource-in-C-and-VBNet.aspx


1 additional answer

Sort by: Most helpful
  1. Tijn Rodrigo 21 Reputation points
    2022-04-08T17:16:37.567+00:00

    Thanks @AgaveJoe , @Karen Payne MVP

    I found the solutions and it works now.

    0 comments No comments