Solved

A Nullreferenceexception was unhandled

Posted on 2006-06-30
2
483 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best way to handle data being sent over a serial COM 5 32
Visual Studio 2013 Shortcut (VB) 4 34
Receiving a string from a WebService Push 21 36
Help with LINQ and XML 10 26
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

912 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now