473,320 Members | 1,946 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,320 software developers and data experts.

This code crashes, what am I doing wrong????

ASP.NET 2.0

This code crashes. It generate this error:
Value cannot be null.
Parameter name: type

I've created some custom web.config settings and this crash is related to
accessing theme custom settings. This code is where the crash occur:
_instance =
(DAL)Activator.CreateInstance(Type.GetType(min.tes t.Config.Settings.msgElement.ProviderType));
This code is trying to do is to create an instance of the DAL provider,

************* custom web.config settings: **********************
<configSections>
<section name="msg" type="min.test.ConfigSection, __code"/>
</configSections>
<msg>
<messages providerType="min.test.DAL.SqlServer2005"/>
</msg>

********* In this code the crash occur *********
/* This code is from the DAL class, this code is trying to create an
instance of the DAL set in web.config - min.test.DAL.SqlServer2005
static public DAL Instance
{
get
{
if (_instance == null)
_instance =
(DAL)Activator.CreateInstance(Type.GetType(min.tes t.Config.Settings.msgElement.ProviderType));
<--- code crashes here
return _instance;
}
}

******** definition of the custom settings in web.config
**********************
using System;
using System.Collections.Generic;
using System.Configuration;

namespace min.test
{
public class ConfigSection : ConfigurationSection
{
[ConfigurationProperty("messages", IsRequired = true)]
public MsgElement msgElement
{
get { return (MsgElement)base["messages"]; }
}

}

public class MsgElement : ConfigurationElement
{
[ConfigurationProperty("providerType", DefaultValue =
"min.test.DAL.SqlServer2005")]
public string ProviderType
{
get { return (string)base["providerType"]; }
set { base["providerType"] = value; }
}
}

}

Any suggestions??

Jeff
Apr 9 '07 #1
5 1524
Well,
The exception message doesn't lie - it's telling you that you've got a null
value, and it cannot accept it and continue.

One way to get at this is to "take apart" your compound statement so that it
will becom easier to set breakpoints and examine values, e.g.:

instance =
(DAL)Activator.CreateInstance(Type.GetType(min.tes t.Config.Settings.msgElement.ProviderType));

could be:

Type t = Type.GetType(min.test.Config.Settings.msgElement.P roviderType);
// check to see if t above is null.

instance = (DAL)Activator.CreateInstance(t);

Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Jeff" wrote:
ASP.NET 2.0

This code crashes. It generate this error:
Value cannot be null.
Parameter name: type

I've created some custom web.config settings and this crash is related to
accessing theme custom settings. This code is where the crash occur:
_instance =
(DAL)Activator.CreateInstance(Type.GetType(min.tes t.Config.Settings.msgElement.ProviderType));
This code is trying to do is to create an instance of the DAL provider,

************* custom web.config settings: **********************
<configSections>
<section name="msg" type="min.test.ConfigSection, __code"/>
</configSections>
<msg>
<messages providerType="min.test.DAL.SqlServer2005"/>
</msg>

********* In this code the crash occur *********
/* This code is from the DAL class, this code is trying to create an
instance of the DAL set in web.config - min.test.DAL.SqlServer2005
static public DAL Instance
{
get
{
if (_instance == null)
_instance =
(DAL)Activator.CreateInstance(Type.GetType(min.tes t.Config.Settings.msgElement.ProviderType));
<--- code crashes here
return _instance;
}
}

******** definition of the custom settings in web.config
**********************
using System;
using System.Collections.Generic;
using System.Configuration;

namespace min.test
{
public class ConfigSection : ConfigurationSection
{
[ConfigurationProperty("messages", IsRequired = true)]
public MsgElement msgElement
{
get { return (MsgElement)base["messages"]; }
}

}

public class MsgElement : ConfigurationElement
{
[ConfigurationProperty("providerType", DefaultValue =
"min.test.DAL.SqlServer2005")]
public string ProviderType
{
get { return (string)base["providerType"]; }
set { base["providerType"] = value; }
}
}

}

Any suggestions??

Jeff
Apr 9 '07 #2
Thanks

Type t = Type.GetType(min.test.Config.Settings.msgElement.P roviderType);
t gets a NULL value here. I've looked at the web.config settings and the
code that creates the custom web.config settings and I cannot see what I'm
doing wrong here... or maybe I'm becoming blind to my own programming
errors. Do you see why it get NULL value??

Jeff

"Peter Bromberg [C# MVP]" <pb*******@yahoo.yabbadabbadoo.comwrote in
message news:AC**********************************@microsof t.com...
Well,
The exception message doesn't lie - it's telling you that you've got a
null
value, and it cannot accept it and continue.

One way to get at this is to "take apart" your compound statement so that
it
will becom easier to set breakpoints and examine values, e.g.:

instance =
(DAL)Activator.CreateInstance(Type.GetType(min.tes t.Config.Settings.msgElement.ProviderType));

could be:

Type t = Type.GetType(min.test.Config.Settings.msgElement.P roviderType);
// check to see if t above is null.

instance = (DAL)Activator.CreateInstance(t);

Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Jeff" wrote:
>ASP.NET 2.0

This code crashes. It generate this error:
Value cannot be null.
Parameter name: type

I've created some custom web.config settings and this crash is related to
accessing theme custom settings. This code is where the crash occur:
_instance =
(DAL)Activator.CreateInstance(Type.GetType(min.te st.Config.Settings.msgElement.ProviderType));
This code is trying to do is to create an instance of the DAL provider,

************* custom web.config settings: **********************
<configSections>
<section name="msg" type="min.test.ConfigSection, __code"/>
</configSections>
<msg>
<messages providerType="min.test.DAL.SqlServer2005"/>
</msg>

********* In this code the crash occur *********
/* This code is from the DAL class, this code is trying to create an
instance of the DAL set in web.config - min.test.DAL.SqlServer2005
static public DAL Instance
{
get
{
if (_instance == null)
_instance =
(DAL)Activator.CreateInstance(Type.GetType(min.te st.Config.Settings.msgElement.ProviderType));
<--- code crashes here
return _instance;
}
}

******** definition of the custom settings in web.config
**********************
using System;
using System.Collections.Generic;
using System.Configuration;

namespace min.test
{
public class ConfigSection : ConfigurationSection
{
[ConfigurationProperty("messages", IsRequired = true)]
public MsgElement msgElement
{
get { return (MsgElement)base["messages"]; }
}

}

public class MsgElement : ConfigurationElement
{
[ConfigurationProperty("providerType", DefaultValue =
"min.test.DAL.SqlServer2005")]
public string ProviderType
{
get { return (string)base["providerType"]; }
set { base["providerType"] = value; }
}
}

}

Any suggestions??

Jeff

Apr 9 '07 #3
Type.GetType gives you the ability to get a type back from a string. You pass
a known, available type name string to it, and expect it return that type.

If the assembly name is specified in the typeName string, Type.GetType()
will search inside this assembly only; otherwise, it tries to find one in the
caller assembly and then the system assembly (mscorlib.dll). Anything after
',' in the typeName string is treated as assembly name. To make it work,you
need to specify "YourAssembly.dll" in typeName. In contrast,
Type.GetType("System.Int32") can return type System.Int32 without mentioning
"mscorlib.dll".

Type.AssemblyQualifiedName is the right way to tell what kind of typeName
you should provide.
typeof(System.Data.SqlClient.SqlException).Assembl yQualifiedName equals:
"System.Data.SqlClient.SqlException, System.Data, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089". Type.GetType with this
long string will succeed in .NET 2.0.

In .NET, namespace and the associated assembly name are not necessarily
related. Type "System.Data.SqlClient.SqlException" is not associated with
"System.Data.SqlClient.dll" (which does not exist).

-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Jeff" wrote:
Thanks

Type t = Type.GetType(min.test.Config.Settings.msgElement.P roviderType);
t gets a NULL value here. I've looked at the web.config settings and the
code that creates the custom web.config settings and I cannot see what I'm
doing wrong here... or maybe I'm becoming blind to my own programming
errors. Do you see why it get NULL value??

Jeff

"Peter Bromberg [C# MVP]" <pb*******@yahoo.yabbadabbadoo.comwrote in
message news:AC**********************************@microsof t.com...
Well,
The exception message doesn't lie - it's telling you that you've got a
null
value, and it cannot accept it and continue.

One way to get at this is to "take apart" your compound statement so that
it
will becom easier to set breakpoints and examine values, e.g.:

instance =
(DAL)Activator.CreateInstance(Type.GetType(min.tes t.Config.Settings.msgElement.ProviderType));

could be:

Type t = Type.GetType(min.test.Config.Settings.msgElement.P roviderType);
// check to see if t above is null.

instance = (DAL)Activator.CreateInstance(t);

Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Jeff" wrote:
ASP.NET 2.0

This code crashes. It generate this error:
Value cannot be null.
Parameter name: type

I've created some custom web.config settings and this crash is related to
accessing theme custom settings. This code is where the crash occur:
_instance =
(DAL)Activator.CreateInstance(Type.GetType(min.tes t.Config.Settings.msgElement.ProviderType));
This code is trying to do is to create an instance of the DAL provider,

************* custom web.config settings: **********************
<configSections>
<section name="msg" type="min.test.ConfigSection, __code"/>
</configSections>
<msg>
<messages providerType="min.test.DAL.SqlServer2005"/>
</msg>

********* In this code the crash occur *********
/* This code is from the DAL class, this code is trying to create an
instance of the DAL set in web.config - min.test.DAL.SqlServer2005
static public DAL Instance
{
get
{
if (_instance == null)
_instance =
(DAL)Activator.CreateInstance(Type.GetType(min.tes t.Config.Settings.msgElement.ProviderType));
<--- code crashes here
return _instance;
}
}

******** definition of the custom settings in web.config
**********************
using System;
using System.Collections.Generic;
using System.Configuration;

namespace min.test
{
public class ConfigSection : ConfigurationSection
{
[ConfigurationProperty("messages", IsRequired = true)]
public MsgElement msgElement
{
get { return (MsgElement)base["messages"]; }
}

}

public class MsgElement : ConfigurationElement
{
[ConfigurationProperty("providerType", DefaultValue =
"min.test.DAL.SqlServer2005")]
public string ProviderType
{
get { return (string)base["providerType"]; }
set { base["providerType"] = value; }
}
}

}

Any suggestions??

Jeff


Apr 9 '07 #4
Jeff,

In addition to Peter's reply, there's another way of obtaining the reference
in ASP.NET 2.0 dynamic compilation enviroment. Please use
System.Web.Compilation.BuildManager.GetType(min.te st.Config.Settings.msgElement.ProviderType, true);
instead.

Regards
--
Milosz
"Peter Bromberg [C# MVP]" wrote:
Type.GetType gives you the ability to get a type back from a string. You pass
a known, available type name string to it, and expect it return that type.

If the assembly name is specified in the typeName string, Type.GetType()
will search inside this assembly only; otherwise, it tries to find one in the
caller assembly and then the system assembly (mscorlib.dll). Anything after
',' in the typeName string is treated as assembly name. To make it work,you
need to specify "YourAssembly.dll" in typeName. In contrast,
Type.GetType("System.Int32") can return type System.Int32 without mentioning
"mscorlib.dll".

Type.AssemblyQualifiedName is the right way to tell what kind of typeName
you should provide.
typeof(System.Data.SqlClient.SqlException).Assembl yQualifiedName equals:
"System.Data.SqlClient.SqlException, System.Data, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089". Type.GetType with this
long string will succeed in .NET 2.0.

In .NET, namespace and the associated assembly name are not necessarily
related. Type "System.Data.SqlClient.SqlException" is not associated with
"System.Data.SqlClient.dll" (which does not exist).

-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Jeff" wrote:
Thanks

Type t = Type.GetType(min.test.Config.Settings.msgElement.P roviderType);
t gets a NULL value here. I've looked at the web.config settings and the
code that creates the custom web.config settings and I cannot see what I'm
doing wrong here... or maybe I'm becoming blind to my own programming
errors. Do you see why it get NULL value??

Jeff

"Peter Bromberg [C# MVP]" <pb*******@yahoo.yabbadabbadoo.comwrote in
message news:AC**********************************@microsof t.com...
Well,
The exception message doesn't lie - it's telling you that you've got a
null
value, and it cannot accept it and continue.
>
One way to get at this is to "take apart" your compound statement so that
it
will becom easier to set breakpoints and examine values, e.g.:
>
instance =
(DAL)Activator.CreateInstance(Type.GetType(min.tes t.Config.Settings.msgElement.ProviderType));
>
could be:
>
Type t = Type.GetType(min.test.Config.Settings.msgElement.P roviderType);
// check to see if t above is null.
>
instance = (DAL)Activator.CreateInstance(t);
>
Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net
>
>
>
>
"Jeff" wrote:
>
>ASP.NET 2.0
>>
>This code crashes. It generate this error:
>Value cannot be null.
>Parameter name: type
>>
>I've created some custom web.config settings and this crash is related to
>accessing theme custom settings. This code is where the crash occur:
>_instance =
>(DAL)Activator.CreateInstance(Type.GetType(min.te st.Config.Settings.msgElement.ProviderType));
>This code is trying to do is to create an instance of the DAL provider,
>>
>************* custom web.config settings: **********************
><configSections>
> <section name="msg" type="min.test.ConfigSection, __code"/>
></configSections>
><msg>
> <messages providerType="min.test.DAL.SqlServer2005"/>
></msg>
>>
>********* In this code the crash occur *********
>/* This code is from the DAL class, this code is trying to create an
>instance of the DAL set in web.config - min.test.DAL.SqlServer2005
>static public DAL Instance
>{
> get
> {
> if (_instance == null)
> _instance =
>(DAL)Activator.CreateInstance(Type.GetType(min.te st.Config.Settings.msgElement.ProviderType));
><--- code crashes here
> return _instance;
> }
>}
>>
>******** definition of the custom settings in web.config
>**********************
>using System;
>using System.Collections.Generic;
>using System.Configuration;
>>
>namespace min.test
>{
> public class ConfigSection : ConfigurationSection
> {
> [ConfigurationProperty("messages", IsRequired = true)]
> public MsgElement msgElement
> {
> get { return (MsgElement)base["messages"]; }
> }
>>
> }
>>
> public class MsgElement : ConfigurationElement
> {
> [ConfigurationProperty("providerType", DefaultValue =
>"min.test.DAL.SqlServer2005")]
> public string ProviderType
> {
> get { return (string)base["providerType"]; }
> set { base["providerType"] = value; }
> }
> }
>>
>}
>>
>Any suggestions??
>>
>Jeff
>>
>>
>>
Apr 9 '07 #5
thank you guys, you helped me solve this problem.

This is a bit embarrassing. I tryed to create an instance of this class:
min.test.DAL.SqlServer2005, but this class don't exist in my project.
min.test.DAL.SqlServer2005.SqlServer2005 is the class I needed.... I had
forgot that I had a namespace with the same name as the class...
anyway I changed the .SqlServer2005 namespace to SqlClient so now I'm
accessing min.test.DAL.SqlClient.SqlServer2005

once again, thank you

cheers

Jeff
"Milosz Skalecki [MCAD]" <mi*****@DONTLIKESPAMwp.plwrote in message
news:FB**********************************@microsof t.com...
Jeff,

In addition to Peter's reply, there's another way of obtaining the
reference
in ASP.NET 2.0 dynamic compilation enviroment. Please use
System.Web.Compilation.BuildManager.GetType(min.te st.Config.Settings.msgElement.ProviderType,
true);
instead.

Regards
--
Milosz
"Peter Bromberg [C# MVP]" wrote:
>Type.GetType gives you the ability to get a type back from a string. You
pass
a known, available type name string to it, and expect it return that
type.

If the assembly name is specified in the typeName string, Type.GetType()
will search inside this assembly only; otherwise, it tries to find one in
the
caller assembly and then the system assembly (mscorlib.dll). Anything
after
',' in the typeName string is treated as assembly name. To make it
work,you
need to specify "YourAssembly.dll" in typeName. In contrast,
Type.GetType("System.Int32") can return type System.Int32 without
mentioning
"mscorlib.dll".

Type.AssemblyQualifiedName is the right way to tell what kind of typeName
you should provide.
typeof(System.Data.SqlClient.SqlException).Assemb lyQualifiedName equals:
"System.Data.SqlClient.SqlException, System.Data, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089". Type.GetType with this
long string will succeed in .NET 2.0.

In .NET, namespace and the associated assembly name are not necessarily
related. Type "System.Data.SqlClient.SqlException" is not associated with
"System.Data.SqlClient.dll" (which does not exist).

-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Jeff" wrote:
Thanks

Type t =
Type.GetType(min.test.Config.Settings.msgElement.P roviderType);
t gets a NULL value here. I've looked at the web.config settings and
the
code that creates the custom web.config settings and I cannot see what
I'm
doing wrong here... or maybe I'm becoming blind to my own programming
errors. Do you see why it get NULL value??

Jeff

"Peter Bromberg [C# MVP]" <pb*******@yahoo.yabbadabbadoo.comwrote in
message news:AC**********************************@microsof t.com...
Well,
The exception message doesn't lie - it's telling you that you've got
a
null
value, and it cannot accept it and continue.

One way to get at this is to "take apart" your compound statement so
that
it
will becom easier to set breakpoints and examine values, e.g.:

instance =
(DAL)Activator.CreateInstance(Type.GetType(min.tes t.Config.Settings.msgElement.ProviderType));

could be:

Type t =
Type.GetType(min.test.Config.Settings.msgElement.P roviderType);
// check to see if t above is null.

instance = (DAL)Activator.CreateInstance(t);

Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Jeff" wrote:

ASP.NET 2.0

This code crashes. It generate this error:
Value cannot be null.
Parameter name: type

I've created some custom web.config settings and this crash is
related to
accessing theme custom settings. This code is where the crash occur:
_instance =
(DAL)Activator.CreateInstance(Type.GetType(min.te st.Config.Settings.msgElement.ProviderType));
This code is trying to do is to create an instance of the DAL
provider,

************* custom web.config settings: **********************
<configSections>
<section name="msg" type="min.test.ConfigSection, __code"/>
</configSections>
<msg>
<messages providerType="min.test.DAL.SqlServer2005"/>
</msg>

********* In this code the crash occur *********
/* This code is from the DAL class, this code is trying to create an
instance of the DAL set in web.config - min.test.DAL.SqlServer2005
static public DAL Instance
{
get
{
if (_instance == null)
_instance =
(DAL)Activator.CreateInstance(Type.GetType(min.te st.Config.Settings.msgElement.ProviderType));
<--- code crashes here
return _instance;
}
}

******** definition of the custom settings in web.config
**********************
using System;
using System.Collections.Generic;
using System.Configuration;

namespace min.test
{
public class ConfigSection : ConfigurationSection
{
[ConfigurationProperty("messages", IsRequired = true)]
public MsgElement msgElement
{
get { return (MsgElement)base["messages"]; }
}

}

public class MsgElement : ConfigurationElement
{
[ConfigurationProperty("providerType", DefaultValue =
"min.test.DAL.SqlServer2005")]
public string ProviderType
{
get { return (string)base["providerType"]; }
set { base["providerType"] = value; }
}
}

}

Any suggestions??

Jeff


Apr 9 '07 #6

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

Similar topics

7
by: Jerry Krinock | last post by:
I've declared a class that has some std::vector data members like this: class MyClass { public: ... std::vector<Apples> apples ; ... private: ...
1
by: Matthew Wilson | last post by:
I need to write a function crc(msg, len) that gets a char array of length len and then calculates the crc32 for the code. I don't understand what's going wrong in the code I have. It goes...
11
by: Alfonso Morra | last post by:
Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure...
41
by: tech guru | last post by:
Hi, I am doing some mathematical analysis on large numbers in C. The same code runs perfectly in Windows but produces unpredictable results in Unix (like negative numbers or very very big...
2
by: coala | last post by:
My visual studio crashes when I attach to a process (i.e. Excel) so I cannot debug. Would anyone have any advice on what I can do/I am doing wrong please? I have installed all the latest...
3
by: RallyDSM | last post by:
Pre STory - I've had a lot of problems with this program, and I just added the last part of it (the add item code) and now an older part of the program crashes. Public Structure Stocks Public...
11
by: linq936 | last post by:
Hi, My program crashes on the following code, void lower(char* src, char* ret){ while ( src ) { char c = tolower(*src); *ret = c; <----------------------------- crash here ++ret; ++src; }
14
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
31
by: Tommy | last post by:
struct stat *stats IF_LINT (= 0); I don't know why "IF_LINT (= 0)" is needed ? Source is df.c of df program on Linux coreutils-5.2.1\coreutils-5.2.1\src Thanks!
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.