Avatar of MTecho
MTecho
 asked on

Print server problem

A new printer setup causes an "Object reference not set to an instance of an object" error.

The print server is a Fuji-Xerox Fiery SP4000 PS v1.0. Previous to using this print server, my application worked fine.

My application sends documents to the printer, alternating between two trays (different paper media). When printing the documents, the user prints test page from each tray. The PrinterSettings from each printer dialog are then used for the main documents being printed.

The error message above occurs when the second document is printed, when the docLabel.Print() statement is executed. docLabel is the PrintDocument.
The error cannot be trapped in the docLabel_PrintPage routine, so I am guessing that the problem occurs in the line:
Private Sub docLabel_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles docLabel.PrintPage

Would either of the variables "sender" or "e" be Nothing?
What can be done?
Visual Basic.NET

Avatar of undefined
Last Comment
MTecho

8/22/2022 - Mon
DjDezmond

When you call "docLabel.Print", you don't need to specify "Sender" or "e".

Something is being referenced that doesn't exist.

Just to clarify, you use the same PrintDocument for both pages, but use different dialog controls to specify the settings?

Has any of your code changed since you installed this new printer setup?

It just looks like you've declared an object somewhere without the 'New' prefix, but if your code hasn't changed since the new hardware setup, then you could be calling or referencing something that corresponded to a setting or physical device thats no longer there.

You may have to post the code so we can look at it...

Dez
MTecho

ASKER
Thanks for your response, Dez.

I use separate PrintDocuments for each paper tray in the printer.
The two print documents copy their PrintSettings from the form that does the test prints. This is the New initialisation for the form that prints the main documents (this is the relevant code):

    Private dabr As SqlClient.SqlDataAdapter, dtbr As DataTable, NextBranchItem As DataRow
    Private dard As SqlClient.SqlDataAdapter, dtrd As DataTable, NextRoundItem As DataRow
    Private dalb As SqlClient.SqlDataAdapter, dtlb As DataTable, NextLabelItem As DataRow

    Public Sub New(ByVal LabelPrinter As PrintDialog, ByVal PickListPrinter As PrintDialog)
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        Try
            ...
            docLabel.PrinterSettings = LabelPrinter.PrinterSettings
            docPickList.PrinterSettings = PickListPrinter.PrinterSettings
            ...
        Catch ex As Exception
            ...
        End Try
    End Sub

I use different forms to print the test pages and the main documents so the form that prints the main documents can be called multiple times in a user session, while printing the test pages only once.

I did not change any code when the new printer setup was made, though I have made some to try to isolate the error. The only code I have added is of the type "If docLabel Is Nothing Then ... Else ... End If" and an extra "Try ... Catch ... End Try" where it did not exist before. The PrintDocument docLabel is definitely not Nothing when the call docLabel.Print() is made. This call is made in the usual way, so the variables "sender" and "e" are system-generated, as they have been.

The error gets trapped in the print button routine:

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim SQL As String, da As SqlClient.SqlDataAdapter, dtp As DataTable, Cmd As SqlClient.SqlCommand
        '
        Try
            SQL = sql statement
            '
            Cmd = New SqlClient.SqlCommand(SQL, Conn)
            da = New SqlClient.SqlDataAdapter(Cmd)
            Cmd = Nothing
            dtp = New DataTable
            da.Fill(dtp)
            For Each drp As DataRow In dtp.Rows
                ...
                make dtbr table
                make dtrd table
                ...
                            docPickList.Print()                                                ' plain paper
                            MyLib.TimeFn.Wait(3000)
                ...
                make dtlb table
                ...
                            NextLabelItem = Nothing
                            If dtlb.Rows.Count > 0 Then
                                If Not docLabel Is Nothing Then docLabel.Print() ' <<<<< error occurs here
                                MyLib.TimeFn.Wait(3000)
                            End If
                ...
            Next
        Catch ex As Exception
            ... display error message
        End Try
    End Sub

I have found that a 3-second delay is necessary between sending documents to a networked printer to preserve the printing sequence of the documents (this was before the new printer setup).

The error cannot be trapped in the PrintDocument's PrintPage event, as it occurs before the Try statement:

    Private Sub docLabel_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles docLabel.PrintPage
        Try
            e.HasMorePages = DrawLabels(e.Graphics, e.PageBounds.Width, e.PageBounds.Height, NextLabelItem)
        Catch ex As Exception
            ... display error message
        End Try
    End Sub

NextLabelItem is a DataRow in table dtlb (see above) and is referenced with ByRef in the DrawLabels routine. The contents of dtlb are different for each drp item. The DrawLabels routine cycles through the dtlb table and only returns False when it gets to the end of the dtlb table (one page of labels per DataRow). It starts printing when NextLabelItem is Nothing or the DataRow in the dtlb table matches NextLabelItem. When DrawLabels return True, NextLabelItem is set to the DataRow following the one just printed.

I hope this helps.
ASKER CERTIFIED SOLUTION
DjDezmond

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
MTecho

ASKER
Sorry about the delay in responding - had to work on something else...

