11 Replies - 1811 Views - Last Post: 09 February 2011 - 09:11 PM Rate Topic: -----

#1 ganeshtvpm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-February 11

why is System.NullReferenceException thrown at the end of for next loo

Posted 09 February 2011 - 04:27 AM

This is my first ever post in this wonderful forum. I am new to vb.net programming. Given below is a piece of code which throws a system null reference exception at the end of the loop. I am unable to figure out the problem. "drledger" is null at the end of the loop. Even though i am able to catch the exception what can be done to avoid the null reference exception. Any help will be much appreciated.

Private Sub btnSalesExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSalesExport.Click
Dim iRowMax As Integer
If Not txtSelSales.Text = "" Then
            Try
                If xlWorkSheet.Name = "Sale" Then
                    iRowMax = xlWorkSheet.UsedRange.Rows.Count
                    
                    For iRowMax = 2 To iRowMax

                        dtp1 = xlWorkSheet.Cells(iRowMax, 1).Value
                        invNoSale = xlWorkSheet.Cells(iRowMax, 2).Value
                        drLedger = xlWorkSheet.Cells(iRowMax, 3).Value.ToString
                        crLedger = xlWorkSheet.Cells(iRowMax, 4).Value.ToString
                        crAmount = xlWorkSheet.Cells(iRowMax, 5).Value
                        drAmount = crAmount * -1
                        
                    Next iRowMax

                Else
                    MessageBox.Show("sheet does not exist")

                End If

            Catch ex1 As System.NullReferenceException
                MessageBox.Show(ex1.ToString)

            Catch ex As Exception
                MessageBox.Show(ex.ToString)
                txtSelSales.Focus()
            Finally
                If Not xlWorkBook Is Nothing Then
                    xlWorkBook.Close()
                    xlApp.Quit()
                    releaseObject(xlApp)
                    releaseObject(xlWorkBook)
                    releaseObject(xlWorkSheet)
                End If
            End Try
        
Else
           
MessageBox.Show("File name cannot be blank")
       
End If
End Sub


Edited by Dogstopper: :code:

Is This A Good Question/Topic? 0
  • +

Replies To: why is System.NullReferenceException thrown at the end of for next loo

#2 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1066
  • View blog
  • Posts: 4,173
  • Joined: 02-July 08

Re: why is System.NullReferenceException thrown at the end of for next loo

Posted 09 February 2011 - 04:55 AM

You don't use iRowMax as the maximum of a loop and also ask it to be the iterator integer. The variable i is always changing based on the looping rules the iRowMax just tell the loop when to stop iterating.

Private Sub btnSalesExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSalesExport.Click
Dim iRowMax As Integer
If Not txtSelSales.Text = "" Then
            Try
                If xlWorkSheet.Name = "Sale" Then
                    iRowMax = xlWorkSheet.UsedRange.Rows.Count
                    
                    For i As Integer = 2 To iRowMax

                        dtp1 = xlWorkSheet.Cells(i, 1).Value
                        invNoSale = xlWorkSheet.Cells(i, 2).Value
                        drLedger = xlWorkSheet.Cells(i, 3).Value.ToString
                        crLedger = xlWorkSheet.Cells(i, 4).Value.ToString
                        crAmount = xlWorkSheet.Cells(i, 5).Value
                        drAmount = crAmount * -1
                        
                    Next iRowMax

                Else
                    MessageBox.Show("sheet does not exist")

                End If

            Catch ex1 As System.NullReferenceException
                MessageBox.Show(ex1.ToString)

            Catch ex As Exception
                MessageBox.Show(ex.ToString)
                txtSelSales.Focus()
            Finally
                If Not xlWorkBook Is Nothing Then
                    xlWorkBook.Close()
                    xlApp.Quit()
                    releaseObject(xlApp)
                    releaseObject(xlWorkBook)
                    releaseObject(xlWorkSheet)
                End If
            End Try
        
Else
           
MessageBox.Show("File name cannot be blank")
       
End If
End Sub

Was This Post Helpful? 0
  • +
  • -

#3 ganeshtvpm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-February 11

Re: why is System.NullReferenceException thrown at the end of for next loo

Posted 09 February 2011 - 11:15 AM

@hawkvalley1
thanks for correcting my mistake.I have tried your new code but the problem of null reference is thrown at the end of the loop. Do i have to make any other changes to the code.
thanks and regards
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Jim*


Reputation:

Re: why is System.NullReferenceException thrown at the end of for next loo

Posted 09 February 2011 - 11:35 AM

You've got a few things that look kinda weird.

Why would you assign these values in a loop in the first place? Whatever is assigned the first time through the loop is going to be overwritten the second time through the loop, etc. What is dtp1? Is it a variable or a datetimepicker control or something else? If it's a control you're going to need to assign the value to a property of the control. . . .

I would guess that a line like this:

drLedger = xlWorkSheet.Cells(i, 3).Value.ToString



might give the error that you've described as any empty cell isn't going to return a string value. But, I think that is really the least of your problems w/ this piece of code.

Do you know how to set breakpoints and step through your code? Click on the first line of your For loop. Go to the "Debug" menu and select "Toggle Breakpoint". That line of text should now appear with a red background. Run your code. When execution gets to that line it will turn yellow. Go back to the "Debug" menu and select "Step Into" to execute each line of code. When you get to the error execution will transfer to one of your Catch blocks.

I don't think the compiler cares what comes after the next. That is there primarily for documentation purposes, but I doubt that it matters what value it is.

Jim
Was This Post Helpful? 0

#5 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1066
  • View blog
  • Posts: 4,173
  • Joined: 02-July 08

Re: why is System.NullReferenceException thrown at the end of for next loo

Posted 09 February 2011 - 11:56 AM

Good catch Jim, that does not make sense too me either.
Was This Post Helpful? 0
  • +
  • -

#6 ganeshtvpm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-February 11

Re: why is System.NullReferenceException thrown at the end of for next loo

Posted 09 February 2011 - 11:59 AM

@guest_jim
dtp1 is just a variable.
Again coming to the purpose of the loop. I am concatenating these variables to form a string which in turn is used to form an xml file. How can i check whether an empty cell is encountered before the assignment.
thanks
Was This Post Helpful? 0
  • +
  • -

#7 Guest_Jim*


Reputation:

Re: why is System.NullReferenceException thrown at the end of for next loo

Posted 09 February 2011 - 12:48 PM

It's good practice to declare the variables that you're using so that the data you're assigning is stored properly and lasts for the scope that is required by the application.

You may *want* to concatenate those values, but that is not what you've coded there. Have you tried to step through your code? You can check the value that your variables have while you are stepping through your code by selecting "QuickWatch..." off the "Debug" menu. I suggest that a little debugging will help you figure out what is going on here very quickly.

If you'd like to check for nothing you might do something like

        Dim drLedger As String
        Dim vObj As Object = xlWrkSheet.Cells(1, 2).value
        If vObj Is Nothing Then
        Else
            drLedger = vObj.ToString
        End If



Jim
Was This Post Helpful? 0

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: why is System.NullReferenceException thrown at the end of for next loo

Posted 09 February 2011 - 02:23 PM

jim, just quick note (more of an observation) on your code.
        Dim drLedger As String
        Dim vObj As Object = xlWrkSheet.Cells(1, 2).value
        If vObj Is Nothing Then
' If this not use.
        Else
            drLedger = vObj.ToString
        End If


The either use the negated result of the boolean answer.
If Not(vObj Is Nothing) Then

or the nagated form of the IS operator.
        If vObj IsNot Nothing Then
               drLedger = vObj.ToString
        End If


and then remove the empty section.
Was This Post Helpful? 0
  • +
  • -

#9 Guest_Jim*


Reputation:

Re: why is System.NullReferenceException thrown at the end of for next loo

Posted 09 February 2011 - 02:36 PM

You don't like the "empty section" do you? ;-)

Jim
Was This Post Helpful? 0

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: why is System.NullReferenceException thrown at the end of for next loo

Posted 09 February 2011 - 02:48 PM

jim, I take the view if its not being utilised why keep it, can it be re-factored out.
Was This Post Helpful? 0
  • +
  • -

#11 Guest_Jim*


Reputation:

Re: why is System.NullReferenceException thrown at the end of for next loo

Posted 09 February 2011 - 02:54 PM

It's a style thing. I happen to favor it. I suppose it's not for everyone. . . .

Jim
Was This Post Helpful? 0

#12 ganeshtvpm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-February 11

Re: why is System.NullReferenceException thrown at the end of for next loo

Posted 09 February 2011 - 09:11 PM

@Jim
Problem solved. Your code below solved my problem.

Dim drLedger As String
Dim vObj As Object = xlWrkSheet.Cells(1, 2).value
If vObj Is Nothing Then
If this not use.
Else
drLedger = vObj.ToString
End If

@Adam
I would like to use your suggestion by removing the "empty section".

off topic.
I have for some reasons removed the code in my first post which concatenates and performs the operation.
Anyway thanks for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1