?
Solved

A Nullreferenceexception was unhandled

Posted on 2006-06-30
2
Medium Priority
?
489 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

741 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