9 Replies - 3651 Views - Last Post: 11 June 2011 - 08:01 PM Rate Topic: ***** 1 Votes

#1 VIPER5646  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 01-March 09

Object reference not set to an instance of an object.

Posted 10 June 2011 - 09:40 PM

I cant figure out why I'm getting this error.
ERROR: Object reference not set to an instance of an object.

I receive this error at ModelBuilder_Relation.Add(m_dr.GetValue(0))
When I tried ModelBuilder_Relation.Add(m_dr.Getstring(0)) I get Invalid attempt to call Read when reader is closed ERRo

   Public Sub ModelsBuilder(ByVal BModel As String)

        query = "Select BldrID From " & Tabela & " where Model= @Model" & " Order by model asc"

        Dim txtSource As String
        Dim ObjFile As New System.IO.StreamReader("C:\CWDEsign\CWDEsign.txt")
        txtSource = ObjFile.ReadToEnd()
        ObjFile.Close()
        ObjFile.Dispose()

        Dim sourse As String = "Data Source=" & txtSource & ";"
        Dim providr As String = "Provider=Microsoft.ACE.OLEDB.12.0;"
        Dim MYcon = providr & sourse
        Dim con = New OleDb.OleDbConnection(MYcon)
        Dim cmd As New OleDb.OleDbCommand(query, con)
        With cmd.Parameters
            .AddWithValue("@Model", BModel)
        End With
        cmd.CommandType = CommandType.Text
        cmd.CommandText = query
        con.Open()
        Dim m_dr As OleDb.OleDbDataReader

        m_dr = cmd.ExecuteReader
        Dim x As Integer
        While m_dr.Read
            ModelBuilder_Relation.Add(m_dr.GetValue(0))
            x = x + 1
        End While
        m_dt.Load(m_dr)


        Dataviews = New DataView(m_dt)
        con.Close()
        m_dt.Dispose()
    End Sub

   


Is This A Good Question/Topic? 0
  • +

Replies To: Object reference not set to an instance of an object.

#2 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Object reference not set to an instance of an object.

Posted 10 June 2011 - 11:45 PM

As the error says one of the objects you use(ModelBuilder_Relation and m_dr) may have not been instantiated. if m_dr was nothing, the application would have crashed in
while m_dr.Read
. My guess is that you forgot to instantiate ModelBuilder_Relation.

This post has been edited by Ionut: 10 June 2011 - 11:46 PM

Was This Post Helpful? 1
  • +
  • -

#3 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 549
  • Joined: 08-May 08

Re: Object reference not set to an instance of an object.

Posted 11 June 2011 - 05:30 AM

Can we clean up some stuff first...

15      Dim cmd As New OleDb.OleDbCommand(query, con)  
16      With cmd.Parameters  
17          .AddWithValue("@Model", BModel)  
18      End With 
19      cmd.CommandType = CommandType.Text  
20      cmd.CommandText = query  
21      con.Open()  
22      Dim m_dr As New OleDb.OleDbDataReader23   
24      m_dr = cmd.ExecuteReader  
25      Dim x As Integer 
26      While m_dr.Read  



Try that.....

I have a great example for what you are doing, but i will need to dig through some source code....the above code is from memory and probably not exactly what needs to be done but all-in-all it should get the point across.

Oh and th eproblem is that the DataReader has not been initialized, although you assigned a value. Its a quirk with the DataReaders that have spun me for loops in the past.

My bad, just did a MSDN search....you may want to change the base DataReader class.
Was This Post Helpful? 0
  • +
  • -

#4 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 549
  • Joined: 08-May 08

Re: Object reference not set to an instance of an object.

Posted 11 June 2011 - 05:49 AM

Here a code-snippet implementation of what i have done in the past.

