Calling ToolWindow.show() throws exception

已回答

This is my code:

protected fun showMyToolWindow() {
val currentProject = getActiveProject()
if (currentProject != null) {
SwingUtilities.invokeLater {

val task = RegisterToolWindowTask(
id = "MyWindow",
anchor = ToolWindowAnchor.RIGHT,
component = ComposePanel(),
sideTool = true,
icon = testRailImageIcon,
stripeTitle = { "My Window" },
)
val toolWindowManager = ToolWindowManager.getInstance(currentProject)
val toolWindowIds = toolWindowManager.toolWindowIds.toList()
if (!toolWindowIds.contains("MyWindow")) {
toolWindowManager.registerToolWindow(task)
}
val toolWindow = toolWindowManager.getToolWindow("MyWindow")
toolWindow?.show() // Throws NullPointerException <<<<<<<<
}
}
}

 

This is the Error:

2022-04-01 12:36:14,332 [ 346214]  ERROR - llij.ide.plugins.PluginManager - null 
java.lang.NullPointerException
    at java.desktop/java.awt.BorderLayout.preferredLayoutSize(BorderLayout.java:725)
    at java.desktop/java.awt.Container.preferredSize(Container.java:1823)
    at java.desktop/java.awt.Container.getPreferredSize(Container.java:1807)
    at java.desktop/javax.swing.JComponent.getPreferredSize(JComponent.java:1685)
    at com.intellij.ui.components.JBPanel.getPreferredSize(JBPanel.java:108)
    at java.desktop/java.awt.BorderLayout.preferredLayoutSize(BorderLayout.java:724)
    at java.desktop/java.awt.Container.preferredSize(Container.java:1823)
    at java.desktop/java.awt.Container.getPreferredSize(Container.java:1807)
    at java.desktop/javax.swing.JComponent.getPreferredSize(JComponent.java:1685)
    at com.intellij.openapi.actionSystem.impl.ActionToolbarImpl.actionsUpdated(ActionToolbarImpl.java:1252)
    at com.intellij.openapi.actionSystem.impl.ActionToolbarImpl.updateActionsImplFastTrack(ActionToolbarImpl.java:1208)
    at com.intellij.openapi.actionSystem.impl.ActionToolbarImpl.updateActionsImpl(ActionToolbarImpl.java:1176)
    at com.intellij.openapi.actionSystem.impl.ActionToolbarImpl$1.updateActionsImpl(ActionToolbarImpl.java:190)
    at com.intellij.openapi.actionSystem.impl.ToolbarUpdater$MyUpdateRunnable.run(ToolbarUpdater.java:152)
    at com.intellij.openapi.actionSystem.impl.ToolbarUpdater.updateActions(ToolbarUpdater.java:68)
    at com.intellij.openapi.actionSystem.impl.ActionToolbarImpl.updateActionsImmediately(ActionToolbarImpl.java:1155)
    at com.intellij.openapi.actionSystem.impl.ActionToolbarImpl.updateActionsImmediately(ActionToolbarImpl.java:1140)
    at com.intellij.openapi.actionSystem.impl.ActionToolbarImpl.addNotify(ActionToolbarImpl.java:240)
    at java.desktop/java.awt.Container.addNotify(Container.java:2800)
    at java.desktop/javax.swing.JComponent.addNotify(JComponent.java:4791)
    at com.intellij.openapi.wm.impl.ToolWindowHeader.addNotify(ToolWindowHeader.kt:171)
    at java.desktop/java.awt.Container.addNotify(Container.java:2800)
    at java.desktop/javax.swing.JComponent.addNotify(JComponent.java:4791)
    at java.desktop/java.awt.Container.addNotify(Container.java:2800)
    at java.desktop/javax.swing.JComponent.addNotify(JComponent.java:4791)
    at java.desktop/java.awt.Container.addNotify(Container.java:2800)
    at java.desktop/javax.swing.JComponent.addNotify(JComponent.java:4791)
    at com.intellij.openapi.wm.impl.InternalDecoratorImpl.addNotify(InternalDecoratorImpl.java:577)
    at java.desktop/java.awt.Container.addImpl(Container.java:1146)
    at java.desktop/java.awt.Container.add(Container.java:436)
    at com.intellij.openapi.ui.ThreeComponentsSplitter.doAddComponent(ThreeComponentsSplitter.java:543)
    at com.intellij.openapi.ui.ThreeComponentsSplitter.setLastComponent(ThreeComponentsSplitter.java:505)
    at com.intellij.openapi.wm.impl.ToolWindowsPane.setComponent(ToolWindowsPane.java:343)
    at com.intellij.openapi.wm.impl.ToolWindowsPane.addDecorator(ToolWindowsPane.java:252)
    at com.intellij.openapi.wm.impl.ToolWindowManagerImpl.doShowWindow(ToolWindowManagerImpl.kt:1029)
    at com.intellij.openapi.wm.impl.ToolWindowManagerImpl.showToolWindowImpl(ToolWindowManagerImpl.kt:982)
    at com.intellij.openapi.wm.impl.ToolWindowManagerImpl.showToolWindowImpl$default(ToolWindowManagerImpl.kt:971)
    at com.intellij.openapi.wm.impl.ToolWindowManagerImpl.showToolWindow(ToolWindowManagerImpl.kt:865)
    at com.jetbrains.rdserver.toolWindow.BackendServerToolWindowManager.showToolWindow(BackendServerToolWindowManager.kt:179)
    at com.intellij.openapi.wm.impl.ToolWindowImpl.show(ToolWindowImpl.kt:253)
  at com.intellij.openapi.wm.ToolWindow.show(ToolWindow.java:61)

 

