I am new to java and stackOverflow so please be patient if I don't post all the necessary information about my question. Basically, I am trying to read lines from a .txt file and store them in a Stack. I also need to access this Stack in a different class. So I created a get method but it always returns null. Please help!
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.util.Stack;
public class Hints {
private Stack stack;
private File f;
private String line;
private Scanner scanner;
public Hints(){
f = new File("hints.txt");
stack = new Stack();
}
public void getList() throws FileNotFoundException, IOException {
scanner = new Scanner(f);
while(scanner.hasNextLine()) {
line = scanner.nextLine();
stack.add(line);
}
scanner.close();
}
public Stack getStack(){
return stack;
}
}
When I try to print the stack with a simple System.out.print, it will come out as null. Where is my issue(s)?
Thank you.
printcode. stack should not be null.