I am relatively new to Java, and get this occasionally myself due to the
"static main" method being typed within the class.

The issue is that you are trying to access "BF" and "str" that exist inside
a class, from a static environment (main). I think it would have helped if
the exception had been a little more explicit, mentioning the fields that
were at fault.

If you were accessing the class from another source file (class) you would
first have to create an instance, i.e.

Add2StrJOp myAdd2StrJOp = new Add2StrJOp();

Then you could access "BF" and "str" as

myAdd2StrJOp.BF and myAdd2StrJOp.str.

As the main method is a static method, it also needs to create an instance
of Add2StrJOp using "new" before you can access the fields within the
class, just as if using your class from an external source file.

So add the line above at the top of your main method, just after the "try
{" line and then prefix the class fields with your newly created instance
variable to reference them.

Hope that helps.

John

On Sun, 22 May 2022, 23:53 Zulfi Khan, <zulfi6...@yahoo.com.invalid> wrote:

> Hi,
>
> I have written the following code:
>
> /*
>  * Click
> nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to
> change this license
>  * Click
> nbfs://nbhost/SystemFileSystem/Templates/Project/Maven2/JavaApp/src/main/java/${packagePath}/${mainClassName}.java
> to edit this template
>  */
>
> package com.mycompany.add2strjop;
> import javax.swing.*;
> import java.lang.NumberFormatException;
> import java.io.FileWriter;
> import java.io.FileReader;
> import java.io.BufferedWriter;
> import java.io.BufferedReader;
> import java.io.IOException;
> public class Add2StrJOp {
>   BufferedReader BF= null;
>   BufferedReader BW= null;
>   String str = "test";
>
>    public static void main(String args[]){
>       try{
>           FileReader file = new FileReader("abc.txt");
>           BF = new BufferedReader(file);//E
>          //BF= new BufferedReader(new FileReader("abc.txt"));
>           while ((str = BF.readLine())!=null){//E
>             JOptionPane.showMessageDialog(null, str);//E
>           //JOptionPane.showMessageDialog(null, "hello World");
>             //BF.clos()
>           }
>          }catch(IOException e){
>               e.printStackTrace();
>          }
>           finally{
>                  try{
>                     if( BF!=null){//E
>                        BF.close();//E
>                     }
>                  }catch(IOException e) {
>                     e.printStackTrace();
>                   }
>                 }//finally
>           try{
>                 BW = new BufferedWriter(new FileWriter("abc.txt"));//E
>                 String str= "This is a test";
>                 BW.write(str);//E
>                 BW.close();//E
>              }catch(IOException e) {
>                  e.printStackTrace();
>              }
> }
> }
>
>
>
> I have commented the lines as //E where I am getting the error "Non-static
> variable cannot be referenced from static context.
>
> Zulfi.
>
>
>

Reply via email to