dcsimg
Results 1 to 4 of 4

Thread: Getting errors when running my Visual Basic application

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2018
    Posts
    3

    Getting errors when running my Visual Basic application

    I get this error, can you guys help? someone else wrote this code before I started working here and now its not working so im trying to figure it out. Thanks so much

    Here is the error I get.
    Code:
    ************** Exception Text **************
    System.NullReferenceException: Object reference not set to an instance of an object.
       at ERX_Ad_Hoc.Form1.ASCIIsumOfString(String[] ArrayString) in P:\AD_HOC ERX POLLING\ERX Ad Hoc\Form1.vb:line 172
       at ERX_Ad_Hoc.Form1.OpenExcelWkbk_Click(Object sender, EventArgs e) in P:\AD_HOC ERX POLLING\ERX Ad Hoc\Form1.vb:line 147
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    Here is the Actual Code, I'll Bold the actual lines the error is referring to. Thanks so much

    Code:
        Private Sub OpenExcelWkbk_Click(sender As Object, e As EventArgs) Handles OpenExcelWkbk.Click
            Dim xlApp As Object = CreateObject("Excel.Application") 'Creates Excel Object
            Dim xlWorkbook As Object = xlApp.Workbooks.Open("""G:\ERX Trending.xlsm""") 'Opens Workbook
            Dim xlSheet As Object = xlWorkbook.Worksheets("Historical Data") 'Opens Worksheets
            xlSheet.Range("B1").Value = ComboBox1.SelectedItem 'Puts ERX in ComboBox into ERX Selection in Spreadsheet
            Dim DateEnd As Date = Date.Today 'Gets Todays Date
            Dim DateStart As Date = DateEnd.AddDays(-1) 'Gets Yesterdays Date
            Dim Time = Date.Now.ToShortTimeString 'Gets current time
            xlSheet.Range("D1").Value = DateStart 'Sets Time/Date Options for Last 24 Hours
            xlSheet.Range("D2").Value = DateEnd 'Sets Time/Date Options for Last 24 Hours
            xlSheet.Range("F1").Value = Time 'Sets Time/Date Options for Last 24 Hours
            xlSheet.Range("F2").Value = Time 'Sets Time/Date Options for Last 24 Hours
            xlSheet = xlWorkbook.Worksheets("Lists") 'Moves to hidden "Lists" Worksheet
            Dim Counter = 0 'Creates Counter
            Dim CellValue = xlSheet.Range("A1").VALUE 'Get Value from cell A1 in the hidden "Lists" Worksheet
            Dim ExcelList(500) As String
            While CellValue <> Nothing 'This will loop while there is a value in cell A1
                CellValue = xlSheet.Range("A" & (Counter + 1).ToString).VALUE 'cell A1 value
                ExcelList(Counter) = CellValue
                Counter = Counter + 1
            End While
            Array.Resize(ExcelList, Counter)  <--- Line 147
            Dim ExcelByte = ASCIIsumOfString(ExcelList)
            Dim ERXByte = ASCIIsumOfString(ERX_Arrays.objArray)
            If ExcelByte <> ERXByte Then
                Counter = 0
                While Counter <= ERX_Arrays.objArray.Length - 1 'While Loop Updates ERX Spreadsheet List
                    xlSheet.Range("A" & (Counter + 1).ToString).Value = ERX_Arrays.objArray(Counter)
                    Counter = Counter + 1
                End While
            End If
            xlWorkbook.Save() 'Saves Workbook
            xlWorkbook.Close() 'Closes Workbook
            xlApp.Quit() 'Quits Excel
            xlSheet = Nothing 'Empties Excel Variables
            xlWorkbook = Nothing 'Empties Excel Variables
            xlApp = Nothing 'Empties Excel Variables
            System.GC.Collect() 'Ends Excel Processes
            Process.Start("EXCEL.EXE", """G:\ERX Trending.xlsm""") 'Starts UI for Excel Wkbk
    
        End Sub
        Private Function ASCIIsumOfString(ArrayString() As String)
            Dim sum As Int32
            Dim tempString As String
            For i = 0 To (ArrayString.Length - 1) <---Line 172
                tempString = ArrayString(i)
                For n = 1 To tempString.Length
                    sum = sum + Convert.ToByte(tempString(n - 1))
                Next
            Next
            Return sum
        End Function

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,914

    Re: Getting errors when running my Visual Basic application

    It looks like two lines are bolded. This one from the first snippet:

    Array.Resize(ExcelList, Counter) <--- Line 147

    and this one from the second:

    For i = 0 To (ArrayString.Length - 1) <---Line 172

    In the first case, the mistake was using an array in the first place. You should be using a List(of T), instead. Arrays don't change size. The List has .Add, Insert, and other methods for adding items, so the step you are taking of resizing the array downwards at the end isn't necessary if you use a list. If you want it to be an array afterwards, just use the .ToArray method of the List.

    It also looks like you'd want a List(of Object), but if the values will always be something else, like a numeric type, then a list of that type would be better.

    For the second bolded line, there's something wrong with the argument passed to the method. If it's a NullReferenceException, then what is wrong is that Nothing was passed to the sub, which would mean that ERX_Arrays.objArray is Nothing, unless the method is called from somewhere else.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2018
    Posts
    3

    Re: Getting errors when running my Visual Basic application

    Thank you so much for a quick response.

    I think the reason an array was used is because in the other part of the code which is in the module not the form. When ever the program is opened, what it does is connects to a server on which there is a software installed that is used to communicate with difference devices so it has its own database and all. But what this code does is looks at specific path for the software to find all of the current devices added on there.
    it goes down the list and takes the names from that program on that server and copies them into a combo box on this form. So whenever this program is opened up the combo box is populated with all of the device names.

    The code that I have pasted here initially is the code when a button is pressed on the Form when the program is opened.

    with this information, do you think using list would still be a better option?

    Regarding the second case, the NullReferenceException. I figured as much from reading other articles that there is a null or a nothing that was passed from somewhere to somewhere. But I can't wrap my head around what was passed, from where, and to where.
    Sorry for sounding dumb, i'm just so very new to all this and I dont write code all the time.

    Thank you again so much for any help.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,914

    Re: Getting errors when running my Visual Basic application

    As a general rule, a list is the right choice if you don't know how many you will need, as it will efficiently expand as necessary. Having said that, the reason the List is efficient has to do with allocating memory. The solution used kind of skips that by allocating more than it needed, then trimming out what isn't necessary. That trimming step (the Resize) isn't great, but overall the approach does avoid the allocation issues.

    However, you can do the same thing with a List, though it is rarely used. There is a constructor that takes an integer as a capacity. If you created a List(of whateverTypeMakesSense)(300), you'd have the same advantage of the current solution, but you wouldn't need the Resize, nor would it be useful.

    As to the second part, don't feel too bad about that. It's going to take some digging for ANYONE to figure that one out. The first step would be to either put a breakpoint on the line that throws the exception, or just go look at the code when the debugger throws the exception. All you'd be doing is confirming that ArrayString is Nothing, though you barely need to confirm that, as it's the only possible cause for that exception on that line. The reason to confirm it is more just that it is good practice.

    Once you have confirmed it, then the fun begins. I only see that function being called in this line:

    Dim ERXByte = ASCIIsumOfString(ERX_Arrays.objArray)

    You can look at the stack trace to be sure that's how you got there. Otherwise, you can right click on the function and Find All References (or something close to that). The point is that once you know how you got to the function, you have to figure out why the argument was Nothing. If you got there from that particular function call, then ERX_Arrays.objArray is Nothing. Why? Who knows, but I will say that the very name of the thing suggests that it's going to get ugly.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width