Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Fixed NullReferenceException when renaming files #14350

Merged
merged 2 commits into from
Jan 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/Files.App/Views/Layouts/GridLayoutPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,20 +253,27 @@ protected override void EndRename(TextBox textBox)
{
Popup? popup = gridViewItem.FindDescendant("EditPopup") as Popup;
TextBlock? textBlock = gridViewItem.FindDescendant("ItemName") as TextBlock;
popup!.IsOpen = false;
textBlock!.Opacity = (textBlock.DataContext as ListedItem)!.Opacity;

if (popup is not null)
popup.IsOpen = false;

if (textBlock is not null)
textBlock.Opacity = (textBlock.DataContext as ListedItem)!.Opacity;
}
else if (FolderSettings.LayoutMode == FolderLayoutModes.TilesView)
{
TextBlock? textBlock = gridViewItem.FindDescendant("ItemName") as TextBlock;

textBox.Visibility = Visibility.Collapsed;
textBlock!.Visibility = Visibility.Visible;

if (textBlock is not null)
textBlock.Visibility = Visibility.Visible;
}

// Unsubscribe from events
if (textBox is not null)
{
textBox!.LostFocus -= RenameTextBox_LostFocus;
textBox.LostFocus -= RenameTextBox_LostFocus;
textBox.KeyDown -= RenameTextBox_KeyDown;
}

Expand Down Expand Up @@ -430,15 +437,17 @@ private async void FileList_ItemTapped(object sender, TappedRoutedEventArgs e)
if (FolderSettings.LayoutMode == FolderLayoutModes.GridView)
{
Popup popup = gridViewItem.FindDescendant("EditPopup") as Popup;
var textBox = popup.Child as TextBox;
var textBox = popup?.Child as TextBox;

await CommitRenameAsync(textBox);
if (textBox is not null)
await CommitRenameAsync(textBox);
Comment on lines +442 to +443
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this makes sense because GridLayoutPage.EndRename doesn't appear to throw a NullReferenceException even if textBox is null.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think is causing the exception?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we need to consider the possibility of popup and textBlock being null.

else if (FolderSettings.LayoutMode == FolderLayoutModes.GridView)
{
Popup? popup = gridViewItem.FindDescendant("EditPopup") as Popup;
TextBlock? textBlock = gridViewItem.FindDescendant("ItemName") as TextBlock;
popup!.IsOpen = false;
textBlock!.Opacity = (textBlock.DataContext as ListedItem)!.Opacity;
}
else if (FolderSettings.LayoutMode == FolderLayoutModes.TilesView)
{
TextBlock? textBlock = gridViewItem.FindDescendant("ItemName") as TextBlock;
textBox.Visibility = Visibility.Collapsed;
textBlock!.Visibility = Visibility.Visible;
}

}
else
{
var textBox = gridViewItem.FindDescendant("TileViewTextBoxItemName") as TextBox;

await CommitRenameAsync(textBox);
if (textBox is not null)
await CommitRenameAsync(textBox);
}
}
}
Expand Down