0

I want to access nodes[i] globally in every method, where am I wrong?

class file{
    private static java.io.File file;
    private static BufferedReader reader;
    private static int[] nodes;
    
    static {
            try {
                file = new java.io.File("/home/madhu/Desktop/readData.txt");
                reader = new BufferedReader(new FileReader(String.valueOf(file)));
                String totalNode = reader.readLine();
                String[] strs = totalNode.trim().split(",");
                 for (int i = 0; i < strs.length; i++) {
                        int a = Integer.parseInt(strs[i]);
                        nodes[i]=a;
                 }
           } catch (IOException e) {
                e.printStackTrace();
           }            
    }
    private static void level() {
        System.out.println(nodes[2]);
    }
    public static void main(String[] args) throws Exception {
        System.out.println(nodes[2]);
    }
}

readData.txt contains :

1,2,4  

I want to use the individual number in further calculations, so how to access it independently?

Basilevs
  • 22,440
  • 15
  • 57
  • 102

2 Answers2

0

No array created

At no point did you create an array. Think of an array as being a container with slots, like a cabinet with shelves. The shelves are empty until you fill them. But your code never built the cabinet. Your code only stated a desire to at some point own a cabinet, but never got around to making said cabinet.

Your line private static int[] nodes; declares a variable nodes that is ready and willing to refer to (point to) an array if and when some array is built.

To actually build that cabinet, that array, call new int[] {…}.

Change your code to something like this:

package work.basil.example;

public class StaticTrial
{
    private static java.io.File file;
    private static int[] nodes;

    static
    {
        StaticTrial.nodes = new int[] { 11 , 12 , 13 };  
    }

    private static void level ( )
    {
        System.out.println( nodes[ 2 ] );
    }

    public static void main ( String[] args ) throws Exception
    {
        System.out.println( "Running `main` method." );
        System.out.println( nodes[ 2 ] );
        System.out.println( "Ending `main` method." );
    }
}

…or this…

package work.basil.example;

public class StaticTrial
{
    private static java.io.File file;
    private static int[] nodes = new int[] { 11 , 12 , 13 };

    private static void level ( )
    {
        System.out.println( nodes[ 2 ] );
    }

    public static void main ( String[] args ) throws Exception
    {
        System.out.println( "Running `main` method." );
        System.out.println( nodes[ 2 ] );
        System.out.println( "Ending `main` method." );
    }
}

For simplicity I am ignoring your file-loading code, as that seems irrelevant to the intent of your question. And I will sweep aside the usual cautions about thinking twice before using static, and I'll not explain how using static is not object-oriented and is fraught with risk of misuse.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

Your code never initializes the int[] nodes. SO you need to initialize it before the for loop as below :

nodes = new int[strs.length];

So the updated static block should be as below:

 static {
    try {
      file = new java.io.File("/home/madhu/Desktop/readData.txt");
      reader = new BufferedReader(new FileReader(String.valueOf(file)));
      final String totalNode = reader.readLine();
      final String[] strs = totalNode.trim().split(",");

      // CHANGE IS HERE
      nodes = new int[strs.length];

      for (int i = 0; i < strs.length; i++) {
        final int a = Integer.parseInt(strs[i]);
        nodes[i] = a;
      }
    } catch (final IOException e) {
      e.printStackTrace();
    }

  }
Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47