0

I have a problem generating qr code. I used a sample from site: https://www.c-sharpcorner.com/article/generate-barcode-and-qr-code-in-xamarin-android/

I have used the exact same code as in the example but I got a null reference error at this line:

bitmapMatrix = new MultiFormatWriter().encode(message, BarcodeFormat.QR_CODE, size, size);

In my project I have installed the next packages:

  • Zxing.net
  • Xamarin.AndroidX.AppCompat
  • Xamarin.Essentials
  • Xamarin.Google.Android.Material

Debug info: enter image description here

Does anyone have a clue why I am getting a null reference?

Yaza
  • 195
  • 2
  • 10
  • 22
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – GSerg Jan 17 '22 at 12:53
  • I know what a null reference is, but at this line everything is not null. So I guess something is happening within encode method. But am I missing something? Or is it because it's a newer version then the example? – Yaza Jan 17 '22 at 12:56
  • `everything is not null` - please show that. – GSerg Jan 17 '22 at 12:59
  • I edited my question and added the screenshot with the watcher. As you can see the null reference would be coming from within the encode method of the MultiFormatWriter but I can't figure out what it is. – Yaza Jan 17 '22 at 13:38
  • I had done a simple demo and repeated the problem. But it just happends on the QRCode and the others work well. So you can try to post the issue to the developer of zxing package on the github. – Liyun Zhang - MSFT Jan 18 '22 at 08:33
  • Thank you @LiyunZhang-MSFT I will contact the developer of zxing package. – Yaza Jan 18 '22 at 09:57

2 Answers2

1

It's a bug inside the static initialization routines which occurs if some encodings (character sets) are missing. The next version (>0.16.7) will fix it.

Michael
  • 2,361
  • 1
  • 15
  • 15
  • Hi Michael do you have any idea when this fix is going to be deployed? – Yaza Jan 21 '22 at 23:23
  • I'm working on it. It will take some time. – Michael Jan 24 '22 at 20:10
  • Btw. version 0.16.8 is released. – Michael Mar 23 '22 at 19:43
  • hi Michael, I have tested the code but I get the next error: BitMatrix/MultiFormatWriter/BarcodeFormat exists both in zxing and zxing.portable. Do you have any idea what the problem could be? – Yaza May 03 '22 at 16:26
  • Perhaps you added the nuget package ZXing.Net.Mobile and ZXing.Net. Remove one of them or use a different alias instead of "global" for one of the references. – Michael May 04 '22 at 18:20
  • Hi Michael, yes that was my problem. I referenced a project and that project contained zxing.net.mobile. Thank you for your support. Is the zxing.net a big improvement if you compare this to zxing.net.mobile? I have been using zxing.net.mobile now but I was wondering if the zxing.net a better version is? – Yaza May 05 '22 at 09:46
  • ZXing.Net.Mobile uses ZXing.Net under the hood (an older version). ZXing.Net.Mobile adds some classes which make it easier to integrate into mobile scenarios. It would be great if the maintainer of ZXing.Net.Mobile would release a new version of the library built against the newest ZXing.Net assemblies. – Michael May 07 '22 at 10:04
0

I solved my problem thanks to a post from Jarvan Zhang in xamarin forums: https://learn.microsoft.com/en-us/answers/questions/697849/generate-qr-code-in-xamarinandroid.html

Jarvan Zhang: The ZXing.Net API may not perform well on Android. Try using the ZXing.Net.Mobile package instead, which is a mobile-specific. I've tested the code with this package, it works as expected. The nuget requires the Xamarin.AndroidX.Legacy.Support.V4 dependency, please install the package on the project.

I hope this helps someone who's experiencing the same problem.

Yaza
  • 195
  • 2
  • 10
  • 22