简体   繁体   English

使用Linq Xamarin时出现System.NullReferenceException

[英]System.NullReferenceException in while using Linq Xamarin

Solved 解决了

I have a SQLite Table created from a class named Strategy , which has two properties String StrategyCode and String StrategyID . 我有一个从名为Strategy创建的SQLite Table,它具有两个属性String StrategyCodeString StrategyID I am Binding the StrategyCode Column to a Spinner with this code. 我使用此代码将StrategyCode列绑定到Spinner。

private void BindList()
{
    try
    {
        var list = connection.Query<Strategy>("SELECT StrategyCode FROM Strategy");
        BindStrategy = list.ConvertAll(x => Convert.ToString(x));
        var StratAdpater = new ArrayAdapter<System.String>(this, Resource.Layout.spinner_item, BindStrategy);
        StratAdpater.SetDropDownViewResource(Resource.Layout.spinner_item);
        spinner2.Adapter = StratAdpater;       
    }
    catch(Exception e)
    {
        Toast.MakeText(this, e.ToString(), ToastLength.Long).Show();
    }
}  

I have a textview right below this Spinner, Where, i want to show the StrategyID of the corresponding StrategyCode selected from the Spinner. 我有权低于这个微调,在哪里,我想展现一个TextView StrategyID从微调选择相应StrategyCode的。 This is How I am doing it. 这就是我的做法。

try
 {
   var selectedstrat = spinner2.SelectedItem.ToString();
   var userselection = (from a in connection.Table<Strategy>()
                                     where a.StrategyCode == selectedstrat
                                     select a);            
    strategyID.Text = userselection.FirstOrDefault().StrategyID;
 }
catch (Exception e)
{
    Toast.MakeText(this, e.ToString(), ToastLength.Long).Show();
}    

But i am getting a System.NullReferenceException - (Object Reference not Set to Instance of an Object) and the StrategyID is not getting set onto the Textview. 但是我正在获取System.NullReferenceException-(对象引用未设置为对象的实例),并且StrategyID尚未设置为Textview。 But, When i HardCode the StrategyCode value For Example: 但是,当我使用HardCode表示StrategyCode值时,例如:

var userselection = (from a in connection.Table<Strategy>()
                                         where a.StrategyCode == "Strategy1"
                                         select a);

Then the TextView Shows the ID of the Hardcoded Strategy. 然后,TextView显示硬编码策略的ID。 What is Possibly Going Wrong Here? 这里可能出什么问题了? I tried this 我试过了

 var userselection = (from a in connection.Table<Strategy>()
                                         where a.StrategyCode == selectedstrat
                                         select a.StrageyID).FirstOrDefault();

                    StrategyID.Text = userselection.ToString();

But Still i am not able to get the StrategyID on the TextView. 但是我仍然无法在TextView上获取StrategyID。

Let's have a look at this line: 让我们看一下这一行:

var userselection = (from a in connection.Table<Strategy>() where a.StrategyCode == selectedstrat select a.StrageyID).FirstOrDefault();

What if userselection is null ? 如果userselectionnull怎么办?

StrategyID.Text = userselection.ToString(); ^^^^^^^^^^^^^ <- null => throws

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM