Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Converting C# to VB.net but code gives null reference

Converting C# to VB.net but code gives null reference

Scheduled Pinned Locked Moved Visual Basic
helpcsharphardwareworkspace
12 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Shane James

    Hi Please forgive me, this may be a simple answer for some. I have self taught myself VB.net and have developed some software for myself. I often get code samples for hardware that i use, but it is in C#. I have used code converters, but I still seem to get issues. The major issue I have is below. The C# code works perfectly, but the VB code gives me a "Object reference not set to an instance of an object.'" error when I call the connect sub. Please help. C# Code

    using SPORTident;
    using System.Windows.Forms;
    using SPORTident.Communication;
    using System;

    namespace SportidentMinimal
    {
    public partial class MainForm : Form
    {
    public Reader reader;

        public MainForm()
        {
            InitializeComponent();
    
            reader = new Reader
            {
                WriteBackupFile = true,
                BackupFileName = System.IO.Path.Combine(Environment.CurrentDirectory, $@"backup\\{DateTime.Now:yyyy-MM-dd}\_stamps.bak")
            };
    
            reader.DeviceConfigurationRead += new DeviceConfigurationReadEventHandler(reader\_DeviceConfigurationRead);
            reader.InputDeviceChanged += new ReaderDeviceChangedEventHandler(reader\_InputDeviceChanged);
            reader.InputDeviceStateChanged += new ReaderDeviceStateChangedEventHandler(reader\_InputDeviceStateChanged);
            reader.CardRead += new SPORTident.DataReadCompletedEventHandler(reader\_CardRead);
        }
    
        private void reader\_DeviceConfigurationRead(object sender, StationConfigurationEventArgs e)
        {
            writeLog("StationConfigRead, OperatingMode: " + e.Device.OperatingMode);
            writeLog("StationConfigRead, LegacyProtocolMode: " + e.Device.LegacyProtocolMode);
            writeLog("StationConfigRead, AutoSend: " + e.Device.AutoSendMode);
            writeLog("StationConfigRead, FirmwareVersion: " + e.Device.FirmwareVersion);
            writeLog("StationConfigRead, CodeNumber: " + e.Device.CodeNumber);
            writeLog("StationConfigRead, SerialNumber: " + e.Device.SerialNumber);
        }
    
        private void reader\_InputDeviceStateChanged(object sender, ReaderDeviceStateChangedEventArgs e)
        {
            writeLog("InputDeviceStateChanged: " + e.PreviousState + " => " + e.CurrentState);
        }
    
        private void reader\_InputDeviceChanged(object sender, ReaderDeviceChangedEventArgs e)
        {
            writeLog("InputDeviceChanged: " + e.PreviousDevice + " => " + e.CurrentDevice);
        }
    
        private
    
    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    Please edit your question and show us where the exception occurs.

    1 Reply Last reply
    0
    • S Shane James

      Hi Please forgive me, this may be a simple answer for some. I have self taught myself VB.net and have developed some software for myself. I often get code samples for hardware that i use, but it is in C#. I have used code converters, but I still seem to get issues. The major issue I have is below. The C# code works perfectly, but the VB code gives me a "Object reference not set to an instance of an object.'" error when I call the connect sub. Please help. C# Code

      using SPORTident;
      using System.Windows.Forms;
      using SPORTident.Communication;
      using System;

      namespace SportidentMinimal
      {
      public partial class MainForm : Form
      {
      public Reader reader;

          public MainForm()
          {
              InitializeComponent();
      
              reader = new Reader
              {
                  WriteBackupFile = true,
                  BackupFileName = System.IO.Path.Combine(Environment.CurrentDirectory, $@"backup\\{DateTime.Now:yyyy-MM-dd}\_stamps.bak")
              };
      
              reader.DeviceConfigurationRead += new DeviceConfigurationReadEventHandler(reader\_DeviceConfigurationRead);
              reader.InputDeviceChanged += new ReaderDeviceChangedEventHandler(reader\_InputDeviceChanged);
              reader.InputDeviceStateChanged += new ReaderDeviceStateChangedEventHandler(reader\_InputDeviceStateChanged);
              reader.CardRead += new SPORTident.DataReadCompletedEventHandler(reader\_CardRead);
          }
      
          private void reader\_DeviceConfigurationRead(object sender, StationConfigurationEventArgs e)
          {
              writeLog("StationConfigRead, OperatingMode: " + e.Device.OperatingMode);
              writeLog("StationConfigRead, LegacyProtocolMode: " + e.Device.LegacyProtocolMode);
              writeLog("StationConfigRead, AutoSend: " + e.Device.AutoSendMode);
              writeLog("StationConfigRead, FirmwareVersion: " + e.Device.FirmwareVersion);
              writeLog("StationConfigRead, CodeNumber: " + e.Device.CodeNumber);
              writeLog("StationConfigRead, SerialNumber: " + e.Device.SerialNumber);
          }
      
          private void reader\_InputDeviceStateChanged(object sender, ReaderDeviceStateChangedEventArgs e)
          {
              writeLog("InputDeviceStateChanged: " + e.PreviousState + " => " + e.CurrentState);
          }
      
          private void reader\_InputDeviceChanged(object sender, ReaderDeviceChangedEventArgs e)
          {
              writeLog("InputDeviceChanged: " + e.PreviousDevice + " => " + e.CurrentDevice);
          }
      
          private
      
      R Offline
      R Offline
      Ralf Meier
      wrote on last edited by
      #3

      Do you have an Item selected inside the Combobox (cbbSportIdentDevices.SelectedItem) ? What do you see by using the Debugger (in case of exeption) ?

      S 1 Reply Last reply
      0
      • R Ralf Meier

        Do you have an Item selected inside the Combobox (cbbSportIdentDevices.SelectedItem) ? What do you see by using the Debugger (in case of exeption) ?

        S Offline
        S Offline
        Shane James
        wrote on last edited by
        #4

        Hi Ralf Yes I select a Com port. For my current scenario it happens to be COM5. Its weird that it all works perfectly in C#, but in my project, when I have converted to VB, it gives this exception.

        R 1 Reply Last reply
        0
        • S Shane James

          Hi Ralf Yes I select a Com port. For my current scenario it happens to be COM5. Its weird that it all works perfectly in C#, but in my project, when I have converted to VB, it gives this exception.

          R Offline
          R Offline
          Ralf Meier
          wrote on last edited by
          #5

          Hi Shane, I never thought that you assign a Com-Port at this place. So ... there are some Information missing to go further. How is ReaderDeviceInfo declared ? How and where do you have the COM-Port in your code ? Where do you assign an initialize it ? I think that are parts which belong to the Designer-Scripts from the C#-Project - you should look also there and perhaps complete your code. If not try to answer my questions ...

          S 1 Reply Last reply
          0
          • R Ralf Meier

            Hi Shane, I never thought that you assign a Com-Port at this place. So ... there are some Information missing to go further. How is ReaderDeviceInfo declared ? How and where do you have the COM-Port in your code ? Where do you assign an initialize it ? I think that are parts which belong to the Designer-Scripts from the C#-Project - you should look also there and perhaps complete your code. If not try to answer my questions ...

            S Offline
            S Offline
            Shane James
            wrote on last edited by
            #6

            Perhaps little background will help as I can't post an image of the form. I have an RFID reader which connects to the computer via a usb. The sample app from the supplier (the c# Code). Just provides a list of available devices in a combobox after clicking the refresh button. This all works in my VB code as well and after clicking the button it shows a list of (COM1 and COM5 on my laptop) in the combobox. After selecting the correct COM from the dropdown (COM5 in my case). You then click the connect button and it is at this point that my VB code falls over, but the C# code works fine. The form is very simple, I have compared the designer code and nothing different there either. I'm just very confused as to why the VB code gives the null exception but the C# code doesn't. Yet the code is almost identical. The writelog is just a list box which provides a log of all the steps taken and events which are initiated by the reader. Hope this helps Regards Shane

            R L 2 Replies Last reply
            0
            • S Shane James

              Perhaps little background will help as I can't post an image of the form. I have an RFID reader which connects to the computer via a usb. The sample app from the supplier (the c# Code). Just provides a list of available devices in a combobox after clicking the refresh button. This all works in my VB code as well and after clicking the button it shows a list of (COM1 and COM5 on my laptop) in the combobox. After selecting the correct COM from the dropdown (COM5 in my case). You then click the connect button and it is at this point that my VB code falls over, but the C# code works fine. The form is very simple, I have compared the designer code and nothing different there either. I'm just very confused as to why the VB code gives the null exception but the C# code doesn't. Yet the code is almost identical. The writelog is just a list box which provides a log of all the steps taken and events which are initiated by the reader. Hope this helps Regards Shane

              R Offline
              R Offline
              Ralf Meier
              wrote on last edited by
              #7

              Hi Shane, when I compare both codes (C# vs. VB) I can't see any relevant difference. For me both codes should do the same. But any difference must be there ... So : what about the Visual Studio Settings ? Have you added the required DDL's to the references (I hope the name is right - In German it's called 'Verweis') of your Project ? Greetings Ralf

              S 2 Replies Last reply
              0
              • R Ralf Meier

                Hi Shane, when I compare both codes (C# vs. VB) I can't see any relevant difference. For me both codes should do the same. But any difference must be there ... So : what about the Visual Studio Settings ? Have you added the required DDL's to the references (I hope the name is right - In German it's called 'Verweis') of your Project ? Greetings Ralf

                S Offline
                S Offline
                Shane James
                wrote on last edited by
                #8

                Yes, all dll's have been added. Very confusing to me, why it doesnt work

                1 Reply Last reply
                0
                • R Ralf Meier

                  Hi Shane, when I compare both codes (C# vs. VB) I can't see any relevant difference. For me both codes should do the same. But any difference must be there ... So : what about the Visual Studio Settings ? Have you added the required DDL's to the references (I hope the name is right - In German it's called 'Verweis') of your Project ? Greetings Ralf

                  S Offline
                  S Offline
                  Shane James
                  wrote on last edited by
                  #9

                  If I put the reader = New Reader() in the connect sub, the error goes away, but nothing works like it should. it doesnt pick up the events or anything

                  Private Sub connect()
                      reader = New Reader()
                      Dim readerDeviceInfo As ReaderDeviceInfo = CType((cbbSportIdentDevices.SelectedItem), ReaderDeviceInfo)
                      reader.InputDevice = readerDeviceInfo
                      reader.OutputDevice = New ReaderDeviceInfo(ReaderDeviceType.None)
                      reader.OpenInputDevice()
                      reader.OpenOutputDevice()
                  
                  End Sub
                  
                  R P 2 Replies Last reply
                  0
                  • S Shane James

                    If I put the reader = New Reader() in the connect sub, the error goes away, but nothing works like it should. it doesnt pick up the events or anything

                    Private Sub connect()
                        reader = New Reader()
                        Dim readerDeviceInfo As ReaderDeviceInfo = CType((cbbSportIdentDevices.SelectedItem), ReaderDeviceInfo)
                        reader.InputDevice = readerDeviceInfo
                        reader.OutputDevice = New ReaderDeviceInfo(ReaderDeviceType.None)
                        reader.OpenInputDevice()
                        reader.OpenOutputDevice()
                    
                    End Sub
                    
                    R Offline
                    R Offline
                    Ralf Meier
                    wrote on last edited by
                    #10

                    Let's get back to the ComboBox and it's ReaderDeviceInfo ... I can't see a code-line where the ComboBox is assigned/filled AND with what it is filled. I suggest that you should look there ...

                    1 Reply Last reply
                    0
                    • S Shane James

                      If I put the reader = New Reader() in the connect sub, the error goes away, but nothing works like it should. it doesnt pick up the events or anything

                      Private Sub connect()
                          reader = New Reader()
                          Dim readerDeviceInfo As ReaderDeviceInfo = CType((cbbSportIdentDevices.SelectedItem), ReaderDeviceInfo)
                          reader.InputDevice = readerDeviceInfo
                          reader.OutputDevice = New ReaderDeviceInfo(ReaderDeviceType.None)
                          reader.OpenInputDevice()
                          reader.OpenOutputDevice()
                      
                      End Sub
                      
                      P Offline
                      P Offline
                      Pawel Wzietek
                      wrote on last edited by
                      #11

                      Shane James wrote:

                      If I put the reader = New Reader() in the connect sub, the error goes away, but nothing works like it should. it doesnt pick up the events or anything

                      This is normal: you create a new instance of the Reader class but this one is not configured like the first one was in the constructor of Results_Capture_Enduro. But I don't think the problem is here, you should remove this line. You did not tell us which variable triggers the exception. To find it you can remove the try...catch block around the call to connect() to let the debugger stop at the line the exception occurs. There is one line where the VB code is not equivalent to its C# counterpart:

                      Dim readerDeviceInfo As ReaderDeviceInfo = CType((cbbSportIdentDevices.SelectedItem), ReaderDeviceInfo)

                      The strictly equivalent version would be:

                      Dim readerDeviceInfo As ReaderDeviceInfo = DirectCast((cbbSportIdentDevices.SelectedItem), ReaderDeviceInfo)

                      See this article: DirectCast vs. CType[^] for explanations. In general using CType for a simple type cast is dangerous because it will try to do something even if the cast is not possible, so that the error may propagate to other parts of the code where it is difficult to understand. NB. Also be careful when translating from C# to VB.NET as the latter is not really case sensitive which may cause compilation errors: vb.net - Is VB really case insensitive? - Stack Overflow[^]

                      1 Reply Last reply
                      0
                      • S Shane James

                        Perhaps little background will help as I can't post an image of the form. I have an RFID reader which connects to the computer via a usb. The sample app from the supplier (the c# Code). Just provides a list of available devices in a combobox after clicking the refresh button. This all works in my VB code as well and after clicking the button it shows a list of (COM1 and COM5 on my laptop) in the combobox. After selecting the correct COM from the dropdown (COM5 in my case). You then click the connect button and it is at this point that my VB code falls over, but the C# code works fine. The form is very simple, I have compared the designer code and nothing different there either. I'm just very confused as to why the VB code gives the null exception but the C# code doesn't. Yet the code is almost identical. The writelog is just a list box which provides a log of all the steps taken and events which are initiated by the reader. Hope this helps Regards Shane

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #12

                        Shane James wrote:

                        it shows a list of (COM1 and COM5 on my laptop) in the combobox.

                        I am going to guess that it does not contain a list of strings that represent comports, but objects. Show us how the VB version that initializes the combobox looks like.

                        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups