The variable may not have been initialized?

so in the while loop I print some of the storage items of the ArrayList. but then when I call it it says the array may not have been initialized.

any thoughts? I am trying to read a file of strings. each line has at least 8 elements and I am sure the array is not empty because I printed it in a while loop.

?

public class ReaderFile {
    public static Scanner input;
    public static Scanner input2;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        int count=0;
        ArrayList<Team> store;
        ArrayList<Robot> store2;
        //Robot robot;

        String fileLocation = "Tourney2Teams.csv";
        String fileLocation2 = "Tourney1robots.csv";


        try{
            input = new Scanner(new File(fileLocation)).useDelimiter(",");
        }
        catch (IOException ioException)
        {
            System.out.print("PROBLEM");
        }


        try {
            input2 = new Scanner(new File (fileLocation2)).useDelimiter(",");
        }
        catch (IOException ioException)
        {
            System.out.print("problem with robot");
        }


        try{
                input.nextLine();
                System.out.print("PLEAse\n");
                int countt = 0;
                while(input.hasNext())
                {
                        //int countt = 0;
                        int ID = input.nextInt();
                        String teamName = input.next();
                        String coachFirst = input.next();
                        String coachLast = input.next();
                        String mentorFirst = input.next();
                        String mentorLast = input.next();
                        String teamFs = input.next();
                        String teamSS = input.next();
                        input.nextLine();
                        store = new ArrayList<>();
                        Team team = new Team (teamName, ID, coachFirst, coachLast,mentorFirst,mentorLast,teamFs,teamSS);
                        store.add(team);
                        System.out.print("Team Numer"+store.get(0).teamNumber+"\n");
                        countt = countt+1;
                        System.out.print("\n"+countt);
                }
        }
        catch (NoSuchElementException statExcemtion)
        {
            System.out.print("\nAnkosh");
        }
        String x = store.get(2).teamName;
    }
}

      

+3


source to share


2 answers


store = new ArrayList<>();

      

This line reinitializes the store on each pass in while

. You probably want to initialize it before it while

accumulates during the loop.



It says that it was not initialized because for some reason it never executed a while loop (empty input).

0


source


It can be uninitialized in two cases:

  • An exception is thrown in the try block NoSuchElementException

    , in which case your block is executed catch

    rather store

    than initialized. I would suggest either return

    from a block catch

    or move your line String x =

    inside a block try

    .

  • Your loop has zero iterations. In this case, it is store

    also uninitialized. It also looks like a boolean error, you probably want yours to store

    be instantiated before the loop while

    .



I would also suggest checking what store

has at least three elements before accessing the element 2

.

0


source







All Articles