Object reference not set

  • Thread starter Thread starter localhost
  • Start date Start date
L

localhost

Can someone point me the small error I have made here?


Public Sub InitBestand()
Dim Programma As Application
gstrBestand = Programma.UserAppDataPath
If Strings.Right(gstrBestand, 1) <> "\" Then gstrBestand &= "\"
gstrBestand &= "beginner.txt"
End Sub


The error 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.

The line with the error is:
gstrBestand = Programma.UserAppDataPath

I thought it was enough to do "Dim Programma As Application"
Apparently not?
 
Dim Programma As New Application

Regards - OHM
Can someone point me the small error I have made here?


Public Sub InitBestand()
Dim Programma As Application
gstrBestand = Programma.UserAppDataPath
If Strings.Right(gstrBestand, 1) <> "\" Then gstrBestand &=
"\" gstrBestand &= "beginner.txt"
End Sub


The error 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.

The line with the error is:
gstrBestand = Programma.UserAppDataPath

I thought it was enough to do "Dim Programma As Application"
Apparently not?
 
* localhost said:
Can someone point me the small error I have made here?

Public Sub InitBestand()
Dim Programma As Application
gstrBestand = Programma.UserAppDataPath
If Strings.Right(gstrBestand, 1) <> "\" Then gstrBestand &= "\"
gstrBestand &= "beginner.txt"
End Sub

Skip the 1st line and use this:

\\\
gstrBestand = Application.UserAppDataPath
///
 
???

I'm just working on this one, 'twas my first try too but the constructor for
"Application" is private.
Hex.
 
Dim Programma As New Application

Regards - OHM

I already guessed, thanks anyway. By the way, .NET added paranthesis after
Application.

But now I get "Overload resolution failed because no 'New' is accessible."
 
(e-mail address removed) (Herfried K. Wagner [MVP]) wrote in
Skip the 1st line and use this:

\\\
gstrBestand = Application.UserAppDataPath
///

No, same error again. Now on this new line you suggested.
:(
 
Not totally confident on why your solution doesn't work but for the
meantime, try

Public Sub InitBestand()
' Dim Programma As Application
' gstrBestand = Programma.UserAppDataPath
gstrBestand = Application.UserAppDataPath
If Strings.Right(gstrBestand, 1) <> "\" Then gstrBestand &= "\"
End Sub

Hexathioorthooxalate
 
???

I'm just working on this one, 'twas my first try too but the
constructor for "Application" is private.
Hex.

I'm not as good as most people here, but is "private constructor for
Application" just another way of saying you cannot use "New"?

PS: until a week ago I dabbled around in VB6, so bear with me. :)
 
localhost said:
I'm not as good as most people here, but is "private constructor for
Application" just another way of saying you cannot use "New"?

Yes.

It is odd you are getting this error. Both your original solution, and the
one I posted a few minutes ago, work fine for me (VS2003, .NET 1.1).
 
Yes.

It is odd you are getting this error. Both your original solution, and
the one I posted a few minutes ago, work fine for me (VS2003, .NET
1.1).

I am using VS2002 and .NET 1.0.
Could that be the root cause of all evil?
 
Just to so we'll all looking at the same picture, create a new WinForms
application with a single form (called Form1), pop a button on the form
(called Button1), and paste in the following.

When you click the Button, do you get the same error?


Option Strict On
Option Explicit On

Public Class Form1
Inherits System.Windows.Forms.Form

'[+]Windows Form Designer Code

Public Sub InitBestand()
Dim gstrBestand As String = Application.UserAppDataPath
If Strings.Right(gstrBestand, 1) <> "\" Then gstrBestand &= "\"
MsgBox(gstrBestand)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Call InitBestand()
End Sub
End Class
 
Just to so we'll all looking at the same picture, create a new WinForms
application with a single form (called Form1), pop a button on the form
(called Button1), and paste in the following.

When you click the Button, do you get the same error?


Option Strict On
Option Explicit On

Public Class Form1
Inherits System.Windows.Forms.Form

'[+]Windows Form Designer Code

Public Sub InitBestand()
Dim gstrBestand As String = Application.UserAppDataPath
If Strings.Right(gstrBestand, 1) <> "\" Then gstrBestand &= "\"
MsgBox(gstrBestand)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Call InitBestand()
End Sub
End Class

Hmmm... that works?
What now? Throw away my old application and copy/paste the code to the new
one?
 
* localhost said:
No, same error again. Now on this new line you suggested.
:(

Are you using 'System.Windows.Forms.Application' or a user defined
class? Maybe you have to instantiate this class:

\\\
Dim d As New Application()
///
 
localhost said:
Can someone point me the small error I have made here?
Public Sub InitBestand()
Dim Programma As Application
gstrBestand = Programma.UserAppDataPath
If Strings.Right(gstrBestand, 1) <> "\" Then gstrBestand &= "\"
gstrBestand &= "beginner.txt"
End Sub
I thought it was enough to do "Dim Programma As Application"
Apparently not?

I believe two things are going on here. The first problem is that you are
defining variable as type Application but not assigning it any reference to
an Application. You cannot create an instance of the Application class. So
that's why you are getting the null reference exception, the reference is
null.

Now as to why it doesn't work when you reference Application.UserAppDataPath
directly? Something else is up. When something odd happens I suggest you
simply start over with a new test app and just try to type the single line
Console.Writeline(Application.UserAppDataPath)

You should see UserAppDataPath show up in IntelliSense and of course it
should output the path when you run it.

Tom
 
Are you using 'System.Windows.Forms.Application' or a user defined
class? Maybe you have to instantiate this class:

\\\
Dim d As New Application()
///

uhhh... dunno.
I started again with a new app and copied all the code over.
No more problems now...
 
Or go bug hunting!

A technique I often use when bug hunting, is putting MsgBox-es all over to
see what is going on. I've learned one thing: NEVER EVER put a msgbox in a
sub that handles the activated event of a form...
form activated --> msgbox pops up --> form is deactivated
close msgbox --> form is activated again --> msgbox popup...

aaaaaargh!
:-@
Glad to be of help.
Hex.

Thanks for giving me a few clues.
 
Now as to why it doesn't work when you reference
Application.UserAppDataPath directly? Something else is up. When
something odd happens I suggest you simply start over with a new test
app and just try to type the single line
Console.Writeline(Application.UserAppDataPath)

Yup, already did that.
See Message-ID: <n9%[email protected]>
 
Now as to why it doesn't work when you reference
Application.UserAppDataPath directly? Something else is up. When
something odd happens I suggest you simply start over with a new test
app and just try to type the single line
Console.Writeline(Application.UserAppDataPath)

Already did that.
See Message-ID: <[email protected]>
Thanks anyway.
 
Back
Top