Click here to Skip to main content
12,763,299 members (37,707 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB VB.NET
I want to call a function which returns boolean using a variable supplied by the user but to no avail ... Here's the code

        Try
            Dim ass As Assembly = Assembly.LoadFile(Application.StartupPath & "\ReflectionLib.dll")
            Dim t_sam As Type = ass.GetType("ReflectionLib")
            Dim hh_m As MethodInfo = t_sam.GetMethod("ShowX", BindingFlags.Public Or BindingFlags.Static)
            hh_m.Invoke(Nothing, New Object() {})
        Catch ex As Exception
            MessageBox.Show(ex.Message + ex.StackTrace + vbLf)
        End Try


I keep getting Object reference not set to an instance of object error with this code

hh_m.Invoke(Nothing, New Object() {})
Posted 23-Jan-13 22:37pm
Updated 23-Jan-13 23:58pm
v3
Comments
Abhishek Pant 24-Jan-13 3:46am
   
code?
PIEBALDconsult 24-Jan-13 3:48am
   
Why? What function? What have you tried so far? Wouldn't you rather go bowling?
__BrokenArrow__ 24-Jan-13 4:17am
   
I want to call a function which returns boolean using a variable supplied by the user but to no avail ... Here's the code


Try
Dim ass As Assembly = Assembly.LoadFile(Application.StartupPath & "\ReflectionLib.dll")
Dim t_sam As Type = ass.GetType("ReflectionLib")
Dim hh_m As MethodInfo = t_sam.GetMethod("ShowX", BindingFlags.Public Or BindingFlags.Static)
hh_m.Invoke(Nothing, New Object() {})
Catch ex As Exception
MessageBox.Show(ex.Message + ex.StackTrace + vbLf)
End Try

I keep getting Object reference not set to an instance of object error with this code

hh_m.Invoke(Nothing, New Object() {})
__BrokenArrow__ 24-Jan-13 4:17am
   
I want to call a function which returns boolean using a variable supplied by the user but to no avail ... Here's the code


Try
Dim ass As Assembly = Assembly.LoadFile(Application.StartupPath & "\ReflectionLib.dll")
Dim t_sam As Type = ass.GetType("ReflectionLib")
Dim hh_m As MethodInfo = t_sam.GetMethod("ShowX", BindingFlags.Public Or BindingFlags.Static)
hh_m.Invoke(Nothing, New Object() {})
Catch ex As Exception
MessageBox.Show(ex.Message + ex.StackTrace + vbLf)
End Try

I keep getting Object reference not set to an instance of object error with this code

hh_m.Invoke(Nothing, New Object() {})
PIEBALDconsult 24-Jan-13 4:21am
   
Which line throws the exception?You didn't supply a value.
Are you sure the Method exists?
PIEBALDconsult 24-Jan-13 4:22am
   
You still didn't answer why.
Earloc 24-Jan-13 4:28am
   
you try to invoke a method on "Nothing (null in C#)"..no wonder this throws a NullReferenceException...

btw: really bad variable naming. you should always use names that actually make sense...
PIEBALDconsult 24-Jan-13 4:30am
   
I believe that's because it's a static method, but he hasn't shown the definition of the method.
Earloc 24-Jan-13 4:30am
   
yeah, just noticed the BindingFlag :)
__BrokenArrow__ 24-Jan-13 4:34am
   
I have created the dll with this and played around but the same error occurs with the line
hh_m.Invoke(Nothing, New Object() {})

And I tried using a function with parameters and changeing the lines to something like this

Dim args as Object = New Object() {variable}
hh_m.Invoke(Nothing, args)

But still the same error...This function is supposed to check and validate a user input on my database for an activation...Trick is the dll should be located elsewhere on the hdd
PIEBALDconsult 24-Jan-13 4:36am
   
Step through with the debugger and see whether or not hh_m is nothing.
__BrokenArrow__ 24-Jan-13 4:43am
   
