&引用;AWT-EventQueue-0“;java.lang.NullPointerException异常

&引用;AWT-EventQueue-0“;java.lang.NullPointerException异常,java,multithreading,swing,nullpointerexception,jtextpane,Java,Multithreading,Swing,Nullpointerexception,Jtextpane,我想突出显示字符串中存在的模式。为此,我创建了一个模式数据库,并将它们存储在一个数组中。现在,我希望这些模式在jtextpane中以不同的颜色高亮显示,为此,我使用了if-else条件,该条件将根据存储在其他arraylist中的字符串高亮显示具有不同颜色的模式,但我有以下例外 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.text.DefaultHighlighter.p

我想突出显示字符串中存在的模式。为此,我创建了一个模式数据库,并将它们存储在一个数组中。现在,我希望这些模式在jtextpane中以不同的颜色高亮显示,为此,我使用了if-else条件,该条件将根据存储在其他arraylist中的字符串高亮显示具有不同颜色的模式,但我有以下例外

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.text.DefaultHighlighter.paint(DefaultHighlighter.java:72)
at javax.swing.plaf.basic.BasicTextUI.paintSafely(BasicTextUI.java:731)
at javax.swing.plaf.basic.BasicTextUI.paint(BasicTextUI.java:881)
at javax.swing.plaf.synth.SynthEditorPaneUI.paint(SynthEditorPaneUI.java:180)
at javax.swing.plaf.synth.SynthEditorPaneUI.update(SynthEditorPaneUI.java:168)
at javax.swing.JComponent.paintComponent(JComponent.java:778)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5219)

at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1452)
at javax.swing.RepaintManager.paint(RepaintManager.java:1249)
at javax.swing.JComponent._paintImmediately(JComponent.java:5167)
at javax.swing.JComponent.paintImmediately(JComponent.java:4978)
at javax.swing.RepaintManager$3.run(RepaintManager.java:808)
at javax.swing.RepaintManager$3.run(RepaintManager.java:796)
at java.security.AccessController.doPrivileged(Native Method)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718)
at javax.swing.RepaintManager.access$1100(RepaintManager.java:62)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1677)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
atjava.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
这是我的密码

  jTextPane2.setText(seq);

  for (int i = 0; i < mot.size(); i++) {
    if (mot.get(i) == "set") {
      myHighlightPainter = new MyHighlightPainter(Color.red);
    } else if (mot.get(i) == "get") {
      myHighlightPainter = new MyHighlightPainter(Color.green);
    }
    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) {
  }
}
jTextPane2.setText(seq);
对于(int i=0;i=0){
//使用私人画师创建荧光笔并在图案周围应用
hilite.addHighlight(pos,pos+pattern.length(),myHighlightPainter);
pos+=pattern.length();
}
}捕获(错误位置异常e){
}
}

如何解决此问题?

您的myHighlightPainter在某些情况下为
null

第72行


如何解决此问题?
——通过在局部变量
mot.get(I)=“set”
->中为
JTextPane
s
Document
发布带有硬编码文本的文本,这是错误的。改为使用
mot.get(i).equals(“set”)
(但它不会解决此问题)。这是在setText方法中传递的字符串。seq=“aset”;我怎样才能避免这个问题?
            Highlighter.HighlightPainter p = info.getPainter();
      -->   p.paint(g, info.getStartOffset(), info.getEndOffset(),a, component);