question

TijnRodrigo-9943 avatar image
0 Votes"
TijnRodrigo-9943 asked TijnRodrigo-9943 answered

Null reference error in licence key program

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)


dotnet-csharpwindows-forms
· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Best to set a breakpoint prior to the exception, step through the code and inspect variable values to see where the issue is coming from.

0 Votes 0 ·

Dear @karenpayneoregon,

I already try'd this, it doesn't work.

0 Votes 0 ·

I has to work, you must look at the Stream for the reader constructor

189279-f1.png


0 Votes 0 ·
f1.png (18.9 KiB)
Show more comments

I found out what the problem is, im trying to fix it

0 Votes 0 ·
AgaveJoe avatar image
0 Votes"
AgaveJoe answered TijnRodrigo-9943 commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks @AgaveJoe,

I will try this out, and i don't really know how to use a debugger (I am still a child) because normally my code works first try i code.

0 Votes 0 ·
TijnRodrigo-9943 avatar image
0 Votes"
TijnRodrigo-9943 answered

Thanks @AgaveJoe , @karenpayneoregon

I found the solutions and it works now.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.