question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NullReferenceException in Page.AddConsoleMessageAsync

See original GitHub issue

The exception is thrown when I call Page.WaitForSelectorAsync, but it looks like the actual issue is that something is being logged to the console that PuppeteerSharp can’t handle correctly:

PuppeteerSharp.TargetClosedException: 'Protocol error(Runtime.callFunctionOn): Target closed. (Page failed to process Runtime.consoleAPICalled. Object reference not set to an instance of an object..    at PuppeteerSharp.Page.<>c.<AddConsoleMessageAsync>b__262_1(IJSHandle i) in C:\projects\puppeteer-sharp\lib\PuppeteerSharp\Page.cs:line 1672
   at System.Linq.Enumerable.SelectArrayIterator`2.MoveNext()
   at System.String.JoinCore[T](ReadOnlySpan`1 separator, IEnumerable`1 values)
   at System.String.Join[T](String separator, IEnumerable`1 values)
   at PuppeteerSharp.Page.AddConsoleMessageAsync(ConsoleType type, IJSHandle[] values, StackTrace stackTrace) in C:\projects\puppeteer-sharp\lib\PuppeteerSharp\Page.cs:line 1685
   at PuppeteerSharp.Page.Client_MessageReceived(Object sender, MessageEventArgs e) in C:\projects\puppeteer-sharp\lib\PuppeteerSharp\Page.cs:line 1471)'

This is reproducible on both PuppeteerSharp v8 and v9, running on .NET 7. Not sure I can share a repro case since the page I’m accessing requires authentication. When I look at the console for the page in question, I see some uncaught TypeErrors, e.g.:

image

Issue Analytics

  • State:closed
  • Created 8 months ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
amaitlandcommented, Jan 17, 2023

@kblok The JTokenType.Object check added in #2025, was there a reason to check the JToken type rather than RemoteObjectType.Object?

Calling console.debug(null); results in an exception with a callstack similar to this one.

[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldNotFailForNullArgument()
{
	var consoleTcs = new TaskCompletionSource<string>();

	void EventHandler(object sender, ConsoleEventArgs e)
	{
		consoleTcs.TrySetResult(e.Message.Text);
		Page.Console -= EventHandler;
	}

	Page.Console += EventHandler;

	await Task.WhenAll(
		consoleTcs.Task,
		Page.EvaluateExpressionAsync("console.debug(null);")
	);

	Assert.Equal("JSHandle:null", await consoleTcs.Task);
}

Checking RemoteObject.Type == RemoteObjectType.Object resolves the exception. Argubily passing null to console.debug is pointless. Nonetheless it shouldn’t crash.,

0reactions
danportscommented, Jan 24, 2023

9.0.1 looks good, thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

NullReferenceException on page initialization if I use ...
Your page is trying to render @pageGlobal.Title , before it's even got it from the async method. That's why if you do pageGlobal...
Read more >
puppeteer-sharp/lib/PuppeteerSharp/Page.cs at master
Url, AddConsoleMessageAsync, HandleException); _workers[sessionId] = worker; WorkerCreated?.Invoke(this, new WorkerEventArgs(worker)); } if (target.
Read more >
Null Reference Exception in Blazor Router - Matthew Champion
So this is a bit speculative, but I believe that the NullReferenceException is thrown in circumstances where the router is unable to correctly ......
Read more >
Object reference not set to an instance of an object | Page 2
A null reference means that it is trying to access something that doesn't exist. You either forgot to drag something in the editor,...
Read more >
NullReferenceException Class (System)
The exception that is thrown when there is an attempt to dereference a null object reference.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found