none
Variable X is passed by reference before it has been assigned a value.A null reference exception could result at runtime.

    Question

  • Hi

       I had a backup project from my partner and we need fix to restore. On backup code had an error (Variable X  is passed by reference before it has been assigned a value. A null reference exception could result at runtime.) and I can't compile please help

    I'm new on programming language WPF

    Part code:

    Public Function getScalingFactorTagValue(ByVal ItemServerHandle As Integer) As Integer
            Dim ReadScalingFactor As Integer = 1
            ' DRJ 3-31-17
          
            Try
                ' Read only 1 item
                Dim ItemCount As Short = 1
                ' Provide storage the arrays returned by the 'SyncRead' method
                Dim SyncItemServerHandles(1) As Integer
                Dim SyncItemValues As System.Array
                Dim SyncItemServerErrors As System.Array
                ' Get the Servers handle for the desired item.  The server handles were
                ' returned in add item subroutine.
                SyncItemServerHandles(1) = ItemServerHandle ' TODO modify this to be index 1?
                ' Invoke the SyncRead operation.  Remember this call will wait until completion.
                MeterMeasurementsOPCGroup.SyncRead(OPCAutomation.OPCDataSource.OPCCache, ItemCount, SyncItemServerHandles, SyncItemValues, SyncItemServerErrors)
                ' Save off the value returned after checking for error
                If SyncItemServerErrors(1) = 0 Then
                    If IsArray(SyncItemValues(1)) Then
                        Dim ItsAnArray As Array
                        Dim x As Integer
                        Dim Suffix As String
                        ItsAnArray = SyncItemValues(1)
                        For x = ItsAnArray.GetLowerBound(0) To ItsAnArray.GetUpperBound(0)
                            If x = ItsAnArray.GetUpperBound(0) Then
                                Suffix = ""
                            Else
                                Suffix = ", "
                            End If
                            ReadScalingFactor = _
                            ReadScalingFactor & ItsAnArray(x) & CInt(Suffix)
                        Next x
                    Else
                        ReadScalingFactor = SyncItemValues(1)
                        ' ' Console.WriteLine("Manual read value: " + CStr(SyncItemValues(1)))
                    End If
                Else
                    Dim dialog As MetroDialogWindow = New MetroDialogWindow()
                    dialog.Owner = Application.Current.MainWindow
                    dialog.Content = vbNewLine + vbNewLine
                    dialog.HasCancelButton = False
                    dialog.Content += "SyncItemServerError: " + SyncItemServerErrors(1)
                    dialog.ShowInTaskbar = False
                    dialog.ShowDialog()
                End If
            Catch ex As Exception
                ' Error handling
                Dim dialog As MetroDialogWindow = New MetroDialogWindow()
                dialog.Owner = Application.Current.MainWindow
                dialog.Content = vbNewLine + vbNewLine
                dialog.HasCancelButton = False
                dialog.Content += "OPC server read item failed with exception: " + ex.Message
                dialog.ShowInTaskbar = False
                dialog.ShowDialog()
            End Try

    • Moved by CoolDadTxMVP Friday, March 31, 2017 7:52 PM VB related
    Friday, March 31, 2017 6:36 PM

All replies

  • Public Function getScalingFactorTagValue(ByVal ItemServerHandle As Integer) As Integer
            Dim ReadScalingFactor As Integer = 1
            ' DRJ 3-31-17
          
            Try
                ' Read only 1 item
                Dim ItemCount As Short = 1
                ' Provide storage the arrays returned by the 'SyncRead' method
                Dim SyncItemServerHandles(1) As Integer
                Dim SyncItemValues As System.Array
                Dim SyncItemServerErrors As System.Array
                ' Get the Servers handle for the desired item.  The server handles were
                ' returned in add item subroutine.
                SyncItemServerHandles(1) = ItemServerHandle ' TODO modify this to be index 1?
                ' Invoke the SyncRead operation.  Remember this call will wait until completion.
                MeterMeasurementsOPCGroup.SyncRead(OPCAutomation.OPCDataSource.OPCCache, ItemCount, SyncItemServerHandles, SyncItemValues, SyncItemServerErrors)

                ' Save off the value returned after checking for error
                If SyncItemServerErrors(1) = 0 Then
                    If IsArray(SyncItemValues(1)) Then
                        Dim ItsAnArray As Array

    ...

    You appear to have stumbled into the wrong forum. This one is for issues
    related to the C# (C Sharp) programming language. The code you posted
    appears to be Basic. A better forum would be:

    Visual Studio Languages > Visual Basic
    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vbgeneral

    - Wayne

    Friday, March 31, 2017 7:10 PM
  • Hello,

    Not sure what line of code you are talking about but even so, why not use assertion before sending the variable to another method or in the method check if it's null/nothing before using it?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, March 31, 2017 8:04 PM
    Moderator
  • Hi

    It would be nice if you could use the code block tool in the toolbar to post code.

    One possibility.

    Remove:   

    Dim x As Integer

    and change:

    For x = ItsAnArray.GetLowerBound(0) To ItsAnArray.GetUpperBound(0)
    
    to  
    
    For X as integer = ItsAnArray.GetLowerBound(0) To ItsAnArray.GetUpperBound(0)

    It may also be useful to insert these three lines at the very top of your code - this will bring errors to your attention.

    Option Strict On
    Option Explicit On
    Option Infer Off
    



    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, March 31, 2017 9:58 PM
    Friday, March 31, 2017 9:56 PM
  • Thanks

      I'm new on programming world !I will contact you directly by FB

    Monday, April 3, 2017 9:33 PM
  • (Variable X  is passed by reference before it has been assigned a value. A null reference exception could result at runtime.) and I can't compile please help

    You declare the variables for your arrays:

                Dim SyncItemValues As System.Array
                Dim SyncItemServerErrors As System.Array

    but you never create the actual arrays.  That problem might cause an error when you pass the variables to the method:

    MeterMeasurementsOPCGroup.SyncRead(OPCAutomation.OPCDataSource.OPCCache, _
    ItemCount, SyncItemServerHandles, SyncItemValues, SyncItemServerErrors)

    however that depends on the internal operation of the method. The problem will not stop your code from compiling.  There must be other errors elsewhere that stop the code compiling.  Post the exact error message (not a summary) and identify the line of code that causes the error.
    Monday, April 3, 2017 9:59 PM
  • Which version of VB do you use. A while there was a Warning (be aware a warning) likewise this. 

    There was thought that VB needed always instancing of value types while this is done in VB automatically. 

    Also put in top of this code Option Strict On because it is now trying to behave as VB6, that mean fixing things at runtime which better can be solved at design time. Your code looks very C#'s. Try to use VB complete VB code for better performance and stabler running. 

    For instance a string concatinator in VB is a "&" and "&="


    Success
    Cor


    • Edited by Cor LigthertMVP Tuesday, April 4, 2017 7:18 AM mind twist &= was typed as =&
    Tuesday, April 4, 2017 6:02 AM
  • For instance a string concatinator in VB is a "&" and "=&"

    <cough>

    &= Operator (Visual Basic)
    https://msdn.microsoft.com/en-us/library/fz1zs43f.aspx

    - Wayne

    Tuesday, April 4, 2017 7:14 AM
  • Thanks,

    Changed


    Success
    Cor

    Tuesday, April 4, 2017 7:18 AM