Solved

A Nullreferenceexception was unhandled

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

21 Experts available now in Live!

Get 1:1 Help Now