Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strange stuff happening with object instantiation...

Status
Not open for further replies.

ShikkurDude

Programmer
Mar 15, 2005
55
US
How could it be that I instantiated an object like this:
Code:
Public reqToEdit As New Requisition
and it worked just fine for some time.

Then, without any apparent reason or cause for things to change, when I referenced the object elsewhere in the code, I all of a sudden started getting a null exception.

When I changed the statement to this:
Code:
Public reqToEdit As Requisition = New Requisition
it started working again.

How could that be and why, please?

Thanks!
E
 
is Requisition an object that you've created? if so, show the class...
 
Yes, that would help, I'd imagine...

Thanks,
E

Code:
Imports System.Data.SqlClient

Public Class Requisition
	Dim reqID As Integer
	Dim divisionName_bill As String
	Dim divisionID_bill As Integer
	Dim department_bill As String
	Dim costCenterID_bill As Integer
	Dim costCenter_bill As String

	Dim divisionName_install As String
	Dim divisionID_install As Integer
	Dim departmentName_install As String
	Dim departmentID_install As Integer
	Dim orgID_install As Integer
	Dim facilityName_install As String
	Dim facilityID_install As Integer
	Dim contactName_install As String
	Dim contactPhone_install As String
	Dim contactEmail_install As String

	Dim contactFirstName As String
	Dim contactLastName As String
	Dim requestedBy As String

	Dim costAmt As String
	Dim busNeed As String

	Dim pcScope As String
	Dim pcName As String

	Dim ticketNum As String
	Dim instructions As String

	Dim isEditable As Boolean

	Public Sub buildReq(ByVal intReqID As Integer)

		Me.set_reqID(intReqID)

		Dim cnReq As New SqlConnection
		cnReq.ConnectionString = cnPurchIS

		Dim cmd As New SqlCommand
		cmd.Connection = cnReq
		cmd.CommandType = CommandType.StoredProcedure
		cmd.CommandText = "dbo.requistions_summary"

		cmd.Parameters.Add("@RequistionId", intReqID)

		Dim dr As SqlDataReader
		Try
			If Not cnReq.State = ConnectionState.Open Then
				cnReq.Open()
			End If

			dr = cmd.ExecuteReader
			While dr.Read
				divisionName_bill = dr.Item("divisionName_charge").ToString
				divisionID_bill = dr.Item("divisionID_charge").ToString
				department_bill = dr.Item("organizationName_charge").ToString
				costCenterID_bill = dr.Item("organizationID_charge").ToString
				costCenter_bill = dr.Item("costCenter").ToString

				divisionName_install = dr.Item("divisionName_install").ToString
				divisionID_install = dr.Item("divisionID_install").ToString
				departmentID_install = dr.Item("organizationID_install").ToString
				departmentName_install = dr.Item("organizationName_install").ToString
				orgID_install = dr.Item("organizationID_install").ToString
				facilityName_install = dr.Item("facilityName").ToString
				facilityID_install = dr.Item("facilityID").ToString
				contactName_install = dr.Item("contactFirstName").ToString & " " & dr.Item("contactLastName").ToString
				contactFirstName = dr.Item("contactFirstName").ToString
				contactLastName = dr.Item("contactLastName").ToString
				contactPhone_install = dr.Item("phone_contact").ToString
				contactEmail_install = dr.Item("email_contact").ToString
				requestedBy = dr.Item("personFirstName_creator").ToString & " " & dr.Item("personLastName_creator").ToString

				pcScope = dr.Item("PCScope").ToString
				pcName = dr.Item("PCName").ToString
				ticketNum = dr.Item("HelpDeskTicket").ToString
				instructions = dr.Item("installationInstructions").ToString

				costAmt = dr.Item("totalAmount").ToString
				busNeed = dr.Item("BusinessNeedDescr").ToString
			End While
			dr.Close()
			cnReq.Close()

		Catch ex As Exception
			catchCodeErrorMsg(cnReq)
		End Try
	End Sub
	Public Function get_instructions() As String
		Return instructions
	End Function
	Public Function get_ticketNum() As String
		Return ticketNum
	End Function
	Public Sub set_contactFirstName(ByVal fName As String)
		contactFirstName = fName
	End Sub
	Public Function get_contactFirstName() As String
		Return contactFirstName
	End Function
	Public Sub set_contactLastName(ByVal lName As String)
		contactLastName = lName
	End Sub
	Public Function get_contactLastName() As String
		Return contactLastName
	End Function
	Public Sub set_pcName(ByVal name As String)
		pcName = name
	End Sub
	Public Function get_pcName() As String
		Return pcName
	End Function
	Public Sub set_reqID(ByVal intReqID As Integer)
		reqID = intReqID
	End Sub
	Public Function get_reqID() As String
		Return reqID
	End Function
	Public Sub set_isEditable(ByVal editable As Boolean)
		isEditable = editable
	End Sub
	Public Function get_isEditable() As Boolean
		Return isEditable
	End Function
	Public Function get_pcScope() As String
		Return pcScope
	End Function
	Public Sub set_pcScope(ByVal new_pcScope As String)
		pcScope = new_pcScope
	End Sub
	Public Function get_divisionName_bill_STRING() As String
		Return divisionName_bill
	End Function
	Public Function get_divisionID_bill_INT() As Integer
		Return divisionID_bill
	End Function
	Public Function get_department_bill_STRING() As String
		Return department_bill
	End Function
	Public Function get_costCenterID_bill_INT() As Integer
		Return costCenterID_bill
	End Function
	Public Function get_costCenter_bill_STRING() As String
		Return costCenter_bill
	End Function
	Public Function get_divisionName_install_STRING() As String
		Return divisionName_install
	End Function
	Public Function get_divisionID_install_INT() As Integer
		Return divisionID_install
	End Function
	Public Function get_departmentName_install_STRING() As String
		Return departmentName_install
	End Function
	Public Function get_departmentID_install_INT() As Integer
		Return departmentID_install
	End Function
	Public Function get_facilityName_install_STRING() As String
		Return facilityName_install
	End Function
	Public Function get_facilityID_install_STRING() As String
		Return facilityID_install
	End Function
	Public Function get_orgID_install_INT() As Integer
		Return orgID_install
	End Function
	Public Function get_contactName_install_STRING() As String
		Return contactName_install
	End Function
	Public Function get_contactPhone_install_STRING() As String
		Return contactPhone_install
	End Function
	Public Function get_contactEmail_install_STRING() As String
		Return contactEmail_install
	End Function
	Public Function get_requestedBy_STRING() As String
		Return requestedBy
	End Function
	Public Function get_costAmt_STRING() As String
		Return costAmt
	End Function
	Public Function get_busNeed_STRING() As String
		Return busNeed
	End Function
