-1

I am writing objects to an arraylist in which I need to access particular elements of the object. However, I keep getting this annoying error. My load class just has setters for the elements within the objects. How do I fix it? I am just wanting to assign that particular element to a variable.

Driver class

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Driver {

public static void main(String[] args) throws IOException 
        {
            BufferedReader file = new BufferedReader(new          FileReader("test.txt"));
            ArrayList<Load> list = new ArrayList<Load>();
            String line;
            String[] words=new String[3] ;
            Load load = new Load();
            int x =0;
            while((line=file.readLine())!=null)
            {   
            words=line.split("\t");

            String process=words[0];
            int arrivalTime=Integer.parseInt(words[1]);
            int serviceTime=Integer.parseInt(words[2]);

            list.add(new Load(process,arrivalTime,serviceTime));
            x++;
            }
    }
    }

RoundRobin class

public class RoundRobin {


    Driver data = new Driver();
    String a =((Load) data.list.get(0)).process; //This is where the error occurs
}
The_Enigma
  • 117
  • 1
  • 11

3 Answers3

1

You define list as a local variable in main:

public static void main(String[] args) {
    ....
    ArrayList<Load> list = new ArrayList<Load>();

It will only be available within the scope of main.

You need to make it a class member:

public class Driver {
    private List<Load> list = new ArrayList<Load>();

    public List<Load> getList() {
        return list;
    }
    ....
}

Then you call

String a =((Load) data.getList().get(0)).process;

You probably don't want to expost list directly like this, but that is another matter.

bradimus
  • 2,472
  • 1
  • 16
  • 23
0

This is how your Driver class should looks like:

public class Driver {

    ArrayList<Load> list = new ArrayList<Load>();

    public void initializeList() throws IOException{
        BufferedReader file = new BufferedReader(new FileReader("test.txt"));
        String line;
        String[] words=new String[3] ;
        Load load = new Load();
        int x =0;
        while((line=file.readLine())!=null){   
            words=line.split("\t");
            String process=words[0];
            int arrivalTime=Integer.parseInt(words[1]);
            int serviceTime=Integer.parseInt(words[2]);
            list.add(new Load(process,arrivalTime,serviceTime));
            x++;
        }
    }

    public List<Load> getList() {
        return list;
    }
}

Then in your RoundRobin class:

public class RoundRobin {
...
    Driver data = new Driver();
    data.initializeList();
    String a =((Load) data.getList.get(0)).process; 
...
}
user6904265
  • 1,938
  • 1
  • 16
  • 21
-1

make this list reference to a class by moving the declaration from method to class level and as

static ArrayList list = new ArrayList();

snofty
  • 70
  • 7