title: NullReferenceException WIA C#
body: <p>When I run the code below I get this error</p>
  
  <p>NullreferenceException was unhandled..</p>
  
  <p>below is my code</p>
  
  <pre><code>private void showScannerDialog()
          {
              this.scanner = null;
              this.imageItem = null;
  
              this.getScanner();
  
              WIA.CommonDialog dialog = new WIA.CommonDialog();
  
              Items imageItems = dialog.ShowSelectItems(this.scanner,  WiaImageIntent.TextIntent, WiaImageBias.MinimizeSize, false, true, false);
              if (imageItems != null)
              {
                                      foreach (Item item in imageItems)
                  {
                      imageItem = item;
                      break;
                  }
              }
          }
  </code></pre>
  
  <p>thanks</p>
  
  <hr>
  
  <h2>// complete code</h2>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  using System.IO;
  using System.Collections;
  using System.Drawing.Imaging;
  using System.Runtime.InteropServices;
  
  namespace WindowsFormsApplication1
  {
      public partial class Form1 : Form
      {
  
          string scannerName;
  
          private Device scanner;
          private Item imageItem;
  
  
          private const int ADF = 1; 
          private const int FLATBED = 2;
  
          private const int DEVICE_NAME_PROPERTY_ID = 7;
          private const int DOCUMENT_HANDLING_PROPERTY_ID = 3088;
  
  
  
          public Form1()
          {
              InitializeComponent();
          }
  
          private void Form1_Load(object sender, EventArgs e)
          {
              showScannerDialog();
          }
          public static string[] GetScannerList()
          {
              ArrayList scannerList = new ArrayList();
  
              DeviceManager deviceManager = new DeviceManager();
  
  
  
              if (deviceManager.DeviceInfos.Count == 0)
              {
                  return new string[0]; // return an empty string array
              }
  
              foreach (DeviceInfo deviceInfo in deviceManager.DeviceInfos)
              {
                  if (deviceInfo.Type == WiaDeviceType.ScannerDeviceType)
                  {
                      Device device = deviceInfo.Connect();
  
  
                      scannerList.Add(getDeviceProperty(device, DEVICE_NAME_PROPERTY_ID));
  
                      }
              }
              return (string[])scannerList.ToArray(typeof(string));
          }
  
  
          public int Init(string scannerName)
          {
              this.scannerName = scannerName;
  
              this.showScannerDialog();
              if (this.imageItem == null)
              {
                  return 0;
              }
              else
              {
                  return 1;
              }
          }
  
  
  
  
  
  
  
  
          public int Scan(string filePath)
          {
  
              Tiff tiff = new Tiff(filePath);
  
              bool adf = false;
              int numScans = 0;
              ArrayList tempFiles = new ArrayList();
  
              // determine if the scanner is set to use an ADF
              string docHandlingSelect = getDeviceProperty(this.scanner, DOCUMENT_HANDLING_PROPERTY_ID);
              if (docHandlingSelect != "")
              {
                  try
                  {
                      if ((int.Parse(docHandlingSelect) &amp; ADF) == ADF)
                      {
                          adf = true;
                      }
                  }
                  catch { }
              }
  
              while (true)
              {
  
                  string tempFile = Path.GetTempFileName();
                  tempFiles.Add(tempFile);
  
  
  
                  File.Delete(tempFile);
  
                  ImageFile wiaFile = (ImageFile)imageItem.Transfer(FormatID.wiaFormatTIFF);
  
                  wiaFile.SaveFile(tempFile);
                  Image tempImage = Image.FromFile(tempFile);
                  tiff.AddImage(tempImage);
                  tempImage.Dispose();
  
                  numScans++;
  
  
                  if (!adf)
                  {
                      DialogResult result =
                          MessageBox.Show("Do you wish to scan another page and save it to the same file?",
                          "Scanner Message", MessageBoxButtons.YesNo);
                      if (result == DialogResult.No)
                      {
                          break;
                      }
  
                      this.showScannerDialog();
                      if (this.imageItem == null)
                      {
                          break;
                      }
                  }
              }
  
              tiff.Close();
  
  
              foreach (string f in tempFiles.ToArray(typeof(string)))
              {
                  File.Delete(f);
              }
              Marshal.ReleaseComObject(imageItem);
  
  
              if (numScans == 0)
              {
                  throw new Exception("Nothing was scanned.");
              }
  
              return 1;
          }
  
  
          private void getScanner()
          {
  
              DeviceManager deviceManager = new DeviceManager();
  
              foreach (DeviceInfo deviceInfo in deviceManager.DeviceInfos)
              {
                  if (deviceInfo.Type == WiaDeviceType.ScannerDeviceType)
                  {
                      Device device = deviceInfo.Connect();
                      if (this.scannerName == getDeviceProperty(device, DEVICE_NAME_PROPERTY_ID))
                      {
                          this.scanner = device;
                      }
                  }
              } 
  
  
          }
  
          private void showScannerDialog()
          {
              this.scanner = null;
              this.imageItem = null;
  
  
  
              this.getScanner();
  
  
              WIA.CommonDialog dialog = new WIA.CommonDialog();
  
              Items imageItems = dialog.ShowSelectItems(this.scanner, WiaImageIntent.TextIntent, WiaImageBias.MinimizeSize, false, true, false);
              if (imageItems != null)
              {
  
                  foreach (Item item in imageItems)
                  {
                      imageItem = item;
                      break;
                  }
              }
          }
  
          private static string getDeviceProperty(Device device, int propteryID)
          {
              string retVal = "";
  
  
                  foreach (Property prop in device.Properties)
                  {
                      if (prop.PropertyID == propteryID)
                      {
                          retVal = prop.get_Value().ToString();
                          break;
                      }
                  }
                  return retVal;
  
          } 
  
  
      }
  }
  </code></pre>