System.ArgumentNullException

时间:2012-10-19 22:38:18

标签: c#

我得到了这个例外......

System.ArgumentNullException: Value cannot be null.
Parameter name: typeName
   at HomeInventory2.Services.Factory.GetService(String servicename) in C:\Users\Protego\Documents\Visual Studio 2010\Projects\HomeInventory2\HomeInventory2\Services\Factory.cs:line 37
   at HomeInventory2.Business.Manager.GetService(String name) in C:\Users\Protego\Documents\Visual Studio 2010\Projects\HomeInventory2\HomeInventory2\Business\Manager.cs:line 14
   at HomeInventory2.Business.InventoryMngr.Create(CreateInventory inv) in C:\Users\Protego\Documents\Visual Studio 2010\Projects\HomeInventory2\HomeInventory2\Business\InventoryMngr.cs:line 19
   at HomeInventory2.Form1.submitButton_Click(Object sender, EventArgs e) in C:\Users\Protego\documents\visual studio 2010\Projects\HomeInventory2\HomeInventory2\Form1.cs:line 52
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

源自此代码 -

private void submitButton_Click(object sender, EventArgs e)
        {
            CreateInventory create = new CreateInventory();
            create.ItemAmount = textBoxAmount.Text;
            create.ItemCategory = textBoxCategories.Text;
            create.ItemProperties = textBoxValue.Text;
            create.ItemValue = textBoxValue.Text;

            InventoryMngr invtryMngr = new InventoryMngr();
            invtryMngr.Create(create);

        }

该代码应以创建的文件结束,并将这些值保存到文本文件中。显然有些事情正在沿着这条线发生 - 我假设在InventoryMngr文件中看起来像这样。

namespace HomeInventory2.Business
{
    public class InventoryMngr : Manager
    {
        /// <summary>
        /// persists the inventory information
        /// </summary>
        /// <param name="inv"></param>
        public void Create(CreateInventory inv)
        {
            InventorySvc inventorySvc =
            (InventorySvc)GetService(typeof(InventorySvc).Name);
            inventorySvc.CreateInventory(inv);
        }
    }
}

业务经理获得服务

namespace HomeInventory2.Business
{
    public abstract class Manager
    {
        private Factory factory = Factory.GetInstance();
        protected IService GetService(String name)
        {
            return factory.GetService(name);
        }
    }
}

和工厂getservice

public IService GetService(string servicename)
{
    Type type;
    Object obj = null;
    try
    {
        type = Type.GetType(GetImplName(servicename));
        obj = Activator.CreateInstance(type);
    }
    catch (Exception e)
    {
        Console.WriteLine("Exception occured: {0}", e);
        throw e;
    }
    return (IService)obj;
}

GetImplName

private string GetImplName(string servicename)
        {
            NameValueCollection settings =
                ConfigurationManager.AppSettings;
            return settings.Get(servicename);
        }

2 个答案:

答案 0 :(得分:2)

这是你的问题:

NameValueCollection settings = ConfigurationManager.AppSettings;
return settings.Get(servicename);

确保您的app.config / web.config具有正确的AppSettings值。 settings.Get(servicename)找不到servicename的密钥。

因此,如果servicename是“AwesomeService”,则需要appSettings中的元素,如:

<add key="AwesomeService" value="somevalue"/>

答案 1 :(得分:-1)

我修改你的代码如下:

public void Create(CreateInventory inv)
{
    InventorySvc inventorySvc =
        (InventorySvc)GetService((typeof(InventorySvc)).toString());
    inventorySvc.CreateInventory(inv);
}