Results 1 to 2 of 2
  1. #1
    Sarah61 is offline Member
    Join Date
    Oct 2013
    Rep Power

    Default Exception in thread "AWT-EventQueue-0"

    I want to highlight the patterns that are present in my string. For this purpose I have created a database of patterns and stored them in an array. Now I want these patterns to be highlighted in jtextpane and with different colors and I have used if else condition for this purpose that will highlight the patterns with different colors based on the strings that are stored in other arraylist but I am having following exception .

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.text.DefaultHighlighter.paint(DefaultH
    at javax.swing.plaf.basic.BasicTextUI.paintSafely(Bas
    at javax.swing.plaf.basic.BasicTextUI.paint(BasicText
    at javax.swing.plaf.synth.SynthEditorPaneUI.paint(Syn
    at javax.swing.plaf.synth.SynthEditorPaneUI.update(Sy
    at javax.swing.JComponent.paintComponent(JComponent.j ava:778)
    at javax.swing.JComponent.paint(
    at javax.swing.JComponent.paintToOffscreen(JComponent .java:5219)

    at javax.swing.RepaintManager$PaintManager.paint(Repa
    at javax.swing.RepaintManager.paint(RepaintManager.ja va:1249)
    at javax.swing.JComponent._paintImmediately(JComponen
    at javax.swing.JComponent.paintImmediately(JComponent .java:4978)
    at javax.swing.RepaintManager$ va:808)
    at javax.swing.RepaintManager$ va:796)
    at Method)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai
    at javax.swing.RepaintManager.paintDirtyRegions(Repai
    at javax.swing.RepaintManager.prePaintDirtyRegions(Re
    at javax.swing.RepaintManager.access$1100(RepaintMana
    at javax.swing.RepaintManager$
    at java.awt.event.InvocationEvent.dispatch(Invocation
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:733)
    at java.awt.EventQueue.access$200( )
    at java.awt.EventQueue$
    at java.awt.EventQueue$
    at Method)
    at java.awt.EventQueue.dispatchEvent( 703)
    atjava.awt.EventDispatchThread.pumpEventsForHierar chy(
    at java.awt.EventDispatchThread.pumpEvents(EventDispa
    at java.awt.EventDispatchThread.pumpEvents(EventDispa

    This is my code:-

    Java Code:
      for (int i = 0; i < mot.size(); i++) {
        if (mot.get(i) == "set") {
          myHighlightPainter = new MyHighlightPainter(;
        } else if (mot.get(i) == "get") {
          myHighlightPainter = new MyHighlightPainter(;
        highlight(jTextPane2, pat.get(i));
      }// patterns arraylist
    public void highlight(JTextComponent textComp, String pattern) {
      try {
        Highlighter hilite = textComp.getHighlighter();
        Document doc = textComp.getDocument();
        String text = doc.getText(0, doc.getLength());
        int pos = 0;
        // Search for pattern
        // see I have updated now its not case sensitive
        while ((pos = text.toUpperCase().indexOf(pattern.toUpperCase(), pos)) >= 0) {
          // Create highlighter using private painter and apply around pattern
          hilite.addHighlight(pos, pos + pattern.length(), myHighlightPainter);
          pos += pattern.length();
      } catch (BadLocationException e) {
    How can I get rid of this exception?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default Re: Exception in thread "AWT-EventQueue-0"

    Can you make and post a small, complete program that compiles, executes and shows the problem?

    Also posted at:
    Last edited by Norm; 10-29-2013 at 08:21 PM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 6
    Last Post: 03-19-2011, 08:10 PM
  2. Replies: 3
    Last Post: 04-30-2010, 08:44 PM
  3. Replies: 5
    Last Post: 08-26-2009, 12:37 PM
  4. Replies: 3
    Last Post: 06-07-2009, 01:14 AM
  5. Replies: 10
    Last Post: 01-29-2009, 09:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts