0

In my project i need contunistly read barcode and put them in a list. But while i trying it its always giving me NullReferenceException but i didn't get it. When i check it in debug i can see it have a string value but while trying put it to list its always giving me that error. Its my code for Scan and take data :

 List<HoldMyString> mylist;  
 HoldMyString hold;
    private void ZXingScannerView_OnScanResult(ZXing.Result result)
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            hold.HoldString = result.Text;  // HoldString is taking string value

            mylist.Add(hold);    
           

            
        });
    }

How i can put my datas in to my list contunistly. Thanks for help!

  • 1
    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) – Yong Shun Apr 18 '21 at 00:15
  • Hi @Uğurcan Uçar, you declare `hold` with `HoldMyString` type but you didn't initialize it. Thus it is `null`. So when you try to access its properties, you will get `NullReferenceException`. – Yong Shun Apr 18 '21 at 00:18

1 Answers1

0

I was able to reproduce the same error with yours. enter image description here

You could use the code below.

HoldMyString:

 public class HoldMyString
{
    public string HoldString { get; set; }
}

Code for Scan and take data:

  List<HoldMyString> mylist=new List<HoldMyString>();
    HoldMyString hold = new HoldMyString();
    private void Handle_OnScanResult(ZXing.Result result)
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            hold.HoldString = result.Text;  // HoldString is taking string value

            mylist.Add(hold);
        });
    }

For more details about how to get the barcodes, you could check the thread i done before. How to embed ZXing scanner in PageRenderer/Fragment/View into Xamarin.Forms ContentPage?

Wendy Zang - MSFT
  • 6,190
  • 1
  • 3
  • 12