Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Null Reference Exception for Array Problem... [modified]

Null Reference Exception for Array Problem... [modified]

Scheduled Pinned Locked Moved Visual Basic
helpdatabasedata-structuresquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rashadaliarshad
    wrote on last edited by
    #1

    Problem: When i fetch a list of column values from database and store them into a String One dimentional Array, it gives me exception error "Null Reference", kindly can anyone help me out, whats wrong with my code? // ========================================================================= // This is Where Problem comes - Complete code in given below // ========================================================================= myProfileNames.SetValue(pfDataSet.Tables("TimerDetails").Rows(j).Item("ProfileName").ToString, j) MsgBox(myProfileNames.GetValue(j)) // ========================================================================= Code: Public Class DataAccess Private m_strAppPath As String Private m_connectionString As String Public m_connectionObject As New OleDbConnection Private dataAdapter As OleDbDataAdapter Private pfDataSet As DataSet // One Dimentional Array to hold String Values fetched from Database... Public myProfileNames() As String // Public Subroutine that will be called through an instance of this // class. Public Sub LoadProfiles() Dim ProfileCount As Integer m_strAppPath = Directory.GetCurrentDirectory m_connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & m_strAppPath & "\TimerData.mdb" Try If Not (m_connectionObject.State = ConnectionState.Open) Then m_connectionObject.ConnectionString = m_connectionString m_connectionObject.Open() m_ConnectionStatus = True End If dataAdapter = New OleDbDataAdapter("Select * from ProfileMaster", m_connectionObject) pfDataSet = New DataSet("TimerDetails") dataAdapter.Fill(pfDataSet, "TimerDetails") // GET the No. of Rows that exists in Dataset ProfileCount = pfDataSet.Tables("TimerDetails").Rows.Count If Not (ProfileCount = 0) Then Dim j As Integer For j = 0 To (ProfileCount - 1) // ========================================================================= // This is Where Problem comes Stating that Null Refrence Exception occured // ========================================================================= myProfileNames.SetValue(pfDataSet.Tables("TimerDetails").Rows(j).Item("ProfileName").ToString, j) MsgBox(myProfileNames.GetValue(j)) // ==================================================

    G R 2 Replies Last reply
    0
    • R rashadaliarshad

      Problem: When i fetch a list of column values from database and store them into a String One dimentional Array, it gives me exception error "Null Reference", kindly can anyone help me out, whats wrong with my code? // ========================================================================= // This is Where Problem comes - Complete code in given below // ========================================================================= myProfileNames.SetValue(pfDataSet.Tables("TimerDetails").Rows(j).Item("ProfileName").ToString, j) MsgBox(myProfileNames.GetValue(j)) // ========================================================================= Code: Public Class DataAccess Private m_strAppPath As String Private m_connectionString As String Public m_connectionObject As New OleDbConnection Private dataAdapter As OleDbDataAdapter Private pfDataSet As DataSet // One Dimentional Array to hold String Values fetched from Database... Public myProfileNames() As String // Public Subroutine that will be called through an instance of this // class. Public Sub LoadProfiles() Dim ProfileCount As Integer m_strAppPath = Directory.GetCurrentDirectory m_connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & m_strAppPath & "\TimerData.mdb" Try If Not (m_connectionObject.State = ConnectionState.Open) Then m_connectionObject.ConnectionString = m_connectionString m_connectionObject.Open() m_ConnectionStatus = True End If dataAdapter = New OleDbDataAdapter("Select * from ProfileMaster", m_connectionObject) pfDataSet = New DataSet("TimerDetails") dataAdapter.Fill(pfDataSet, "TimerDetails") // GET the No. of Rows that exists in Dataset ProfileCount = pfDataSet.Tables("TimerDetails").Rows.Count If Not (ProfileCount = 0) Then Dim j As Integer For j = 0 To (ProfileCount - 1) // ========================================================================= // This is Where Problem comes Stating that Null Refrence Exception occured // ========================================================================= myProfileNames.SetValue(pfDataSet.Tables("TimerDetails").Rows(j).Item("ProfileName").ToString, j) MsgBox(myProfileNames.GetValue(j)) // ==================================================

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      It seems to be that the result doesn't have a field named "ProfileName". Do you have such a field in the table? Specify the fields that you want returned in the query, instead of using *. That makes debugging easier, plus it makes the query only fetch the data that you need.

      --- single minded; short sighted; long gone;

      1 Reply Last reply
      0
      • R rashadaliarshad

        Problem: When i fetch a list of column values from database and store them into a String One dimentional Array, it gives me exception error "Null Reference", kindly can anyone help me out, whats wrong with my code? // ========================================================================= // This is Where Problem comes - Complete code in given below // ========================================================================= myProfileNames.SetValue(pfDataSet.Tables("TimerDetails").Rows(j).Item("ProfileName").ToString, j) MsgBox(myProfileNames.GetValue(j)) // ========================================================================= Code: Public Class DataAccess Private m_strAppPath As String Private m_connectionString As String Public m_connectionObject As New OleDbConnection Private dataAdapter As OleDbDataAdapter Private pfDataSet As DataSet // One Dimentional Array to hold String Values fetched from Database... Public myProfileNames() As String // Public Subroutine that will be called through an instance of this // class. Public Sub LoadProfiles() Dim ProfileCount As Integer m_strAppPath = Directory.GetCurrentDirectory m_connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & m_strAppPath & "\TimerData.mdb" Try If Not (m_connectionObject.State = ConnectionState.Open) Then m_connectionObject.ConnectionString = m_connectionString m_connectionObject.Open() m_ConnectionStatus = True End If dataAdapter = New OleDbDataAdapter("Select * from ProfileMaster", m_connectionObject) pfDataSet = New DataSet("TimerDetails") dataAdapter.Fill(pfDataSet, "TimerDetails") // GET the No. of Rows that exists in Dataset ProfileCount = pfDataSet.Tables("TimerDetails").Rows.Count If Not (ProfileCount = 0) Then Dim j As Integer For j = 0 To (ProfileCount - 1) // ========================================================================= // This is Where Problem comes Stating that Null Refrence Exception occured // ========================================================================= myProfileNames.SetValue(pfDataSet.Tables("TimerDetails").Rows(j).Item("ProfileName").ToString, j) MsgBox(myProfileNames.GetValue(j)) // ==================================================

        R Offline
        R Offline
        rashadaliarshad
        wrote on last edited by
        #3

        Friends, i got ma problem fixed:) // One Dimentional Array to hold String Values fetched from Database... Public myProfileNames() As String <-- Wrong Initialization public myprofileNames(10) as String <-- Problem Solved:-D But i still want to know, what if i dont know, how mnay elements my array gona contain, how to dynamically initialize array elements. anyone there for help?

        :- Rashid Ali -:

        C 1 Reply Last reply
        0
        • R rashadaliarshad

          Friends, i got ma problem fixed:) // One Dimentional Array to hold String Values fetched from Database... Public myProfileNames() As String <-- Wrong Initialization public myprofileNames(10) as String <-- Problem Solved:-D But i still want to know, what if i dont know, how mnay elements my array gona contain, how to dynamically initialize array elements. anyone there for help?

          :- Rashid Ali -:

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          That's what classes like List ( or ArrayList in .NET 1.0 and 1.1 ) are for.

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups