Solved

A Nullreferenceexception was unhandled

Posted on 2006-06-30
2
486 Views
Last Modified: 2006-11-18
Here is what I am trying to do.  I am trying to pull down the names of the tables located in an Excel spreadsheet from my VB.NET application.  After I load the Excel spreadsheet, I want my program to be able to located the table names, or worksheets in an Excel File.  I was able to see the table names in the schema viewer, but I am trying to figure out how to get those names into a string so that I can move those names into a drop down box.  I am getting a NullReferenceException was unhandled error at the line wher it says 'Excelsheets(i) = tblrow("Table_Name").ToString'.  Can somebody help me out please?  Below is my code.

    Private Sub btnGetSchema_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetSchema.Click
        Dim Path As String
        Dim conn_string As OleDbConnection
        Dim worksheets As New System.Data.DataTable
        Dim count_tables As Integer

        If TextBox1.Text = "" Then
            MsgBox("No spreadsheets have been loaded yet.")
            Exit Sub
        Else

            Path = TextBox1.Text
            conn_string = New OleDbConnection( _
                        "Provider= Microsoft.Jet.OLEDB.4.0;" & _
                        "Data Source =" & Path & ";" & _
                        "Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";")
            Using (conn_string)
                conn_string.Open()
                worksheets = conn_string.GetSchema("Tables")

                count_tables = worksheets.Rows.Count

                MsgBox(count_tables)

                Dim Excelsheets() As String

                Dim i As Integer = 0
                Dim tblrow As DataRow
                While (i < count_tables)
                    Excelsheets(i) = tblrow("Table_Name").ToString
                    i = i + 1

                    MsgBox(Excelsheets(i))
                End While

            End Using

        End if


    End Sub
0
Comment
Question by:VBBRett
2 Comments
 
LVL 4

Expert Comment

by:g_johnson
ID: 17019308
the nullreferenceexception error can be tough to trace.  It is typically an unitialized variable and not always in the routine that reports the error.

Put a try .. catch around this and trap ex.Message and see if you get more information.

I don't see anything wrong ...
0
 
LVL 34

Accepted Solution

by:
Sancler earned 500 total points
ID: 17019466
The specific error is because (i) your array has not been dimensioned and (2) tblrow has never been given any value.  At the moment it could be either that is throwing the error, but once you sort one out it will throw the same error again in relation to the other.

If you want to use an array, rather than an ArrayList, you will need to give it dimensions before you can put anything in it.  Replace this

                Dim Excelsheets() As String

with

                Dim Excelsheets(count_tables) As String

or

                Dim Excelsheets(count_tables - 1) As String

Strictly, it should be the latter, as you (intend to) start reading the rows and putting them in the array with the index = 0, so the last of the rows will go in Rows.Count - 1.  

But I don't think the code will, anyway, do what it looks like you want it to do. As I said above, although you have declared

                Dim tblrow As DataRow

tblrow is never given any value, and certainly not one that changes as the while loop iterates.  I imagine you want something like

                While (i < count_tables)
                    tblrow = worksheets.Rows(i)
                    Excelsheets(i) = tblrow("Table_Name").ToString
                    '[...]

Roger
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vb.net winforms sizing/resolution? 4 56
Error message when using PrintSocket.Send 7 27
Add Combobox column to datagridview using vb.net 2 26
Need help with another query 10 37
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question