NB17: javax.swing.text.BadLocationException: Invalid offset=-1 < 0; docLen=1178
- Dominant language
- Java
- Stars
- 3.1k
- Forks
- 935
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 17
Description
### Apache NetBeans version
Apache NetBeans 17
### What happened
Opened the following file below which is part of a larger project using Lombok and I get the error below.
Also, the `package` line is highlighted and the hint says "Convert to switch statement" which is incorrect.
Class:
```
package com.rajant.logger.ui.event;
import java.time.OffsetDateTime;
import java.util.List;
import lombok.Builder;
import lombok.NonNull;
import lombok.Singular;
import lombok.Value;
/**
*
* Class used to contain the fields we want to log for any UI event
*
* @author tmulle
*/
@Builder
@Value
public class EventLogData {
/**
* Holds the date/time of the event in ISO 8601 format
*/
private final OffsetDateTime date = OffsetDateTime.now();
/**
* Required category (ie. TOPOVIEW_OPENED, BREADCRUMB_TABLE_OPENED..)
*
* This can be any text but something useful for group like events should
* be used
*/
@NonNull
private String category;
/**
* Optional list of parameters used when the event was executed
*/
@Singular
private List arguments;
/**
* Optional message describing the event
*/
private String message;
/**
* Represents the Key=VALUE pair for the arguments variable
*/
@Builder
@Value
public static class KeyValue {
@NonNull
String key;
@NonNull
Object value;
}
}
```
Error:
```
javax.swing.text.BadLocationException: Invalid offset=-1 < 0; docLen=1178
at org.netbeans.modules.editor.lib2.document.EditorDocumentContent.checkOffsetNonNegative(EditorDocumentContent.java:246)
at org.netbeans.modules.editor.lib2.document.EditorDocumentContent.checkOffsetInContent(EditorDocumentContent.java:259)
at org.netbeans.modules.editor.lib2.document.EditorDocumentContent.createPosition(EditorDocumentContent.java:173)
at java.desktop/javax.swing.text.AbstractDocument.createPosition(AbstractDocument.java:861)
at org.netbeans.modules.editor.lib2.view.OffsetRegion.createPos(OffsetRegion.java:58)
Caused: java.lang.IllegalArgumentException: Invalid offset=-1 in doc: org.netbeans.modules.editor.NbEditorDocument@35666266, mimeType='text/x-java', kitClass=null, length=1178, version=1, file=org.netbeans.modules.java.JavaDataObject@2277fca7[/Users/tmulle/Development/Rajant/jv11/lib/ui-event-logger/src/main/java/com/rajant/logger/ui/event/EventLogData.java@2495ae28:5b423f47]
at org.netbeans.modules.editor.lib2.view.OffsetRegion.createPos(OffsetRegion.java:60)
at org.netbeans.modules.editor.lib2.view.OffsetRegion.create(OffsetRegion.java:36)
at org.netbeans.modules.editor.lib2.view.OffsetRegion.union(OffsetRegion.java:49)
at org.netbeans.modules.editor.lib2.view.ViewUpdates.viewFactoryChanged(ViewUpdates.java:356)
at jdk.internal.reflect.GeneratedMethodAccessor174.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.openide.util.WeakListenerImpl$ProxyListener.invoke(WeakListenerImpl.java:462)
at jdk.proxy17/jdk.proxy17.$Proxy56.viewFactoryChanged(Unknown Source)
at org.netbeans.modules.editor.lib2.view.EditorViewFactory.fireEvent(EditorViewFactory.java:235)
at org.netbeans.modules.editor.lib2.view.HighlightsViewFactory$1.run(HighlightsViewFactory.java:373)
at org.netbeans.editor.BaseDocument.render(BaseDocument.java:1407)
at org.netbeans.modules.editor.lib2.view.HighlightsViewFactory.highlightChanged(HighlightsViewFactory.java:347)
at jdk.internal.reflect.GeneratedMethodAccessor128.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.openide.util.WeakListenerImpl$ProxyListener.invoke(WeakListenerImpl.java:462)
at jdk.proxy17/jdk.proxy17.$Proxy54.highlightChanged(Unknown Source)
at org.netbeans.modules.editor.lib2.highlighting.DirectMergeContainer.highlightChanged(DirectMergeContainer.java:120)
at jdk.internal.reflect.GeneratedMethodAccessor128.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.openide.util.WeakListenerImpl$ProxyListener.invoke(WeakListenerImpl.java:462)
at jdk.proxy17/jdk.proxy17.$Proxy54.highlightChanged(Unknown Source)
at org.netbeans.spi.editor.highlighting.support.AbstractHighlightsContainer.fireHighlightsChange(AbstractHighlightsContainer.java:88)
at org.netbeans.spi.editor.highlighting.support.OffsetsBag.setHighlights(OffsetsBag.java:282)
at org.netbeans.modules.java.editor.semantic.SemanticHighlighter$1$1.run(SemanticHighlighter.java:79)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:771)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:741)
at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:136)
[catch] at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
```

### How to reproduce
Not sure, but perhaps it's Lombok again causing parsing issues?
### Did this work correctly in an earlier version?
No / Don't know
### Operating System
MacOS
### JDK
JDK17
### Apache NetBeans packaging
Apache NetBeans binary zip
### Anything else
Anytime I open the file in the editor.
### Are you willing to submit a pull request?
Yes
Contributor guide
Research direction
Reproduce the error by opening the supplied EventLogData.java file in the NetBeans editor. Start with the stack entries in SemanticHighlighter, HighlightsViewFactory, ViewUpdates, and OffsetRegion, and trace how the invalid offset reaches document position creation. Done means the file opens without the BadLocationException and the incorrect switch-statement hint is no longer shown.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- developer-experience
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100