Imports System.Windows.Forms

Public Class ReflectionLib
Public Shared Function ShowX(ByVal x As Integer) As Boolean

Dim t As Boolean = True
Dim f As Boolean = False
Dim chk As Boolean
Dim retstring As String = ""

'' I just removed the code for the internet check'''
'' Partial of this is to get the string from my database located on the server and resend it to the application'''

If retstring = Nothing Then
chk = f
Else
chk = t
End If
Return chk & retstring
End Function
End Class
PIEBALDconsult 24-Jan-13 4:44am
   
Ah, don't you need to specify the parameter types when you call GetMethod ?
__BrokenArrow__ 24-Jan-13 4:48am
   
Where?
__BrokenArrow__ 24-Jan-13 4:52am
   
Like this??

Dim oo As Object = New Object() {10}
Dim objInstance As Object = Activator.CreateInstance(type)

method.Invoke(objInstance, oo)
PIEBALDconsult 24-Jan-13 4:53am
   
No
__BrokenArrow__ 24-Jan-13 4:59am
   
Then what would you prescribe on my impossible solution? Lol we'll I am battling
PIEBALDconsult 24-Jan-13 5:01am
   
http://msdn.microsoft.com/en-us/library/5fed8f59.aspx
__BrokenArrow__ 24-Jan-13 5:04am
   
Dim method As MethodInfo = type.GetMethod(strMethodName) """This is the line giving me the error code
PIEBALDconsult 24-Jan-13 5:08am
   
Yes, you need to specify exactly the method you want, including the parameter types.
__BrokenArrow__ 24-Jan-13 5:19am
   
Oh so it should look something like this then
Dim method As MethodInfo = type.GetMethod(strMethodName,int32 or just the number or string?)
PIEBALDconsult 24-Jan-13 5:26am
   
Read the documentation I linked to.
(Binder and ParameterModifier may be null or empty in this case.)
I'm off to bed. Good luck.
Logi Guna 24-Jan-13 11:55am
   
so the method doesn't exist or the parameter specified was invalid right?
you can use dotnet reflector, load your ReflectionLib.dll into reflector
and see whats wrong there (type declaration, method name, etc...)
__BrokenArrow__ 25-Jan-13 0:50am
   
I found the solution to my problem... Posting it now
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Put breakpoint at line 1 in code you posted and step through using F10. See which object is null and figure out why.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

The following code solved my hick up concerning the Invocation of the method... I kept on getting the error that my type was in fact null, so I browsed on old posts and found this solution, though it has to be tweaked a little to suit my needs it works fine. thanks for your valued input I look forward to working with you soon


Imports System.Reflection

Public Class Form1

Private asm As System.Reflection.Assembly
Private t As Type()
Private ty As Type
Private m As MethodInfo()
Private mm As MethodInfo

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Here wec have to load the assembly of classlibrary by givig the full path of our classLibrary.dll

asm = System.Reflection.Assembly.LoadFrom(Environment.CurrentDirectory + "\dll\ReflectionLib.dll")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
t = asm.GetTypes()
For i As Integer = 0 To t.Length - 1
ListBox1.Items.Add(t(i).FullName)
Next i
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
mm = ty.GetMethod(ListBox2.SelectedItem.ToString())
Dim o As Object
o = Activator.CreateInstance(ty)
Dim oo As Object() = {Int32.Parse(TextBox1.Text), Int32.Parse(TextBox2.Text)}
'If there is no parameter in method then on the place of oo pass the null

TextBox3.Text = mm.Invoke(o, oo).ToString()
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
ListBox2.Items.Clear()
ty = asm.GetType(ListBox1.SelectedItem.ToString())
m = ty.GetMethods()
For j As Integer = 0 To m.Length - 1
ListBox2.Items.Add(m(j).Name)
Next j
End Sub
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.170217.1 | Last Updated 25 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100