Printer setting changes getting NilObjectException error

Wrote an Application that prints special labels, entry and printing worked perfectly.
Created an executable, again all working fine.
The next week I tried to run the executable and print, I got an exception of class NilObjectException was not handled.
Tried running the program in xojo, I had some problems with it stopping at the following command

dim w as WindowsPrinterMBS = WindowsPrinterMBS.OpenPrinter(Name)
if w.ChangePrinterSettings(d, 2) Then

Made some changes and fixed the error and it was printing correctly again, built an executable and got the NilObjectException error again.

Does anyone have any suggestions? Following is the code.

dim OrgDfltPrt as String
dim MyShell as new shell

Myshell.execute “wmic printer where default=‘TRUE’ get name”
DfltPrt = Myshell.Result

dim d as new WindowsDeviceModeMBS
dim Name as string

Name = DfltPrt
// here we define which values we want to change
d.Fields = d.DM_PAPERSIZE + d.DM_PAPERLENGTH + d.DM_PAPERWIDTH + d.DM_PRINTQUALITY
d.Papersize = 256
d.Paperlength = ContinuousLab_Length * 254 ’ continuousLab_Length = 1" to 20"
d.PaperWidth = 3.54 * 254
d.PrintQuality = 300

dim w as WindowsPrinterMBS = WindowsPrinterMBS.OpenPrinter(Name)
if w.ChangePrinterSettings(d, 2) Then
MessageBox(“Error - could not set the page length.”)
exit sub
end if

dim g as Graphics
dim ps as PrinterSetup

ps = new PrinterSetup
if ps <> nil then

ps.MaximumHorizontalResolution = -1
ps.MaximumVerticalResolution = -1
g = openprinter(ps)
If g <> Nil Then
PrintLabel(g, Ps)
call ClearPrintWin
if OrgDfltPrt <> DfltPrinter then
call ChangeDefaultPrinter(OrgDfltPrt)
end if

if EntryStatus = 2 then
  call ExitDieCont
end if

end if
end if

I’d be putting in some system.debuglog debugging and then build it and use DebugView to see what messages the debuglog reveals

Norman, thank you.

I did try the following and it works fine in Xojo, but not when you build it and run the app.

Try

dim w as WindowsPrinterMBS = WindowsPrinterMBS.OpenPrinter(Name)
if w.ChangePrinterSettings(d, 2) = True Then
MessageBox(“Error - could not set the page length.”)
exit sub
end if

Catch e As RunTimeException

MessageBox(“Error - There was a problem changing the printer settings.”)

exit sub
End Try

system.debuglog you can print out any message you want about what is or is not nil when you get the error

maybe in a build wmic doen return anything and then the OpenPrinter call fails because there is no such printer ?
maybe print the result from the call to the shell and see what printer name it gets ?

at least then will know better why it fails in a built app

Norman,

I have done this and the printer did exist and was the correct printer, still problems.
However, I reinstalled the Monkeybread Plugins and everything corrected itself.

Thank you so much for helping out, I do appreciate it.

reinstalling them fixed it ?
wow

I don’t understand it either, however that is the only thing I did after the last failure, and it worked.
I’m surprised as well.

Try in a machine where no default Printer is set; if you get the Nil, you will knowt the error, maybe.