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

System.NullReferenceException #4

Closed
Seekeer opened this issue May 4, 2023 · 5 comments
Closed

System.NullReferenceException #4

Seekeer opened this issue May 4, 2023 · 5 comments

Comments

@Seekeer
Copy link

Seekeer commented May 4, 2023

Got code

            if(!string.IsNullOrEmpty(imagePath))
            {
                var bitmap = new Bitmap(imagePath);

                Clowd.Clipboard.ClipboardAvalonia.SetImage(bitmap);

                builder.KeyDown(Keys.Control).SendKeys("v").KeyUp(Keys.Control).Build().Perform();
            }

            Clowd.Clipboard.ClipboardAvalonia.SetText(text);
            builder.KeyDown(Keys.Control).SendKeys("v").KeyUp(Keys.Control).Build().Perform();

It is working at first. But on second or N-th call I got System.NullReferenceException when trying to access Clowd.Clipboard.ClipboardAvalonia

@caesay
Copy link
Member

caesay commented May 5, 2023

Can you share the stacktrace please? And could you also share some more of the surrounding code?

@Seekeer
Copy link
Author

Seekeer commented May 6, 2023

image

Your clipboard is using in class which is working with chromedriver to paste image and text in some input field.

    protected virtual void SendKeys(IWebElement input, string text, string imagePath)
    {
        try
        {
            if (string.IsNullOrEmpty(text))
                return;

            input.Click();
            Actions builder = new Actions(driver);

            if (!string.IsNullOrEmpty(imagePath))
            {
                var bitmap = new Bitmap(imagePath);
                Clowd.Clipboard.ClipboardAvalonia.SetImage(bitmap);

                builder.KeyDown(Keys.Control).SendKeys("v").KeyUp(Keys.Control).Build().Perform();
            }

            Clowd.Clipboard.ClipboardAvalonia.SetText(text);
            builder.KeyDown(Keys.Control).SendKeys("v").KeyUp(Keys.Control).Build().Perform();

        }
        catch (Exception ex)
        {

        }
    }

@caesay
Copy link
Member

caesay commented May 6, 2023

What's happening here is that the clipboard was unable to be opened - probably because some other process or thread had the clipboard open.

Can see here where we try to call OpenClipboard in a loop, if we are unable to open it within 1000ms then it will throw an exception.

This is not a bug in Clowd.Clipboard, however I am going to leave this issue open for now, as that error/exeption message is incorrect and it should be a more helpful message.

@Seekeer
Copy link
Author

Seekeer commented May 6, 2023

What's happening here is that the clipboard was unable to be opened - probably because some other process or thread had the clipboard open.
Strange. When Im using standart Avalonia Clipboard I dont have such problems. Any ideas where I could seek for reason?

@caesay
Copy link
Member

caesay commented Dec 23, 2024

I have improved the error handling

@caesay caesay closed this as completed Dec 23, 2024
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

No branches or pull requests

2 participants