473,403 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

System.NullReferenceException on API call

Hi All,

I am upgrading my app (working well with VB6) to VB.NET. It sends MIDI
messages using the API multimedia winmm.dll .
These functions are declared:

Public Declare Function timeSetEvent Lib "winmm.dll" (ByVal uDelay As
Integer, _
ByVal uResolution As Integer, ByVal lpFunction As MidiPlayDelegate, _
ByVal dwUser As Integer, ByVal uFlags As Integer) As Integer

Delegate Function MidiPlayDelegate(ByVal lTimerID As Integer, ByVal dwMsg As
Integer, _
ByVal dwUser As Integer, ByVal lParam1 As Integer, ByVal lParam2 As
Integer) As Integer

and a function MidiPlay is available:
Public Function MidiPlay(ByVal lTimerID As Integer, ByVal dwMsg As Integer,
_
ByVal dwUser As Integer, ByVal lParam1 As Integer, ByVal lParam2 As
Integer) As Integer
...
lRet = midiOutShortMsg(hMidiOut, mididata)
...
End Function
----------------------------------------------------------------------------
-----

A MIDI port is opened by a call to midiOutOpen, then a timer is started by
the statement
lTimerID = timeSetEvent(1, 1, AddressOf MidiPlay, 0, 1)
which runs in its own thread.
On each time event the call back function MidiPlay is called, which in turn
sends the MIDI data through
lRet = midiOutShortMsg(hMidiOut, mididata)

When the program is run a System.NullReferenceException occurs in an Unknown
Module (the
MidiPlayDelegate, I guess), because
"Object reference not set to an instance of an object"

Which object reference should be set?

Thank you for your help,

ReinierVDW

Nov 20 '05 #1
2 2908
Your problem is probably on this line:

lTimerID = timeSetEvent(1, 1, AddressOf MidiPlay, 0, 1)

The delegate created by the AddressOf MidiPlay statement does not have any
references to it so it will be garbage collected. If you need a delegate
that will stick around for future callbacks you must keep a live reference
to it. For example:

in your class create a class level delegate variable
Private m_TimerCallback as MDIPlayDelegate

then in the code that sets up the timer callbacks:

m_TimerCallback = New MDIPlayDelegate(AddressOf MDIPlay)
lTimerID = timeSetEvent(1, 1, m_TimerCallback, 0, 1)

now the delegate will stick around as long as your class does.
"R. van der Welle" <r.************@onzin.freeler.nl> wrote in message
news:eA**************@tk2msftngp13.phx.gbl...
Hi All,

I am upgrading my app (working well with VB6) to VB.NET. It sends MIDI
messages using the API multimedia winmm.dll .
These functions are declared:

Public Declare Function timeSetEvent Lib "winmm.dll" (ByVal uDelay As
Integer, _
ByVal uResolution As Integer, ByVal lpFunction As MidiPlayDelegate, _
ByVal dwUser As Integer, ByVal uFlags As Integer) As Integer

Delegate Function MidiPlayDelegate(ByVal lTimerID As Integer, ByVal dwMsg As Integer, _
ByVal dwUser As Integer, ByVal lParam1 As Integer, ByVal lParam2 As
Integer) As Integer

and a function MidiPlay is available:
Public Function MidiPlay(ByVal lTimerID As Integer, ByVal dwMsg As Integer, _
ByVal dwUser As Integer, ByVal lParam1 As Integer, ByVal lParam2 As
Integer) As Integer
...
lRet = midiOutShortMsg(hMidiOut, mididata)
...
End Function
-------------------------------------------------------------------------- -- -----

A MIDI port is opened by a call to midiOutOpen, then a timer is started by
the statement
lTimerID = timeSetEvent(1, 1, AddressOf MidiPlay, 0, 1)
which runs in its own thread.
On each time event the call back function MidiPlay is called, which in turn sends the MIDI data through
lRet = midiOutShortMsg(hMidiOut, mididata)

When the program is run a System.NullReferenceException occurs in an Unknown Module (the
MidiPlayDelegate, I guess), because
"Object reference not set to an instance of an object"

Which object reference should be set?

Thank you for your help,

ReinierVDW

Nov 20 '05 #2
* "R. van der Welle" <r.************@onzin.freeler.nl> scripsit:
A MIDI port is opened by a call to midiOutOpen, then a timer is started by
the statement
lTimerID = timeSetEvent(1, 1, AddressOf MidiPlay, 0, 1)
which runs in its own thread.
On each time event the call back function MidiPlay is called, which in turn
sends the MIDI data through
lRet = midiOutShortMsg(hMidiOut, mididata)

When the program is run a System.NullReferenceException occurs in an Unknown
Module (the
MidiPlayDelegate, I guess), because
"Object reference not set to an instance of an object"

Which object reference should be set?


Don't forget to save a reference to the delegate!

Simple sample written by Armin Zingler:

\\\
dim o as CallBack

o = new CallBack(addressof CallBackSub)
'...

private sub CallBackSub
end sub
///

--
Herfried K. Wagner [MVP]
<http://dotnet.mvps.org/>
Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Terrence | last post by:
I am doing some of the C# walkthroughs to transition from VB to C#. When I try to execute static void Main() { Aplication.Run(new Form1()) } I raise a 'System.NullReferenceException" in...
0
by: Yoni Rabinovitch | last post by:
Hi, I am new to C#, so I apologise if this is a stupid question. I have some 3rd party C++ code, which gets built as static libraries (not DLLs). I want to create a C# form, which will call...
1
by: gregory_may | last post by:
This code seems to "work" but I get the following errors: An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll then this one: An unhandled...
1
by: Rafael | last post by:
Hi, I hope I can find some help for this problem IDE: Visual Studio.NET 2003 Developer Editio Language: C# Problem: "An unhandled exception of type 'System.NullReferenceException' occurred in...
0
by: sarah | last post by:
Using .NET C# installed Framework 1.1 with Service Pack 1 I have a MDI application in C# that contains 3 different forms. Form 1 has several controls one of which is a DataGrid control that...
0
by: muralidharan | last post by:
WebForm1.aspx Code: <%@ Register TagPrefix="ComponentArt" Namespace="ComponentArt.Web.UI" Assembly="ComponentArt.Web.UI" %> <ComponentArt:TreeView id="TreeView1" Height="520"...
2
by: Raed Sawalha | last post by:
i have a windows form(Main) with listview, when click an item in listview i open other window form (Sub) which generate the selected item from parent window in as treeview items when click any item...
3
by: Patrick.O.Ige | last post by:
I'm loading an Array below but getting the error "Object reference not set to an instance saying 'ItemNumber = CType(Args.Item.FindControl("ItemNumber"), TextBox).Text' is the error line. I DON'T...
6
by: William Mild | last post by:
I must be getting brain fried. I can't see the error. Create a new web form with the following code begind: Public Class test Inherits System.Web.UI.Page Public Class ReportCardData ...
3
by: Alex J. | last post by:
I just started to learn C# (my background is in C++), and right now I study the sockets, TCP/IP etc... so, I found a usefull source code at:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.