We help IT Professionals succeed at work.

Can't pass text from ComboBox to TextBox

Member_2_4286571
on
Medium Priority
279 Views
Last Modified: 2012-05-12
Hi, for some reason and i am not sure why I am getting error (  Object reference not set to an instance of an object. ).

I am using this code to fill ComboBox with all printers installed on the system:
 Dim pkInstalledPrinters As String

        ' Find all printers installed
        For Each pkInstalledPrinters In _
            PrinterSettings.InstalledPrinters
            cmbPrinteri.Items.Add(pkInstalledPrinters)
        Next pkInstalledPrinters

        ' Set the combo to the first printer in the list
        cmbPrinter.SelectedIndex = 0
        PrinterSelection = 3

Open in new window


Then i have some Case Select

Select Case PrinterSelection
            Case 0
                txtPOSMain.Text = cmbPrinter.Text
            Case 1
                txtPOSSecond.Text = cmbPrinter.Text
            Case 2
             ...

Open in new window


I am getting error  in any of the  txtPOSMain.Text = cmbPrinter.Text,  txtPOSSecond.Text = cmbPrinter.Text...

If i put MsgBox(cmbPrinter.text) I am getting result (Generic/Text Only) as that is selected printer.
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
Have you stepped through the code to see which object is null?

Author

Commented:
Yes, everything works fine until it reaches textBox.

I tried this:
Dim SelectedPrinter As String
 MsgBox(cmbPrinter.Text) - OK (MsgBox showing Generic /Text Only)
                SelectedPrinter = cmbPrinter.Text - OK (SelectedPrinter holds Generic /Text Only)
                txtPOSMain.Text = SelectedPrinter - Error
                txtPOSMain.Text = cmbPrinter.Text - Also Error

Author

Commented:
It's an ordinary TextBox, just dropped from ToolBox. Will try to fill manually ComboBox now.

Author

Commented:
Ok found it, it was some problem with loading null value from database during form load event and setting null value in txtPOSmain.text, however no errors reported there. Funny i never thought that you can't overwrite textbox with another data if there is null value from form load.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.