exception - java.lang.ArrayIndexOutOfBoundsException using highlighter -


i'm trying selected text jtextfield. found .gethighlighter() function , works. desired text keeps throwing , index out of bounds exception. can tell me why?

ps: i've tried checking if h != null , h[0] != null. same result.

private void jtextfield1mousedragged(java.awt.event.mouseevent evt) {                                              highlight[] h = jtextfield1.gethighlighter().gethighlights();     jlabel2.settext("selected text: " + jtextfield1.gettext().substring(h[0].getstartoffset(), h[0].getendoffset())); } 

exception:

exception in thread "awt-eventqueue-0" java.lang.arrayindexoutofboundsexception: 0 @ interfacesprueba1.ej3b.jtextfield1mousedragged(ej3b.java:110) @ interfacesprueba1.ej3b.access$000(ej3b.java:14) @ interfacesprueba1.ej3b$1.mousedragged(ej3b.java:42) @ java.awt.awteventmulticaster.mousedragged(awteventmulticaster.java:320) @ java.awt.component.processmousemotionevent(component.java:6583) @ javax.swing.jcomponent.processmousemotionevent(jcomponent.java:3342) @ java.awt.component.processevent(component.java:6304) @ java.awt.container.processevent(container.java:2236) @ java.awt.component.dispatcheventimpl(component.java:4891) @ java.awt.container.dispatcheventimpl(container.java:2294) @ java.awt.component.dispatchevent(component.java:4713) @ java.awt.lightweightdispatcher.retargetmouseevent(container.java:4888) @ java.awt.lightweightdispatcher.processmouseevent(container.java:4542) @ java.awt.lightweightdispatcher.dispatchevent(container.java:4466) @ java.awt.container.dispatcheventimpl(container.java:2280) @ java.awt.window.dispatcheventimpl(window.java:2750) @ java.awt.component.dispatchevent(component.java:4713) @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:758) @ java.awt.eventqueue.access$500(eventqueue.java:97) @ java.awt.eventqueue$3.run(eventqueue.java:709) @ java.awt.eventqueue$3.run(eventqueue.java:703) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$javasecurityaccessimpl.dointersectionprivilege(protectiondomain.java:76) @ java.security.protectiondomain$javasecurityaccessimpl.dointersectionprivilege(protectiondomain.java:86) @ java.awt.eventqueue$4.run(eventqueue.java:731) @ java.awt.eventqueue$4.run(eventqueue.java:729) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$javasecurityaccessimpl.dointersectionprivilege(protectiondomain.java:76) @ java.awt.eventqueue.dispatchevent(eventqueue.java:728) @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:201) @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:116) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:105) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:101) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:93) @ java.awt.eventdispatchthread.run(eventdispatchthread.java:82) 

you need check array length this

private void jtextfield1mousedragged(java.awt.event.mouseevent evt) {         highlight[] h = jtextfield1.gethighlighter().gethighlights();         if (h != null && h.length > 0) {             jlabel2.settext(                     "selected text: " + jtextfield1.gettext().substring(h[0].getstartoffset(), h[0].getendoffset()));         }     } 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -