Click here to Skip to main content
15,908,673 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have converted a c++ project(which runs) to c# where it throws a null reference exception here is a small sample
public class EQSTATE
{...public double lg; // low  gain...
}
EQSTATE es;

init_3band_state(es, 880,5000,480000);
Then
private void init_3band_state(EQSTATE es, int lowfreq, int highfreq, int mixfreq)
{
  // Clear state
  //memset(es, 0, sizeof(EQSTATE)); does not compile in C++ nor of course c# but runs without it
  es.lg = 1.0; //=============this is where the null reference exception is thrown
  es.mg = 1.0; ditto
  es.hg = 1.0; ditto
}
Posted
Updated 13-Apr-15 18:57pm
v2
Comments
Member 10987276 14-Apr-15 1:47am    
Thanks for the quick reply. EQSTATE es = new EQSTATE(); this worked best as it required less mods to code.
Member 10987276 14-Apr-15 1:57am    
And the struct fix worked without the 'new EQSTATE'

1 solution

In C++ es defines a value on the stack. In C# es defines a reference that should be initialized with an actual object (using the new keyword).


For initializing es with an object (on the heap), you can change the definition to something like:


C#
EQSTATE es = new EQSTATE();

P.S. You can define es on the stack also in C#, by making EQSTATE a value type (by using the struct keyword instead of class).

 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900