Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 06-22-2005, 01:03 PM   #1 (permalink)
 
True Techie

Join Date: Dec 2004

Posts: 145

Mohan Giri

Default VB.Net Error

Hi,
I tried a simple program in vb.net. In that program I used a class. Ok. I feel difficult to explain. I will post my code here.

Class Definition:

Public Class emp
Public ename As String
Public salary As Integer
Public eno As Integer
End Class


Form Load event code:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim x As emp
x.ename = "Rajesh"
x.eno = 1086
x.salary = 10000
MsgBox(CType(x, emp).eno)
End Sub
End Class

I got an error.

An unhandled exception of type 'System.NullReferenceException' occurred in empdetails.exe

Additional information: Object reference not set to an instance of an object.

Anybody can help me to resolve this error??

Thanks in advance
Mohan Giri is offline  
Old 06-22-2005, 01:20 PM   #2 (permalink)
 
Ultra Techie

Join Date: Sep 2003

Location: Bamberg, Germany

Posts: 549

Iron_Cross

Send a message via ICQ to Iron_Cross Send a message via MSN to Iron_Cross Send a message via Yahoo to Iron_Cross
Default

Code:
Dim x As emp
should be
Code:
Dim x As emp = New emp()
x is still null, you have to call the constructor to actual create the object before using it.

For future reference you could also do it like this:
Code:
Dim x As New emp()
`OR you could also do it like this:
Dim x As emp
x = New emp()

__________________

See today\'s Penny-Arcade!(May contain foul lanuage)
Pain is weakness leaving the body.

PM Me for my MSN
Iron_Cross is offline  
Old 06-22-2005, 04:43 PM   #3 (permalink)
 
True Techie

Join Date: Dec 2004

Posts: 145

Mohan Giri

Default

Thanks Iron_Cross.

Dim x As emp = New emp

This code worked fine. But

Dim x As New emp()

its not working. The system not accepting () after emp.
Mohan Giri is offline  
Old 06-22-2005, 05:03 PM   #4 (permalink)
 
Ultra Techie

Join Date: Sep 2003

Location: Bamberg, Germany

Posts: 549

Iron_Cross

Send a message via ICQ to Iron_Cross Send a message via MSN to Iron_Cross Send a message via Yahoo to Iron_Cross
Default

Hmm, well it's been a long time since I used VB.NET so sorry
I think it's because you have no arguments....but eitherway at least it's working now
__________________

See today\'s Penny-Arcade!(May contain foul lanuage)
Pain is weakness leaving the body.

PM Me for my MSN
Iron_Cross is offline  
Old 06-22-2005, 05:53 PM   #5 (permalink)
 
Super Techie

Join Date: Feb 2005

Posts: 262

jinexile

Default

Yes its because you have no arguments in the constructor of emp... its assuming your trying to make an array but your syntax is wrong
__________________
AMD Athlon 64 3000+ (Overclocked to 3300+)
ASUS K8V-Deluxe
1GB PC3200 DDR
2x200GB Seagate SATA RAID 0
BFG 6800GT OC w\\128MB Ram
2x 19\" Samsung 930B LCDs
jinexile is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On