Skip to content

Conversation

Copy link

Copilot AI commented Jul 2, 2025

The app was crashing with a NullReferenceException when requesting messages from the bot on both Android and Windows platforms. The error occurred in UserMessageItemTemplate.BorderText_SizeChanged at line 27 when trying to access _parentGrid.Width.

Root Cause:
The _parentGrid field is only initialized in the ParentGrid_SizeChanged event handler, but BorderText_SizeChanged can be triggered before this event fires, leaving _parentGrid null when accessed.

Solution:
Added defensive null checks in both UserMessageItemTemplate and BotMessageItemTemplate before accessing _parentGrid.Width:

private void BorderText_SizeChanged(object sender, EventArgs e)
{
    var borderText = (Border)sender;

    // Prevent NullReferenceException if _parentGrid hasn't been initialized yet
    if (_parentGrid == null)
        return;

    var widthMaxBorderText = _parentGrid.Width - 45;
    // ... rest of method unchanged
}

Testing:

  • Created validation tests confirming the fix prevents the NullReferenceException
  • Verified the method continues to work correctly when _parentGrid is properly initialized
  • Changes are minimal and surgical (4 lines added per template)

This fix ensures the app no longer crashes when users send messages to the bot while maintaining all existing functionality.

Fixes #6.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: danielmonettelli <14121125+danielmonettelli@users.noreply.github.com>
Copilot AI changed the title [WIP] Issue requesting a message to bot! Fix NullReferenceException in BorderText_SizeChanged for message templates Jul 2, 2025
Copilot AI requested a review from danielmonettelli July 2, 2025 08:39
Copilot finished work on behalf of danielmonettelli July 2, 2025 08:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Issue requesting a message to bot!

2 participants