Reputation: 1390
I'm making an application on C# Winforms which scans documents and places them into a PictureBox, However when I attempt to scan it throws an exception saying "Object Reference not set to an instance of an object" and will not allow me to continue, the stack trace is as below;
To clarify, this is a work project in case anyone gets alarmed by some of the class names.
AbDesktop.exe!AbDesktop.FrmCreditCards.ScanSetup() Line 39 C#
AbDesktop.exe!AbDesktop.FrmCreditCards.ScanFrontBtn_Click(object sender, System.EventArgs e) Line 94 + 0x8 bytes C#
[External Code]
AbDesktop.exe!AbDesktop.Program.Main(string[] args) Line 26 + 0x20 bytes C#
[External Code]
this is the code that is causing the issue;
public void ScanSetup()
{
WIA.CommonDialog dialog = new WIA.CommonDialog();
ImageFile scannedImage=null;
scannedImage = dialog.ShowAcquireImage(
WiaDeviceType.ScannerDeviceType,
WiaImageIntent.UnspecifiedIntent,
WiaImageBias.MaximizeQuality,
FormatID.wiaFormatPNG,
true, true, false);
scannedImage.SaveFile("C:/Users/reece.cottam/Pictures");
}
and this is the button that executes the above code when the click event is fired
private void ScanFrontBtn_Click(object sender, EventArgs e)
{
ScanSetup();
ImageFile IF = new ImageFile();
FrontScanBox.Image = IF.LoadFile("scannedimage.png");
}
Any help would be greatly appreciated.
EDIT The line of code causing the error is ScannedImage.Savefile
Upvotes: 0
Views: 211
Reputation: 29
Have you tried specifying an actual file, instead of just the directory to save to?
scannedImage.SaveFile("C:/Users/reece.cottam/Pictures/test.png");
The lack of a decent filename might cause errors in the SaveFile function. Though that should be visible in the Exception details.
Upvotes: 0
Reputation: 28059
The Documentation is pretty clear, the ShowAcquireImage method Returns an ImageFile object on success, otherwise returns Nothing..
scannedImage is null because this method was not successful, you need to look at the parameters you are passing in here, read the documentation and adjust them appropriately until you are getting the image you want back.
Upvotes: 0
Reputation: 8613
Without extra information about which line throws the exception, I would guess that it's the line scannedImage.SaveFile(...);
since the documentation for CommonDialog.ShowAcquireImage(...)
states that a null value may be returned. In this case, when you try to operate on the variable scannedImage
, you could be attempting to operate on a null reference.
Upvotes: 1