Null Reference Exception [duplicate]

有些话、适合烂在心里 提交于 2019-12-13 03:59:37

问题


This code generates and Null Reference exception. Exception comes at the line where the parameter array is initialized. What can be the problem? I don't know howto follow the stack-trace and work any logic over it. thanks in advance.

DAL dal = new DAL();

    string SQL = @"INSERT INTO Assets ([AssetName],[AssetType],[Model],[Description],
                                                          [PurchaseValue],[SalvageValue],[Currency],[DateAcquired,[DateRetire],[ImagePath],
                                                          [InUse])
                                                          VALUES (?,?,?,?,?,?,?,?,?,?,?)";

    OleDbParameter[] par = new OleDbParameter[]{ 
    new OleDbParameter("@assetname",name.Text),
    new OleDbParameter("@assettype",assettypes.SelectedValue.ToString()),
    new OleDbParameter("@model",model.Text),
    new OleDbParameter("@description",description.Text),
    new OleDbParameter("@purchasevalue",purchaseval.Value),
    new OleDbParameter("@salvagevalue",salvageval.Value),
    new OleDbParameter("@currency",currencies.SelectedIndex),
    new OleDbParameter("@dateacquired",purchasedate.Value),
    new OleDbParameter("@dateretire",purchasedate.Value.AddYears((int)lifetime.Value)),
    new OleDbParameter("@imagepath","N/A"),
    new OleDbParameter("@addedby",MDIParent1.User.ID)
    };

回答1:


You probably have one or more nullable types (perhaps your dates?) that don't have values, although it could also be that there is no SelectedValue. Check that all of your parameters are non-null before the statement in the debugger to see which.




回答2:


It seems most likely that one of the objects you are querying is null.

I suggest printing them out to standard output.




回答3:


One of these lines contains a null reference:

new OleDbParameter("@assetname",name.Text),
new OleDbParameter("@assettype",assettypes.SelectedValue.ToString()),
new OleDbParameter("@model",model.Text),
new OleDbParameter("@description",description.Text),
new OleDbParameter("@purchasevalue",purchaseval.Value),
new OleDbParameter("@salvagevalue",salvageval.Value),
new OleDbParameter("@currency",currencies.SelectedIndex),
new OleDbParameter("@dateacquired",purchasedate.Value),
new OleDbParameter("@dateretire",purchasedate.Value.AddYears((int)lifetime.Value)),
new OleDbParameter("@imagepath","N/A"),
new OleDbParameter("@addedby",MDIParent1.User.ID)

Look at the propery values in the debugger, there's really nothing we can do with the code you have provided.




回答4:


Really it looks like you are taking input directly from the user without validating or scrubbing it first. My advice, and this will probably solve your NRE, is to validate each user entered value before sending it to the database. This will allow you to catch any errors in input before you pig up the db with a bad query.

And please, dont name variables the same as the objects. I mean

DAL dal = new DAL(); 

at least underscore the local vars or use a different name.

DAL _dal = new DAL();
DAL dataAccessLayer = new DAL();


来源:https://stackoverflow.com/questions/452613/null-reference-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!