I put a MsgBox("OK to here") right at the beginning of the "docLabel_PrintPage" routine, and it never appear. The error must occur when the "docLabel.Print()" statement is executed.

I used docLabel.PrinterSettings.InstalledPrinters, and "Fiery SP4000 PS v1.0" is still present. I have not yet checked all the printer settings - will do next.

This is where the dlgLabel printer settings are set before this main form is spawned - no error occurs here (the other dialogs are set in similar fashion):

    Private Sub mnuLabelsPrinter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuLabelsPrinter.Click
        Try
            If Not dlgLabel Is Nothing Then
                If MsgBox("Click ""OK"" to close this message and open the printer dialog.", vbInformation + vbOKCancel, "Labels printer:") = vbCancel Then Exit Sub
                If dlgLabel.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                    PrintersUpdated = True
                    docLabel.Print()                                                                         ' this is on a different form from the code shown earlier
                    MsgBox("Labels printer test complete.", vbInformation, "Print test")
                    LabelsPrinterSet = True
                End If
            End If
        Catch ex As Exception
            MessageBox.Show("An error occurred printing the file - " + ex.Message)
        End Try
    End Sub

Thanks.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
MTecho

ASKER
Are you still there, Dez?

Here is my Debug output:
The printer settings are shown for each loop at the top of my first comment in this exchange (see above). As you see, the main loop has gone twice around: a PickList doc, then a Label doc, next loop a PickList doc, then ERROR on the next attempt to print a label doc.

DocumentName = Epsom Delivery Branch Epp032 pick list: PrinterSettings =========================================
InstalledPrinters = 12
PaperSources = 8
PaperSizes = 58
IsValid = True
DefaultPageSettings.PaperSource = [PaperSource Tray 4 Kind=Custom]
DefaultPageSettings.PaperSize = [PaperSize A4 Kind=A4 Height=1169 Width=827]

The thread '<No Name>' (0x1a4) has exited with code 0 (0x0).
DocumentName = Epsom Delivery Branch Epp032 labels: PrinterSettings =========================================
InstalledPrinters = 12
PaperSources = 8
PaperSizes = 58
IsValid = True
DefaultPageSettings.PaperSource = [PaperSource Tray 3 Kind=Custom]
DefaultPageSettings.PaperSize = [PaperSize A4 Kind=A4 Height=1169 Width=827]

The thread '<No Name>' (0x710) has exited with code 0 (0x0).
DocumentName = Epsom Delivery Branch Epp040 pick list: PrinterSettings =========================================
InstalledPrinters = 12
PaperSources = 8
PaperSizes = 58
IsValid = True
DefaultPageSettings.PaperSource = [PaperSource Tray 4 Kind=Custom]
DefaultPageSettings.PaperSize = [PaperSize A4 Kind=A4 Height=1169 Width=827]

The thread '<No Name>' (0x260) has exited with code 0 (0x0).
DocumentName = Epsom Delivery Branch Epp040 labels: PrinterSettings =========================================
InstalledPrinters = 12
PaperSources = 8
PaperSizes = 58
IsValid = True
DefaultPageSettings.PaperSource = [PaperSource Tray 3 Kind=Custom]
DefaultPageSettings.PaperSize = [PaperSize A4 Kind=A4 Height=1169 Width=827]

>>>>>>>>>>>>>>>>>>>>>> After error <<<<<<<<<<<<<<<<<<<<<
ex.Message: Object reference not set to an instance of an object.
PickListSettings: PrinterSettings =========================================
InstalledPrinters = 12
PaperSources = 0
PaperSizes = 0
IsValid = False

DocumentName = Epsom Delivery Branch Epp040 pick list: PrinterSettings =========================================
InstalledPrinters = 12
PaperSources = 0
PaperSizes = 0
IsValid = False

LabelSettings: PrinterSettings =========================================
InstalledPrinters = 12
PaperSources = 0
PaperSizes = 0
IsValid = False

DocumentName = Epsom Delivery Branch Epp040 labels: PrinterSettings =========================================
InstalledPrinters = 12
PaperSources = 0
PaperSizes = 0
IsValid = False

The PickListSettings: PrinterSettings and LabelSettings: PrinterSettings are cloned to new PrintDialog objects from the original settings when entering this spawned form, and are valid at that time. As they also become invalid when the error occurs, it is not possible to recopy the PrinterSettings from these new PrintDialog objects.

The sequence of events can vary from time to time, and one time I got the error message "Tried to access printer 'Fiery SP4000 PS v1.0' with invalid settings." instead of "Object reference not set to an instance of an object.".

Also, I have not mentioned before, on exiting the application there is the error message:

TITLE: ".NET-BroadcastEventWindow.1.0.5000.0.3: Print.exe - Application Error"

The instruction at "0x77fcb8f1" referenced memory at "0x094c6578". The memory could not be "read".

Click on OK to terminate the program

The same message appears thre times.

What can be done to prevent the printer becoming invalid?
MTecho

ASKER
The computer running this application runs Windows 2000, and the Fiery print server runs Windows XP.
MTecho

ASKER
No further comments.... I'll just have to close off and re-ask the question.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.