1

I'm trying to display result of analysed text in jTextArea, and I'm coding this program via Netbeans. Below is the code that I'm trying to repair in order the result to show in jTextArea3 but its showing non static variable cannot be referenced from static content error. Since in Netbeans jTextArea code will be generated by itself, I couldn't change anything. PLease help me

    public static void main(String[] args) throws NetworkException, AnalysisException {

       File textSRC = new File("MyText.txt");
       String myTextCount = null;
       BufferedReader myTextBr = null;
       String check = "";
       try {
       String myTextCurrentLine;
       myTextBr = new BufferedReader(new FileReader(textSRC));
       while ((myTextCurrentLine = myTextBr.readLine()) != null) {
           myTextCount = myTextCount + " " + myTextCurrentLine;
       }       
       // Sample request, showcasing a couple of TextRazor features
       String API_KEY = "7d5066bec76cb47f4eb4e557c60e9b979f9a748aacbdc5a44ef9375a";

       TextRazor client = new TextRazor(API_KEY);       
       client.addExtractor("words");
       client.addExtractor("entities");
       client.addExtractor("entailments");
       client.addExtractor("senses");
       client.addExtractor("entity_companies");              
       String rules = "entity_companies(CompanyEntity) :- entity_type(CompanyEntity, Company').";        
       client.setRules(rules);                       
       AnalyzedText response = client.analyze(myTextCount);                    
       File file = new File("Hello1.txt");
       // creates the file
       file.createNewFile();
       // creates a FileWriter Object
       FileWriter writer = new FileWriter(file); 
       // Writes the content to the file

       for (Sentence sentence : response.getResponse().getSentences()) {
           for (Word word : sentence.getWords()) {
            System.out.println("----------------");
               System.out.println("Word: " + word.getLemma());

               for (Entity entity : word.getEntities()) {
                   ///System.out.println("Matched Entity: " + entity.getEntityId());
               }                  
               for (Sense sense: word.getSenses()) {
                   //System.out.println("Word sense: " + sense.getSynset() + " has score: "    + sense.getScore());
               }                
           }
       }

                   // Use a custom rule to match 'Company' type entities                       
       for (Custom custom : response.getResponse().getCustomAnnotations()) {
           for (Custom.BoundVariable variable : custom.getContents()) {
               if (null != variable.getEntityValue()) {
                   for (Entity entity : variable.getEntityValue()) {
                       String CompanyFound = ("Variable: " + variable.getKey() +"\n"+    "Value:" + entity.getEntityId());
                       System.out.println(CompanyFound);
                       jTextArea3.append(CompanyFound);
                       writer.write(CompanyFound); 
                       writer.flush();
                       writer.close();
                   }
               }
           }
       }  
    }catch (IOException ex) {
    }           
   }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user3003233
  • 31
  • 1
  • 8

1 Answers1

2

Your error is pretty clear, in a static context you can't refer something non-static . Cause static belongs to class level you cannot use instance level stuff without any instance of it. For that you have to instantiate an object to use it.

Example:

public class Test{

private JTextArea textArea;


public static void someMethod(){
    //textArea = new JTextArea(); you can't do this
     //you need an instance
     Test test = new Test();
     test.textArea = new JTextArea(10,10);
     test.textArea.setText("Hello world");

}
}
nachokk
  • 14,363
  • 4
  • 24
  • 53
  • does that mean i have to create a new method so that static error won't occur? – user3003233 Jan 02 '14 at 01:23
  • @user3003233 Well you don't provide a complete example , but if you are in a static method then for use a method that is not static you need to have an instance of that object, like i show you in the example. I cannot use `textArea` without having an instance – nachokk Jan 02 '14 at 02:17
  • I manage to rectify the error by using your way, Thank You nachokk!But the textarea is empty. Expected result is not shown in all of the textarea exist on the interface. But when I do System.out.println it shows. – user3003233 Jan 02 '14 at 02:54
  • @user3003233 Then that is another problem not related with this thread.. i can't help without any code to see. Consider accepting this answer for this question, and feel free to create a new post with the new question if you can't figure out by yourself the solution. – nachokk Jan 02 '14 at 02:56