Using cmd As SqlCommand = tsConn.CreateCommand
					'Clear any previous erros that may reside in the table
					cmd.CommandType = CommandType.StoredProcedure
					cmd.CommandText = "spClearUploadErrors"
					cmd.ExecuteNonQuery()

					'Clear any previously staged Upload files
					'Get the current Validation List
					cmd.CommandText = "spGetTimesheetsForValidation"
					cmd.Parameters.Add(New SqlParameter("@PeriodEndDate", SqlDbType.SmallDateTime)).Value = CType(cboWeekEnding.SelectedValue, DateTime)

					TimesheetErrors.Clear()

					Using row As New SafeDataReader(cmd.ExecuteReader)
						While row.Read



The SafeDataReader is a CSLA library object that extends the IDataReader interface class. It will allow any reader to be associated and will read any reader that implements IDataReader.

Another question is why are you using the contents of the file read as the data source? Isn't the data source of the ACE driver supposed to be the file itself to be read from?
Was This Post Helpful? 0
  • +
  • -

#5 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 221
  • View blog
  • Posts: 1,478
  • Joined: 16-March 11

Re: Object reference not set to an instance of an object.

Posted 11 June 2011 - 06:54 AM

What's this? :)

 Dim con = New OleDb.OleDbConnection(MYcon)


Was This Post Helpful? 0
  • +
  • -

#6 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 549
  • Joined: 08-May 08

Re: Object reference not set to an instance of an object.

Posted 11 June 2011 - 09:03 AM

trevster,
That is the creation of the Connection object to be used. MYcon is the connection string to use to make the data connection.
Was This Post Helpful? 0
  • +
  • -

#7 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 221
  • View blog
  • Posts: 1,478
  • Joined: 16-March 11

Re: Object reference not set to an instance of an object.

Posted 11 June 2011 - 10:46 AM

You can declare it as nothing and set it's value?
Was This Post Helpful? 0
  • +
  • -

#8 VIPER5646  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 01-March 09

Re: Object reference not set to an instance of an object.

Posted 11 June 2011 - 04:04 PM

Quote

Thanks all for your replies
yes I iniciated Modelbuilder_Relation it is a Public ModelBuilder_Relation As ArrayList
In the clean up all I see is that you removed
the empty lines and added 23 at the end of the
oledb.datareader
which I cant figure out why.

Was This Post Helpful? 0
  • +
  • -

#9 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 549
  • Joined: 08-May 08

Re: Object reference not set to an instance of an object.

Posted 11 June 2011 - 06:52 PM

Viper,
Refer to post #4.....did a little more research and OleDbDataReader has no constructor, which basically treats it as a Structure instead of a Class.

I haven't had much experience using non-instantiated classes for DataReaders. Using the CSLA library has been a god-send for making data-driven application an ease of use.

Outside of that:
22      Dim m_dr As OleDb.OleDbDataReader = cmd.ExecuteReader
23   
24      Dim x As Integer 
25      While m_dr.Read  
26          ModelBuilder_Relation.Add(m_dr.GetValue(0))  
27          x = x + 1  
28      End While 



Outside of this simplification, you need to verify your connection string cause that seems to be a potential source of your problems. If the connection string returns no data based on your query, then that could be the problem.

Lastly, what .net version are your programming against?

Also, try doing console.writeline(m_dr.GetValue(0)) to see what gets returned during the Read loop. I would start interrupt debugging with Break Points verifying the data objects values, making sure none of them evaluate to 'Nothing'.
Was This Post Helpful? 0
  • +
  • -

#10 VIPER5646  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 01-March 09

Re: Object reference not set to an instance of an object.

Posted 11 June 2011 - 08:01 PM

:bananaman:

Quote

Thanks all
As IONUT mention earlier I had forgot to instantiate ModelBuilder_Relation.
At least I asumed I had it wright instead of Dim ModelBuilder_Relation as new Arraylist I had Dim ModelBuilder_Relation as Arraylist. Now it seams to be working

This post has been edited by VIPER5646: 11 June 2011 - 08:03 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1