I'll give that a shot first thing in the morning...
Main Topics
Browse All TopicsI have the following Code that when run produces an error that states Object Reference Not Set to an Instance of an Object...
On Lines 31 - 34, if I change them so that they have a value in that line (ex change line 31 to read TW.Resolution = 300) then it works just fine.
I checked, and the values are getting read out of the file and are being parsed/interpreted correctly. So it is not trying to assign blank values...
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Right, but for some reason:
TW.Resolution = vDPI
TW.ColorType = vColourType
TW.ShowInterface = vShowIF
TW.ShowProgress = vShowPR
TW.StartScan()
The above returns the error that is described. However, if I change it so that instead of getting the values from the variable and assign them directly like below:
TW.Resolution = 300
TW.ColorType = twGREY
TW.ShowInterface = false
TW.ShowProgress = true
TW.StartScan()
The above there now works correctly.
In a nutshell, if I hard code the settings, the software will correctly scan and save the image. However, if I try and make those values dynamic, or read in from a config file, it generates the error. I placed break points at each of the above assignments and it does in fact get, and assign the correct values. So that's where I am confused.
Change this :
Dim DPI As XmlNodeList = xDoc.GetElementsByTagName(
Dim colourType As XmlNodeList = xDoc.GetElementsByTagName(
Dim showIF As XmlNodeList = xDoc.GetElementsByTagName(
Dim showPR As XmlNodeList = xDoc.GetElementsByTagName(
To This:
Dim DPI As string = xDoc.ChildNodes(0).ChildNo
Dim colourType As String = xDoc.ChildNodes(0).ChildNo
Dim showIF As String= xDoc.ChildNodes(0).ChildNo
Dim showPR As string = xDoc.ChildNodes(0).ChildNo
Oops! Also
Dim vDPI As Short = CShort(DPI)
Dim vColourType As Twain.TWAIN_CLASS_COLOURTY
Select Case colourType
Case "twBW"
vColourType = Twain.TWAIN_CLASS_COLOURTY
Case "twGrey"
vColourType = Twain.TWAIN_CLASS_COLOURTY
Case "twRGB"
vColourType = Twain.TWAIN_CLASS_COLOURTY
End Select
Dim vShowIF As Boolean = CBool(showIF)
strObject = "vShowIF"
Dim vShowPR As Boolean = CBool(showPR)
Hmmm....
Maybe I went too far....
Dim DPI As string = xDoc.ChildNodes(0).ChildNo
Dim colourType As String = xDoc.ChildNodes(0).ChildNo
Dim showIF As String= xDoc.ChildNodes(0).ChildNo
Dim showPR As string = xDoc.ChildNodes(0).ChildNo
Apparantly, I think you're pulling the wrong xml nodes. Try this instead...
Dim DPI As String = xDoc.ChildNodes(1).ChildNo
Dim colourType As String = xDoc.ChildNodes(1).ChildNo
Dim showIF As String = xDoc.ChildNodes(1).ChildNo
Dim showPR As String = xDoc.ChildNodes(1).ChildNo
Dim vDPI As Short = CShort(DPI)
Dim vColourType As Twain.TWAIN_CLASS_COLOURTY
Select Case colourType
Case "twBW"
vColourType = Twain.TWAIN_CLASS_COLOURTY
Case "twGrey"
vColourType = Twain.TWAIN_CLASS_COLOURTY
Case "twRGB"
vColourType = Twain.TWAIN_CLASS_COLOURTY
End Select
Dim vShowIF As Boolean = CBool(showIF)
strObject = "vShowIF"
Dim vShowPR As Boolean = CBool(showPR)
If I add a breakpoint on line 35 (in the original code posted with the question), and I examine the values for each of the 4 variables, they are in fact correct, and of the correct type. The data is being pulled in from the XML file, and is being pulled in and converted correctly without making any changes to the XML parsing part of the code.
I also do not simply want to reference the elements by index in case the order gets changed around in the file. With it referenced by name the order of elements in the file does not matter, only the name of the tags encompassing them.
OK, lets then example each variable before they get dumped into being made an image. I truly believe that a value is not "pushed" correctly, if you know what I mean.
TW.Resolution = vDPI
TW.ColorType = vColourType
TW.ShowInterface = vShowIF
TW.ShowProgress = vShowPR
msgbox("TWR:" & TW.Resolution.ToString)
msgbox("TWC:" & TW.ColorType .ToString)
msgbox("TWSI:" & TW.ShowInterface.ToString)
msgbox("TWSP:" & TW.Progress.ToString)
So, to recap. You put static values into your variables for scanning....
TW.Resolution = 300
TW.ColorType = twGREY
TW.ShowInterface = false
TW.ShowProgress = true
TW.StartScan()
and everything works fine, every time. But if you push variable values into the same functions, it cops out? And from the message boxes, they all hold the same values necessary to function and are the same datatype that is required? That is one freaky problem. :( I've looked over your code, and while I think I'm a knowledgable guy, I'm not really seeing the flaw that would be a show stopper.
I'm going to throw a crazy suggestion here. Move the code out of the form_load and make a new button. Put the code into the button1_click function to see if that changes things. Sometimes for no expectant reason, that works.
This may also help, may not...
When I have the values hard coded, and the application reaches the point in the code where it is to begin scanning (TW.StartScan()), you can hear the scanner start up, and then it scans. When the values are assigned in dynamically, you hear the scanner start up as it did with them statically set, however, it never scans after that.
I don't know if the program is feeding the scanner some variable that is causing it to seize up or what. Like you, I too am stumped as the variables that it is feeding it are the correct type and value.
Essentially, what is happening is that the program is starting the scanner, but the scanner never completes and sends the program back an image, thus the object reference error. changed the code to read as below, and when I did that, the program would hang (so no more error) right after the scanner initialized, and CPU usage spike to 100% (because of the while loop).
It seems to me that the problem lies within the Twain Control that I had gotten. After doing some more research, I found the following: http://www.ciansoft.com/tw
So in short, the problem is solved.
Business Accounts
Answer for Membership
by: carmodykPosted on 2009-11-04 at 15:03:22ID: 25745043
Hmmm....
Well, let's first narrow the problem down. Try this first, this should help discover the issue. Move some of your Dim statements within the Try statement.
Select allOpen in new window