RFR: 6286501: JTabbedPane throws NPE from its stateChanged listener in particular case

Prasanta Sadhukhan psadhukhan at openjdk.org
Thu Sep 15 10:02:37 UTC 2022


On Thu, 15 Sep 2022 06:01:37 GMT, Jayathirth D V <jdv at openjdk.org> wrote:

>> JTabbedPane throws NPE from its stateChanged listener as it tries to access uninstantiated TabbedPane.
>> Fix is to use the tabPane passed to stateChangeListener so that correct tabPane is accessed.
>
> src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTabbedPaneUI.java line 4102:
> 
>> 4100:             setFocusIndex(tabPane.getSelectedIndex(), false);
>> 4101: 
>> 4102:             if (tabPane.getLayout() instanceof TabbedPaneScrollLayout) {
> 
> I see BasicTabbedPaneUI.tabPane object getting used in other listeners in this Handler class. Is there a reason why only when we hit stateChanged() function we are seeing NPE?
> 
> Is this NPE for tabPane object itself or Layout not being initialized?

Basically, when a tab index is selected via `tbp.setSelectedIndex()` in testcase, it fires a state change event (via `DefaultSingleSelectionModel#setSelectedIndex`) which is caught by the application and in this case, state change listener in the testcase calls `JTabbedPane.updateUI() `which calls `JComponent.setUI() `which first uninstalls UI (which made `tabPane null` in `BasicTabbedPaneUI#uninstallUI`) and then install new UI instance but when it goes to JDK's stateChange listener `BasicTabbedPaneUI.stateChanged()` it passes the new instance but scrollableTabLayoutEnabled() still uses old UI instance in which tabPane is already made null so NPE happens, so it is better to use the tabPane instance passed to UI's stateChange listener..

-------------

PR: https://git.openjdk.org/jdk/pull/10216



More information about the client-libs-dev mailing list