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.

LSP/TypeScript: NPE when trying to generate getter

See original GitHub issue

Apache NetBeans version

Apache NetBeans 14 RC 1

What happened

When I try to generate a getter for a class field (and, optionally, also a setter), NetBeans shows the following error in the Notifications window:

Cannot invoke "org.eclipse.lsp4j.WorkspaceEdit.getDocumentChanges()" because "edit" is null

This is the stack trace:

java.lang.NullPointerException: Cannot invoke "org.eclipse.lsp4j.WorkspaceEdit.getDocumentChanges()" because "edit" is null
	at org.netbeans.modules.lsp.client.Utils.applyWorkspaceEdit(Utils.java:94)
	at org.netbeans.modules.lsp.client.Utils.applyCodeAction(Utils.java:198)
	at org.netbeans.modules.lsp.client.bindings.LanguageClientImpl$DiagnosticFixList$CommandBasedFix.implement(LanguageClientImpl.java:299)
[catch] at org.netbeans.modules.editor.hints.HintsUI$1.run(HintsUI.java:810)
	at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:1418)
	at org.netbeans.modules.openide.util.GlobalLookup.execute(GlobalLookup.java:45)
	at org.openide.util.lookup.Lookups.executeWith(Lookups.java:278)
	at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:2033)

How to reproduce

Suppose that I’ve the following TypeScript source file:

export class Student {
	private age: number;
}

When I open it in NetBeans, the editor shows a compilation error saying this: ‘age’ is declared but its value is never read. Then, if I do one of these things:

  • Option A
  1. Main menu
  2. Source
  3. Fix code…
  4. Generate ‘get’ and ‘set’ accessors
  • Option B
  1. Click on bulb in editor’s gutter
  2. Generate ‘get’ and ‘set’ accessors

The error mentioned in the previous section is thrown.

Did this work correctly in an earlier version?

No

Operating System

Debian GNU/Linux 11.3 (Bullseye)

JDK

OpenJDK Runtime Environment (build 17.0.2+8-Debian-1deb11u1)

Apache NetBeans packaging

Apache NetBeans binary zip

Anything else

In NetBeans IDE 13, the same error was thrown when adding an import automatically, by choosing Add import from <package> in the bulb’s menu. However, in version 14 RC 1 this seems to be fixed.

In NetBeans IDE 14 RC1, this error also occurs when you select a piece of code and try to extract it into a method (or other kind of refactoring). In this case, we need to choose Insert code… instead of Fix code…, but the result is the same.

Are you willing to submit a pull request?

No

Code of Conduct

Yes

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
matthiasblaesingcommented, Apr 28, 2022

Ok - I pushed a PR, that holds a partial fix: https://github.com/apache/netbeans/pull/4049

Here is a development build from that branch:

https://doppel-helix.eu/NetBeans-dev-dev-de5beb43947a575c5c77eafca7945dda59dff8d0-release.zip

It does not handle the “_typescript.rename” request, but it turns the exception into a log entry with payload, which makes this a usable case in my eyes. In the long run we most probably need to enable language specific plugins to handle these cases, but that is a problem not to be fixed directly before a release.

1reaction
matthiasblaesingcommented, Apr 28, 2022

Thank you - the fix for the exception is trivial - but the it becomes more interesting:

# This patch file was generated by NetBeans IDE
# It uses platform neutral UTF-8 encoding and \n newlines.
--- a/ide/lsp.client/src/org/netbeans/modules/lsp/client/Utils.java
+++ b/ide/lsp.client/src/org/netbeans/modules/lsp/client/Utils.java
@@ -195,7 +195,9 @@
             if (cmd.isLeft()) {
                 command = cmd.getLeft();
             } else {
-                Utils.applyWorkspaceEdit(cmd.getRight().getEdit());
+                if(cmd.getRight().getEdit() != null) {
+                    Utils.applyWorkspaceEdit(cmd.getRight().getEdit());
+                }
                 command = cmd.getRight().getCommand();
             }
             if (command != null) {

But then it becomes more interesting: the typescript server sends a request to the IDE _typescript.rename. The IDE logs that as unsupported and the typescript server then sends an error notification, that the IDE does not support the request. This needs more work and maybe tie in from the typescript plugin in NetBeans itself.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - How to trace a NullPointerException in a chain of getters
This technique is only useful if your tests identify the source of every possible NPE. In production you cannot run your code in...
Read more >
Generate, rename and delete getters/setters instantly in Eclipse
To generate getters and setters, do the following: Create the fields you want in the class then press Alt+Shift+S, R. A dialog will...
Read more >
Avoiding NullPointerException - DZone Java
First of all, to make it clear, we only changed the return type of the getter. The setter and the field type kept...
Read more >
Generate getters and setters in Eclipse IDE - Mkyong.com
Right click on the file, select “Source” –> “Generate Getters and Setters…” ... Choose which field you want to generate, and click on...
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