Results 1 to 9 of 9
  1. #1
    Join Date
    Dec 2009
    Posts
    18
    Rep Power
    0

    Default "Error cannot find symbol" "throws BadLocationException"

    I am getting the error that it is the thread title when running this code:

    Java 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:
    Java Code:
    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 the
    Java Code:
    hilit.addHighlight(0, 5, painter);
    one
    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.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: "Error cannot find symbol" "throws BadLocationException"

    I have not used the Highlighter class before. But I suggest you move the hilit and painter definitions outside the loop. I believe they can be allocated once for subsequent highlighting ahead of time. Then you need to set the highlighter in your JTextArea object using ta.setHighLighter(hilit). I believe there will still be problems with the coordinates you are highlighting but the exceptions go away.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: "Error cannot find symbol" "throws BadLocationException"

    Quote Originally Posted by Daniel Silvester View Post
    I am getting the error that it is the thread title when running this code:
    Rubbish. If you get "cannot find symbol" your code won't compile and you won't have anything to run.

    Post the stack trace. Copy and paste, don't paraphrase.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: "Error cannot find symbol" "throws BadLocationException"

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Join Date
    Dec 2009
    Posts
    18
    Rep Power
    0

    Default Re: "Error cannot find symbol" "throws BadLocationException"

    Shall I update my first post and put these links?

    Rubbish. If you get "cannot find symbol" your code won't compile and you won't have anything to run.

    Post the stack trace. Copy and paste, don't paraphrase.
    When I said "running this code" I meant: "when trying to compile this code"


    I have not used the Highlighter class before. But I suggest you move the hilit and painter definitions outside the loop. I believe they can be allocated once for subsequent highlighting ahead of time. Then you need to set the highlighter in your JTextArea object using ta.setHighLighter(hilit). I believe there will still be problems with the coordinates you are highlighting but the exceptions go away.
    I tried this
    Java Code:
    Highlighter hilit = new DefaultHighlighter();
        Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW); 
         ta.setHighLighter(hilit);
        for (int k=0; k<splitter.length; k++) { 
          if (splitter[k].equals ("bonita")) {
            //ta.append("B*O*N*I*T*A");
            hilit.addHighlight(0, 5, painter);
          }
    But this is leaving to this:
    error: cannot find symbol
    ta.setHighLighter(hilit);

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: "Error cannot find symbol" "throws BadLocationException"

    Post your new code, in the form of a SSCCE (Short, Self Contained, Correct, Example) that gives the same compile time error.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Join Date
    Dec 2009
    Posts
    18
    Rep Power
    0

    Default Re: "Error cannot find symbol" "throws BadLocationException"

    Quote Originally Posted by DarrylBurke View Post
    Post your new code, in the form of a SSCCE (Short, Self Contained, Correct, Example) that gives the same compile time error.

    db
    Java Code:
    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: ");  
        Highlighter hilit = new DefaultHighlighter();
        Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
        ta.setHighLighter(hilit);
        for (int k=0; k<splitter.length; k++) {   
          if (splitter[k].equals ("bonita")) {  
            //ta.append("B*O*N*I*T*A");   
            hilit.addHighlight(0, 5, painter);  
          }    
          ta.append (splitter[k] + " ");  
        }  
        ta.append ("\n");   
      }   
    }

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,669
    Rep Power
    5

    Default Re: "Error cannot find symbol" "throws BadLocationException"

    Is it really so difficult? The way I tracked it down in less than a minute:

    1. google
    2. search for "java 7 jtextarea"
    3. click on first result, which is the jtextarea API doc
    4. activate browser search
    5. search for "setHigh"
    6. browser finds setHighlighter() method

    Now notice the slight difference in the method name from what is in your code. The API docs are your friend; an IDE is an even bigger friend since it will help you catch these kind of typos by providing auto-completion of method names.

  9. #9
    Join Date
    Dec 2009
    Posts
    18
    Rep Power
    0

    Default Re: "Error cannot find symbol" "throws BadLocationException"

    Quote Originally Posted by gimbal2 View Post
    Is it really so difficult? The way I tracked it down in less than a minute:

    1. google
    2. search for "java 7 jtextarea"
    3. click on first result, which is the jtextarea API doc
    4. activate browser search
    5. search for "setHigh"
    6. browser finds setHighlighter() method

    Now notice the slight difference in the method name from what is in your code. The API docs are your friend; an IDE is an even bigger friend since it will help you catch these kind of typos by providing auto-completion of method names.
    Thanks, next time I have a similar error I will repeat this procedure.

    Anyway when I corrected the name of the method and send to compile once more I received
    Procurador de Texto/FileRegistry.java:75: error: unreported exception BadLocationException; must be caught or declared to be thrown
    hilit.addHighlight(0, 5, painter);
    I added a
    Java Code:
    throws BadLocationException
    to this method
    Java Code:
    void printLines (JTextArea ta)  {
        String temp = content.get(0);  
        String [] splitter = temp.replaceAll("[.,?!:;/]", "").split(" ");
        ta.append ("O conteudo do arquivo e: ");
        Highlighter hilit = new DefaultHighlighter();
        Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW); 
         
        for (int k=0; k<splitter.length; k++) { 
          if (splitter[k].equals ("bonita")) {
            ta.append("B*O*N*I*T*A");
            ta.setHighlighter(hilit);  
            hilit.addHighlight(0, 5, painter);
          }  
          ta.append (splitter[k] + " ");
        }
        ta.append ("\n"); 
      }
    and then started to receive this error message:
    error: unreported exception BadLocationException; must be caught or declared to be thrown
    Procurador de Texto/FileManager.java:141: error: unreported exception BadLocationException; must be caught or declared to be thrown
    fileData.get(i).printLines(ta);
    or, in another words the same error passed to the method that calls the first method.

    Then I added the
    Java Code:
    BadLocationException
    to the begining of this method:
    Java Code:
     public void printData (GraphicInterface gi)  {
        int n=fileData.size(); 
        JTextArea ta = gi.getTextArea();           
        for (int i=0; i<n; i++) {
          System.out.println ("Deseja visualizar o arquivo de numero " +  i + " ? s/n"); 
          ta.append ("Deseja visualizar o arquivo de numero " +  i + " ? s/n" + "\n"); 
          while (true) { 
            if (gi.getShowFile()==1) {
              fileData.get(i).printLines(ta);
              gi.setShowFile(2);
              break;
            }
            else if (gi.getShowFile()==0) {
              System.out.println ("Você apertou não"); 
              gi.setShowFile(2);
              break; 
            }
          }
        }
      }
    and now I receive
    Procurador de Texto/FileManager.java:133: error: cannot find symbol
    public void printData (GraphicInterface gi) throws BadLocationException {
    ^
    symbol: class BadLocationException
    location: class FileManager
    Since I throwed the exception in both methods and I still getting compiling error, I tried to reproduce the error into a self-contained file but wasn't able to do so. Instead of getting a compiling error, I am getting a not wanted behaviour.
    Here is the code:
    Java Code:
    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: ");  
        Highlighter hilit = new DefaultHighlighter();
        Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
        
        for (int k=0; k<splitter.length; k++) {   
          if (splitter[k].equals ("bonita")) {  
            //ta.append("B*O*N*I*T*A");   
            ta.setHighlighter(hilit);
            hilit.addHighlight(0, 5, painter);  
          }    
          ta.append (splitter[k] + " ");  
        }  
        ta.append ("\n");   
      }   
    }
    the problem is that the highlight is only in the first letters of the sentence when I expect it to be over the word "bonita"

Similar Threads

  1. Pls solve my "Cannot find symbol" error
    By yvijai in forum New To Java
    Replies: 5
    Last Post: 05-18-2012, 11:17 PM
  2. Strange "Cannot find symbol" error...
    By JimmyD in forum New To Java
    Replies: 8
    Last Post: 04-19-2012, 09:39 PM
  3. Strange "Cannot find symbol" error...
    By JimmyD in forum Advanced Java
    Replies: 7
    Last Post: 04-19-2012, 09:37 PM
  4. Error "can not find symbol variable"
    By FullMetalHollow in forum New To Java
    Replies: 5
    Last Post: 10-04-2009, 09:51 PM
  5. "Cannont find symbol Constructor" error
    By Welsh in forum New To Java
    Replies: 7
    Last Post: 01-25-2008, 12:12 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
  •