End Class
 
this class doesn't look like you should need to do a

Code:
as Req = new Req()

can you post the code area that causes this error?
 
In a VB module "CommonFunctions.vb" I have the following:
Code:
Public reqToEdit As Requisition = New Requisition
It should always (and has always) executed before any of my other code. But then stopped suddenly, like I mentioned...

The error to which I referred was that at one point when one of the classes tried to reference "reqToEdit" (my Requisition object), it crashed saying that the object is Nothing.

My question is - How could it be Nothing when it was supposed to be created by the VB module - but, in actuality, it never was created?

Thanks so much,
E
 
Your class doesn't have a constructor though, does it?
 
No. I was using buildReq() as a quasi-constructor... First I'd create it and then I'd call buildReq()

E
 
So it works now with the new format? Have you tried changing it back to the way it was? Does it still give you null reference exceptions? If not, if you watch the line in the debugger, reqToEdit is still Nothing even after the new?

I've seen a similar problem come up occaisionally when I rename some server controls in .aspx to objects of a different type and rename the generated declaration in .vb. When run, .NET tells me there's no object there. If I rename the object in both places to something else, it runs fine. Once it runs fine once, I can change them back and everything's peachy.

________________________________________
Andrew

I work for a gift card company!
 
It usually works now - about 98% of the time. Occassionally I do get a null/nothing reference still...

Very perturbing!
E
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top