Java 无法从静态内容引用非静态变量jTextArea

Java 无法从静态内容引用非静态变量jTextArea,java,swing,netbeans-7,static,Java,Swing,Netbeans 7,Static,我试图在jTextArea中显示分析文本的结果,并通过Netbeans对该程序进行编码。下面是我试图修复的代码,以便在jTextArea3中显示结果,但其显示的非静态变量不能从静态内容错误中引用。因为在Netbeans中,jTextArea代码将由其自身生成,所以我无法更改任何内容。请帮帮我 public static void main(String[] args) throws NetworkException, AnalysisException { File te

我试图在jTextArea中显示分析文本的结果,并通过Netbeans对该程序进行编码。下面是我试图修复的代码,以便在jTextArea3中显示结果,但其显示的非静态变量不能从静态内容错误中引用。因为在Netbeans中,jTextArea代码将由其自身生成,所以我无法更改任何内容。请帮帮我

    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) {
    }           
   }

您的错误很明显,在静态上下文中,您不能引用非静态的内容。因为static属于类级别,所以在没有实例的情况下不能使用实例级别的东西。为此,必须实例化一个对象才能使用它

例如:

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");

}
}

您的main()方法是否也驻留在与jTextArea3相同的类中?是的,它都在同一个main()方法中,这是否意味着我必须创建一个新方法以避免出现静态错误?@user3003233您没有提供完整的示例,但是,如果您使用的是静态方法,那么为了使用非静态的方法,您需要有该对象的实例,就像我在示例中展示的那样。如果没有实例,我无法使用
textArea
。我可以用你的方式纠正错误,谢谢你,nachokk!但是文本区域是空的。预期结果不会显示在界面上存在的所有文本区域中。但当我执行System.out.println时,它会显示@user3003233,这是另一个与此线程无关的问题。。如果没有任何代码可以查看,我就无能为力。考虑接受这个问题的答案,如果你自己解决不了问题,就可以自由地用一个新的问题来创建一个新的帖子。