-1

I am trying to display a particular text file in JTextArea but eclipse keeps showing me an error.

try {
    FileReader reader = new FileReader("Student.txt");
    BufferedReader br = new BufferedReader(reader);
    JTextArea.read(br,null);
    br.close();
    JTextArea.requestFocus();
    }
catch(Exception e){ 
    JOptionPane.showMessageDialog(null, "Something went wrong");
} 

this is the code i am using and the error i get is

"Cannot make a static reference to the non-static method read(Reader, Object) from the type JTextComponent"

Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
Aman Chawla
  • 229
  • 3
  • 12

1 Answers1

2

You need to create object for JTextArea class.
Please try with below code:

FileReader reader;
        try {

            reader = new FileReader("Student.txt");

            BufferedReader br = new BufferedReader(reader); 

             JTextArea area = new JTextArea();


                area.read(br,null);
                br.close();

             area.requestFocus();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException ioe) {
            // TODO Auto-generated catch block
            ioe.printStackTrace();
        }
Ye Win
  • 2,020
  • 14
  • 21