Click here to Skip to main content
15,891,655 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#
protected override void Append(LoggingEvent loggingEvent)
{
    if (loggingEvent.Level.Name == "INFO")
    {
        FIXOUTUI ui = FIXOUTUI.GetInstance();
        Action action = () =>
        {
            ui.Listbox1.Items.Add(loggingEvent.TimeStamp.ToString("HH:mm:ss") + " " + loggingEvent.RenderedMessage);

            ui.Listbox1.SelectedItem = ui.Listbox1.Items[ui.Listbox1.Items.Count - 1];
            ui.Listbox1.UpdateLayout();

            var listBoxItem = (ListBoxItem)ui.Listbox1
                .ItemContainerGenerator
                .ContainerFromItem(ui.Listbox1.SelectedItem);
            listBoxItem.Focus(); this line throw an error
        };
        ui.Dispatcher.BeginInvoke(action);
    }
}

Quote:
ERROR: Unhandled Exception System.NullReferenceException:
Object reference not set to an instance of an object.
at MyApp.Delogs.<>c__DisplayClass3.b__0()
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
Posted
Updated 9-May-14 5:58am
v2
Comments
CHill60 9-May-14 11:59am    
Put a break point on that line. When you get there check the values of ui.Dispatcher and action... you'll probably find the latter is Null
Matt T Heffron 9-May-14 12:30pm    
Actually, action is the LEAST LIKELY thing to be null, given that it is assigned in the immediately preceding statement.
CHill60 9-May-14 12:42pm    
Only if that statement worked though
Matt T Heffron 9-May-14 12:47pm    
That statement is just the assignment of the lambda-form delegate.
It won't return null if it fails.
It would fail "spectacularly!" (i.e., throw an exception)
In fact, an optimizing compiler would remove the assignment altogether and just pass the reference to the delegate in the .BeginInvoke() directly.
CHill60 9-May-14 12:54pm    
Ah of course. My fault for looking at it properly! Thanks

1 solution

It looks like your ListBox (ListBox1) is not pouplated with data . debug your code .
 
Share this answer
 

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