I am getting the error that it is the thread title when running this code:
void printLines (JTextArea ta) { String temp = content.get(0); String [] splitter = temp.replaceAll("[.,?!:;/]", "").split(" "); ta.append ("O conteudo do arquivo e: "); for (int k=0; k<splitter.length; k++) { if (splitter[k].equals ("bonita")) { //ta.append("B*O*N*I*T*A"); Highlighter hilit = new DefaultHighlighter(); Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW); hilit.addHighlight(0, 5, painter); } ta.append (splitter[k] + " "); } ta.append ("\n"); }
The error began to show up when I added this three lines related to the Highlighter. I think it might be related to the index that I am passing to addHighlight but I am not sure.
I made I self-contained version of the error here:import javax.swing.*; import javax.swing.text.*; import java.awt.*; import java.awt.event.*; public class Main { public static void main (String [] args) throws Exception { JFrame jframe = new JFrame (); JTextArea ta = new JTextArea(20,40); JScrollPane sp = new JScrollPane (ta); jframe.setVisible (true); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jframe.setLayout(new FlowLayout()); jframe.add(sp); jframe.pack(); String temp = "Test1 Test2 bonita Test3"; String [] splitter = temp.replaceAll("[.,?!:;/]", "").split(" "); ta.append ("O conteudo do arquivo e: "); for (int k=0; k<splitter.length; k++) { if (splitter[k].equals ("bonita")) { //ta.append("B*O*N*I*T*A"); Highlighter hilit = new DefaultHighlighter(); Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW); hilit.addHighlight(0, 5, painter); } ta.append (splitter[k] + " "); } ta.append ("\n"); } }
But with this code I don't get the same error.....what I am getting now it is a nullPointerException in line 24 theonehilit.addHighlight(0, 5, painter);
The frame is created shows the two first words before generating the error.
Anyone has an idea about what can be?
I also would appreciate if someone can provide me a few tips, links or whatever about how I can advance in text using the keyboard. In another words, if I press right it advances until a certain occurrence of a certain word, then if I press again, it advances to the next one until I reach the last occurrence of a word.