It was working before but it's now throwing this error in the latest versions of Intellij Idea.

0

Hi,

I don't the reason for the NPE, but the API you used is internal (RegisterToolWindowTask, registerToolWindow()), and it often leads to unexpected errors in newer versions as internal API can change often without trying to be backward compatible.

Please reimplement your tool window to not use internal API. See: https://plugins.jetbrains.com/docs/intellij/tool-windows.html

0

Karol Lewandowski I didn't understand where you noticed that I'm using the internal APIs. I'm using the API from the link you posted which should work.

Looks like I can fix that error if I set a preferred side on the ComposePanel but now when the Window panel is opened it take minimum size instead of expanding to the width I set.

Will try to experiment a bit more.

0

Hi,

It became internal from version 2022.1, please take a look at:
https://github.com/JetBrains/intellij-community/blob/221/platform/platform-api/src/com/intellij/openapi/wm/RegisterToolWindowTask.kt#L11

The same is about registerToolWindow(). Please try to avoid using them directly. I can't see any places in the linked doc where these API is used directly.

0

Karol Lewandowski Maybe I'm missing something but if you look at https://plugins.jetbrains.com/docs/intellij/tool-windows.html#programmatic-setup  in section "Programmatic Setup" you can see it suggests using ToolWindowManager.registerToolWindow().

From https://upsource.jetbrains.com/idea-ce/file/idea-ce-6a856f22b82f6478cd6fbdc0f281c28d5c75b712/platform/platform-api/src/com/intellij/openapi/wm/ToolWindowManager.kt?_ga=2.26599359.303402651.1649052220-380347726.1617666970  you can see that the only function which is not deprecated takes a RegisterToolWindowTask.

I don't want to declare it in the xml. I want to add the Tool window programmatically when the user clicks on something in the editor.

Maybe you could advice on this or update the guide.

 

0

Hi,

I'm sorry, but I missed this somehow... You are right, it is part of the doc, and if you target versions earlier than 2022.1, then you can use it.

I don't understand the general exception cause in your code, but I suggest taking a look at the existing implementations in the platform code. Maybe your ComposePanel is missing some common parts. Check:

If the problem persists I think it might be worth creating an issue at https://youtrack.jetbrains.com/issues/IDEA. 

We will update the docs to mention the new API, thank you for pointing this out.

0

请先登录再写评论。