|
Author |
Message |
JWhite
|
Posted: Visual Basic General, Null Reference Exception |
Top |
I am having a problem with a multi-form application I am writing in VB .Net 2003. When I transition back from one form to my startup form, I get an intermittent Null Reference Exception. Sometimes it will work, moving back and forth from form to form with no problems for 15-20 minutes, and sometimes it will crash everytime. I have tried putting a try-catch block around the instantiation of the main form. It does not catch anything. The exception points to line 2 of the startup form, which reads:
Public Class frmWelcome
Not much to cause a problem there, I would have thought. I am attaching the code for bringing up the welcome screen in hopes that someone will see something I am not thinking of.
Try
Dim welcome As New frmWelcome
welcome.Show()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OKOnly, "Error")
End Try
Me.Close()
The exact error that I am getting is:
An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll
Additional information: Object reference not set to an instance of an object.
I also tried putting a try-catch block around the load method of the welcome form, thinking it might be in there. Nothing. I am stumped. I would appreciate some help with this.
Thanks
Joe White
Visual Basic20
|
|
|
|
|
SvenC
|
Posted: Visual Basic General, Null Reference Exception |
Top |
Run the code from within VB.Net as debug build. When the error is hit during runtime you should be right at the point where the exception occurrs.
-- SvenC
|
|
|
|
|
ReneeC
|
Posted: Visual Basic General, Null Reference Exception |
Top |
|
|
|
ahmedilyas
|
Posted: Visual Basic General, Null Reference Exception |
Top |
I think we need more code than this, can you paste the entire method you are having this code placed within
|
|
|
|
|
stallion_alpa
|
Posted: Visual Basic General, Null Reference Exception |
Top |
Heres the code its pretty long, i've included all the relevant stuff let me know if you need more. I have 3 other classes that are identical to this 1 except for the data they work but this one refuses to. Also for some reason if i go for 0 to 16 instead it works but if i go anything bigger then that it doesn't.
Public Class main Dim ****(23) As weapons Dim ****Display(23) As weaponDisplay
Private Sub main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim x As Integer = 34 Dim y As Integer = 0 load****() Dim ****Primary As New weaponHeaderDisplay("**** - Primary", "Weapon", "Army", "Ammo", "Damage", "Firing Mode", "RPM", "Zoom", "Reload", "Heat", x, y) Me.weaponsPanel.Controls.Add(****Primary)
Dim count As Integer = 22 y = 65 For count = 0 To 22
****Display(count) = New weaponDisplay(****(count).weaponName, ****(count).ammo, ****(count).damage, ****(count).rpm, ****(count).reload, ****(count).heat, engineer(count).army, ****(count).weaponPic, ****(count).zoom, ****(count).firingModes, count, x, y) With ****Display(count) .Name = "****" & count.ToString .Visible = False .BorderStyle = BorderStyle.FixedSingle End With Me.weaponsPanel.Controls.Add(****Display(count)) y = y + 90 Next End Sub End Class
Public Class weaponDisplay Inherits Panel Dim weaponNameLabel As New Label Dim weaponAmmoLabel As New Label Dim weaponDamageLabel As New Label Dim weaponRPMLabel As New Label Dim weaponReloadLabel As New Label Dim weaponHeatLabel As New Label Dim weaponArmyBox As New Label Dim weaponBoxLabel As New PictureBox Dim weaponFiringModeLabel As New Label Dim weaponZoomLabel As New Label
Dim F As New Font("Arial", 10, FontStyle.Bold) Public Sub New(ByVal weaponName As String, ByVal ammo As String, ByVal damage As String, ByVal rpm As String, ByVal reload As String, ByVal heat As String, ByVal army As String, ByVal weaponPic As String, ByVal zoom As String, ByVal firingMode As String, ByVal count As Integer, ByVal x As Integer, ByVal y As Integer) Me.BackColor = Color.Yellow Me.SetBounds(x, y, 750, 85)
weaponBoxLabel.SetBounds(0, 25, 130, 60) weaponBoxLabel.BackColor = Color.White weaponBoxLabel.Font = F Me.Controls.Add(weaponBoxLabel)
weaponNameLabel.SetBounds(0, 0, 130, 20) weaponNameLabel.BackColor = Color.White weaponNameLabel.Font = F weaponNameLabel.TextAlign = ContentAlignment.MiddleCenter Me.Controls.Add(weaponNameLabel)
weaponArmyBox.SetBounds(135, 25, 60, 20) weaponArmyBox.BackColor = Color.White weaponArmyBox.Font = F weaponArmyBox.TextAlign = ContentAlignment.MiddleCenter Me.Controls.Add(weaponArmyBox)
weaponAmmoLabel.SetBounds(200, 25, 50, 20) weaponAmmoLabel.BackColor = Color.White weaponAmmoLabel.Font = F weaponAmmoLabel.TextAlign = ContentAlignment.MiddleCenter Me.Controls.Add(weaponAmmoLabel)
weaponDamageLabel.SetBounds(255, 25, 40, 20) weaponDamageLabel.BackColor = Color.White weaponDamageLabel.Font = F weaponDamageLabel.TextAlign = ContentAlignment.MiddleCenter Me.Controls.Add(weaponDamageLabel)
weaponRPMLabel.SetBounds(300, 25, 35, 20) weaponRPMLabel.BackColor = Color.White weaponRPMLabel.Font = F weaponRPMLabel.TextAlign = ContentAlignment.MiddleCenter Me.Controls.Add(weaponRPMLabel)
weaponFiringModeLabel.SetBounds(340, 25, 100, 20) weaponFiringModeLabel.BackColor = Color.White weaponFiringModeLabel.Font = F weaponFiringModeLabel.TextAlign = ContentAlignment.MiddleCenter Me.Controls.Add(weaponFiringModeLabel)
weaponReloadLabel.SetBounds(445, 25, 40, 20) weaponReloadLabel.BackColor = Color.White weaponReloadLabel.Font = F weaponReloadLabel.TextAlign = ContentAlignment.MiddleCenter Me.Controls.Add(weaponReloadLabel)
weaponZoomLabel.SetBounds(490, 25, 60, 20) weaponZoomLabel.BackColor = Color.White weaponZoomLabel.Font = F weaponZoomLabel.TextAlign = ContentAlignment.MiddleCenter Me.Controls.Add(weaponZoomLabel)
weaponHeatLabel.SetBounds(555, 25, 40, 20) weaponHeatLabel.BackColor = Color.White weaponHeatLabel.Font = F weaponHeatLabel.TextAlign = ContentAlignment.MiddleCenter Me.Controls.Add(weaponHeatLabel)
weaponBoxLabel.Image = Image.FromFile(weaponPic) weaponNameLabel.Text = weaponName weaponAmmoLabel.Text = ammo weaponDamageLabel.Text = damage weaponZoomLabel.Text = zoom weaponFiringModeLabel.Text = firingMode weaponArmyBox.Text = army weaponHeatLabel.Text = heat weaponReloadLabel.Text = reload weaponRPMLabel.Text = rpm End Sub End Class
Public Class weapons Private weaponNameValue As String Private firingModesValue As String Private zoomValue As String Private ammoValue As String Private damageValue As String Private rpmValue As String Private reloadValue As String Private heatValue As String Private armyValue As String Private weaponPicValue As String
Public Property weaponName() As String Get Return weaponNameValue End Get Set(ByVal value As String) weaponNameValue = value End Set End Property Public Property firingModes() As String Get Return firingModesValue End Get Set(ByVal value As String) firingModesValue = value End Set End Property Public Property zoom() As String Get Return zoomValue End Get Set(ByVal value As String) zoomValue = value End Set End Property Public Property ammo() As String Get Return ammoValue End Get Set(ByVal value As String) ammoValue = value End Set End Property Public Property damage() As String Get Return damageValue End Get Set(ByVal value As String) damageValue = value End Set End Property Public Property rpm() As String Get Return rpmValue End Get Set(ByVal value As String) rpmValue = value End Set End Property Public Property reload() As String Get Return reloadValue End Get Set(ByVal value As String) reloadValue = value End Set End Property
Public Property heat() As String Get Return heatValue End Get Set(ByVal value As String) heatValue = value End Set End Property
Public Property army() As String Get Return armyValue End Get Set(ByVal value As String) armyValue = value End Set End Property
Public Property weaponPic() As String Get Return weaponPicValue End Get Set(ByVal value As String) weaponPicValue = value End Set End Property
End Class
Private Sub load****() ****(0) = New weapons ****(0).weaponName = "SCAR 11 AR" ****(0).ammo = " x 6" ****(0).damage = "29" ****(0).firingModes = "Full / Semi" ****(0).reload = "3.9" ****(0).rpm = "-" ****(0).zoom = "51.6%" ****(0).heat = "-" ****(0).army = "EU" ****(0).weaponPic = "C:\2142 vb\img\scar.gif"
****(1) = New weapons ****(1).weaponName = "Voss L-AR" ****(1).ammo = "40 x" ****(1).damage = "25" ****(1).firingModes = "Full / Semi" ****(1).reload = "3.9" ****(1).rpm = "900" ****(1).zoom = "41%" ****(1).heat = "-" ****(1).army = "EU / PAC" ****(1).weaponPic = "C:\2142 vb\img\voss.gif"
****(2) = New weapons ****(2).weaponName = "Baur H-AR" ****(2).ammo = "20 x 7" ****(2).damage = "34" ****(2).firingModes = "Full / Semi" ****(2).reload = "3" ****(2).rpm = "-" ****(2).zoom = "59%" ****(2).heat = "-" ****(2).army = "EU / PAC" ****(2).weaponPic = "C:\2142 vb\img\baur.gif"
****(3) = New weapons ****(3).weaponName = "Krylov FA-37 AR" ****(3).ammo = " x 6" ****(3).damage = "27" ****(3).firingModes = "Full / Semi" ****(3).reload = "3.9" ****(3).rpm = "900" ****(3).zoom = "51.6%" ****(3).heat = "-" ****(3).army = "PAC" ****(3).weaponPic = "C:\2142 vb\img\krylov.gif"
'**** secondary Weapons
****(4) = New weapons ****(4).weaponName = "BJ-2 Combat Knife" ****(4).ammo = "Infinite" ****(4).damage = "30" ****(4).firingModes = "Semi Auto" ****(4).reload = "0" ****(4).rpm = "120" ****(4).zoom = "-" ****(4).heat = "-" ****(4).army = "EU / PAC" ****(4).weaponPic = "C:\2142 vb\img\combatknife.gif"
****(5) = New weapons ****(5).weaponName = "P33 Pereira Pistol" ****(5).ammo = "8 x 6" ****(5).damage = "31" ****(5).firingModes = "Semi Auto" ****(5).reload = "3.8" ****(5).rpm = "360" ****(5).zoom = "20%" ****(5).heat = "-" ****(5).army = "EU" ****(5).weaponPic = "C:\2142 vb\img\pereirpistol.gif"
****(6) = New weapons ****(6).weaponName = "Takao T20 Pistol" ****(6).ammo = "12 x 4" ****(6).damage = "26" ****(6).firingModes = "Semi Auto" ****(6).reload = "3.2" ****(6).rpm = "450" ****(6).zoom = "20%" ****(6).heat = "-" ****(6).army = "PAC" ****(6).weaponPic = "C:\2142 vb\img\takaot20.gif"
'**** - Explosive
****(7) = New weapons ****(7).weaponName = "FRG-1 Grenade" ****(7).ammo = "1 x 2" ****(7).damage = "140 (9m)" ****(7).firingModes = "Semi Auto" ****(7).reload = "0.7" ****(7).rpm = "-" ****(7).zoom = "-" ****(7).heat = "-" ****(7).army = "EU / PAC" ****(7).weaponPic = "c:\2142 vb\1.png"
****(8) = New weapons ****(8).weaponName = "SG-34 Smoke Grenade" ****(8).ammo = "1 x 2" ****(8).damage = "0" ****(8).firingModes = "Semi Auto" ****(8).reload = "0.7" ****(8).rpm = "-" ****(8).zoom = "-" ****(8).heat = "-" ****(8).army = "EU / PAC" ****(8).weaponPic = "c:\2142 vb\1.png"
'**** - Other
****(9) = New weapons ****(9).weaponName = "Medic Upgrade" ****(9).ammo = "-" ****(9).damage = "-" ****(9).firingModes = "-" ****(9).reload = "-" ****(9).rpm = "-" ****(9).zoom = "-" ****(9).heat = "-" ****(9).army = "EU / PAC" ****(9).weaponPic = "c:\2142 vb\1.png"
****(10) = New weapons ****(10).weaponName = "SG-34 Smoke Grenade" ****(10).ammo = "1 x" ****(10).damage = "-" ****(10).firingModes = "Semi Auto" ****(10).reload = "-" ****(10).rpm = "-" ****(10).zoom = "-" ****(10).heat = "-" ****(10).army = "EU / PAC" ****(10).weaponPic = "c:\2142 vb\1.png"
****(11) = New weapons ****(11).weaponName = "AED-6 Defibrillator" ****(11).ammo = "infinite" ****(11).damage = "1000" ****(11).firingModes = "Semi Auto" ****(11).reload = "0.6" ****(11).rpm = "-" ****(11).zoom = "-" ****(11).heat = "50" ****(11).army = "EU / PAC" ****(11).weaponPic = "c:\2142 vb\1.png"
****(12) = New weapons ****(12).weaponName = "SLS Beacon" ****(12).ammo = "1 x 1" ****(12).damage = "-" ****(12).firingModes = "Semi Auto" ****(12).reload = "1.1" ****(12).rpm = "-" ****(12).zoom = "-" ****(12).heat = "-" ****(12).army = "EU / PAC" ****(12).weaponPic = "c:\2142 vb\1.png"
****(13) = New weapons ****(13).weaponName = "unl_best_buy_rifle" ****(13).ammo = "x 6" ****(13).damage = "-" ****(13).firingModes = "Full / Semi" ****(13).reload = "3.9" ****(13).rpm = "-" ****(13).zoom = "51.6%" ****(13).heat = "-" ****(13).army = "EU" ****(13).weaponPic = "c:\2142 vb\1.png"
****(14) = New weapons ****(14).weaponName = "unl_best_buy_rocket" ****(14).ammo = "3 x 4" ****(14).damage = "-" ****(14).firingModes = "Semi Auto" ****(14).reload = "4.5" ****(14).rpm = "100" ****(14).zoom = "51.6%" ****(14).heat = "-" ****(14).army = "EU" ****(14).weaponPic = "c:\2142 vb\1.png"
****(15) = New weapons ****(15).weaponName = "har_rocket" ****(15).ammo = "3 x 4" ****(15).damage = "-" ****(15).firingModes = "Semi Auto" ****(15).reload = "3" ****(15).rpm = "100" ****(15).zoom = "51.6%" ****(15).heat = "-" ****(15).army = "EU / PAC" ****(15).weaponPic = "c:\2142 vb\1.png"
****(16) = New weapons ****(16).weaponName = "lar_rocket" ****(16).ammo = "3 x 4" ****(16).damage = "-" ****(16).firingModes = "Semi Auto" ****(16).reload = "3" ****(16).rpm = "100" ****(16).zoom = "51.6%" ****(16).heat = "-" ****(16).army = "EU / PAC" ****(16).weaponPic = "c:\2142 vb\1.png"
****(17) = New weapons ****(17).weaponName = "har_shotgun" ****(17).ammo = "3 x 4" ****(17).damage = "-" ****(17).firingModes = "Semi Auto" ****(17).reload = "3" ****(17).rpm = "150" ****(17).zoom = "-" ****(17).heat = "-" ****(17).army = "EU / PAC" ****(17).weaponPic = "c:\2142 vb\1.png"
****(18) = New weapons ****(18).weaponName = "lar_shotgun" ****(18).ammo = "3 x 4" ****(18).damage = "-" ****(18).firingModes = "Semi Auto" ****(18).reload = "3" ****(18).rpm = "150" ****(18).zoom = "-" ****(18).heat = "-" ****(18).army = "EU / PAC" ****(18).weaponPic = "c:\2142 vb\1.png"
****(19) = New weapons ****(19).weaponName = "PK-74 AR-Rocket" ****(19).ammo = "3 x 4" ****(19).damage = "-" ****(19).firingModes = "Semi Auto" ****(19).reload = "4.5" ****(19).rpm = "100" ****(19).zoom = "51.6%" ****(19).heat = "-" ****(19).army = "EU" ****(19).weaponPic = "c:\2142 vb\1.png"
****(20) = New weapons ****(20).weaponName = "Herzog AR-Shotgun" ****(20).ammo = "3 x 4" ****(20).damage = "10" ****(20).firingModes = "Semi Auto" ****(20).reload = "4.5" ****(20).rpm = "150" ****(20).zoom = "-" ****(20).heat = "-" ****(20).army = "EU" ****(20).weaponPic = "c:\2142 vb\1.png"
****(21) = New weapons ****(21).weaponName = "PK-74 AR-Rocket" ****(21).ammo = "3 x 4" ****(21).damage = "-" ****(21).firingModes = "Semi Auto" ****(21).reload = "4.3" ****(21).rpm = "100" ****(21).zoom = "51.6%" ****(21).heat = "-" ****(21).army = "PAC" ****(21).weaponPic = "c:\2142 vb\1.png"
****(22) = New weapons ****(22).weaponName = "Herzog AR-Shotgun" ****(22).ammo = "3 x 4" ****(22).damage = "-" ****(22).firingModes = "Semi Auto" ****(22).reload = "4.3" ****(22).rpm = "150" ****(22).zoom = "-" ****(22).heat = "-" ****(22).army = "PAC" ****(22).weaponPic = "c:\2142 vb\1.png"
|
|
|
|
|
DMan1
|
Posted: Visual Basic General, Null Reference Exception |
Top |
|
Public Class frmWelcome
Try
Dim welcome As New frmWelcome
welcome.Show()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OKOnly, "Error")
End Try
Me.Close() |
|
What class and method is the above code located in Because of the Me.Close it looks as if you are creating the class instance from within istself....
|
|
|
|
|
JWhite
|
Posted: Visual Basic General, Null Reference Exception |
Top |
Here is the complete code forthe exit of the second form.
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
key.Close()
Try
Dim welcome As New frmWelcome
welcome.Show()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OKOnly, "Error")
End Try
Me.Close()
End Sub
Key.close is a method call closing a registry key I have open on that form.
Thanks
Joe
|
|
|
|
|
ReneeC
|
Posted: Visual Basic General, Null Reference Exception |
Top |
Have any idea how strange this code looks to a pacifist
|
|
|
|
|
ahmedilyas
|
Posted: Visual Basic General, Null Reference Exception |
Top |
what dman said could be most likely correct, as I have just noticed you are placing the code in the form from which you are executing
|
|
|
|
|
stallion_alpa
|
Posted: Visual Basic General, Null Reference Exception |
Top |
Anyone got any solutions, I've gone through it probably 30 times now and can't see anything that would be causing it.
|
|
|
|
|
JWhite
|
Posted: Visual Basic General, Null Reference Exception |
Top |
I am creating a frmWelcome form from inside the frmOperator form. I was using Me.Hide method to hide the Operator form, rather than closing it. I tried hiding the operator form, to preserve it, but that did not help at all.
Joe
|
|
|
|
|
SJWhiteley
|
Posted: Visual Basic General, Null Reference Exception |
Top |
Where does the error ocurr
|
|
|
|
|
ahmedilyas
|
Posted: Visual Basic General, Null Reference Exception |
Top |
if you do not have to hide the form, you maybe better of using ShowDialog() rather than Show() as ShowDialog() will keep displaying that form and not let any forms from its caller be able to be accessed until the form is closed and only then will the caller get back into action.
how are you calling the form (frmWelcome) - meaning, where abouts in the frmOperator form
|
|
|
|
|
Dave987654321
|
Posted: Visual Basic General, Null Reference Exception |
Top |
“Have any idea how strange this code looks to a pacifist ”
People come here to learn VB programming, not for social/political commentary…
Dave
|
|
|
|
|
ReneeC
|
Posted: Visual Basic General, Null Reference Exception |
Top |
I don't care dave. I am both social and poltical.
|
|
|
|
|
JWhite
|
Posted: Visual Basic General, Null Reference Exception |
Top |
That does not help. I am still getting an unhandled exception Null Reference Exception at that point. I'm still mystified as to where the exception is coming from, since I have try-catch blocks around everything, but it never catches anything. I have try catch blocks around the form creation, the old form destruction, and the new form load. No catches anywhere. I'm at a loss.
|
|
|
|
|
ahmedilyas
|
Posted: Visual Basic General, Null Reference Exception |
Top |
send over the project (email in profile) and include step by step on how you come across the nullref exception :-) Ill be sure to post back with the solution
|
|
|
|
|
Dave987654321
|
Posted: Visual Basic General, Null Reference Exception |
Top |
ReneeC, everyone is both political and social, however that is not why we are here. There are many forums for discussing those subjects. This is a programming forum and I respect it as such and will not be baited into other discussions. Dave
|
|
|
|
|
ReneeC
|
Posted: Visual Basic General, Null Reference Exception |
Top |
No one said a word to you to begin with. I made a poltical statement and I make poltical statements. There is no such thing as "morally neutral". So If you object to my commentary, let microsoft know. But I'm never stop making the commentary. Is that adequately clear
"That is not why we are here."
That is not why YOU are here. I don't leave my poltics at home.
|
|
|
|
|
JWhite
|
Posted: Visual Basic General, Null Reference Exception |
Top |
Thanks to all for your help. After some more searches on the internet, I found some things out. Evidentally, some users have experienced problems with controls with transparent backgrounds. I was using a control on one form that had a transparent background. After setting the backcolor to a definite color, this error has apparently left the building.
Thanks
|
|
|
|
|
Brendan Grant
|
Posted: Visual Basic General, Null Reference Exception |
Top |
At first, second and third glances nothing jumps out at me as being the obvious cause of the exception you are seeing... so like SJWhiteley I’m forced to wonder and ask where abouts the exception is happening.
Any chance that you are modifying the size of the assult or assultDisplay arrays outside of the code pasted above
|
|
|
|
|
jwooley
|
Posted: Visual Basic General, Null Reference Exception |
Top |
Ah, the joys of debugging. Everyone is trying to go down the logical route to determine the source of the problem which turns up to be something completely different.
FWIW, I often find bound controls that are not properly disposed, or where the control is disposed prior to the binding source, can cause this issue as well. In the future, you might try turning on the option to stop on all errors, not just unhandled ones. Another trick I have used in the past is to attach a second instance of VS to the one with the project and turning on stop on all errors. Unfortunatly, this trick does not seem to work in 2005 any more.
Glad you were able to track down the source of the problem this time. Jim Wooley http://devauthority.com/blogs/jwooley
|
|
|
|
|
stallion_alpa
|
Posted: Visual Basic General, Null Reference Exception |
Top |
The error occurs in this line ****Display(count) =
New weaponDisplay(****(count).weaponName, ****(count).ammo,
****(count).damage, ****(count).rpm, ****(count).reload,
****(count).heat, engineer(count).army, ****(count).weaponPic,
****(count).zoom, ****(count).firingModes, count, x, y)
When I'm making new instances of weapons.
The weird thing is its identical to my other 3 except for different names and they work fine.
|
|
|
|
|
stallion_alpa
|
Posted: Visual Basic General, Null Reference Exception |
Top |
So does anyone know what could be causing it
|
|
|
|
|
Brendan Grant
|
Posted: Visual Basic General, Null Reference Exception |
Top |
engineer(count).army ... where are you defining the array engineer and setting it to something That right there looks like a very likely candidate.
|
|
|
|
|
stallion_alpa
|
Posted: Visual Basic General, Null Reference Exception |
Top |
Thx I didn't see that one its working good now.
|
|
|
|
|
|
|