0

Here is the code I wrote :

    try {   
        String[] Flight_Inputs = null;
        Scanner in = new Scanner(f); // f = input file
        int counter =0;
        while(in.hasNextLine()) {
            Flight_Inputs[counter++] = in.next();
          //Flight_Inputs = in.nextLine().split("\n"); // Also tried this
        }
        in.close(); 
         for(int i = 0,j=0; i < Flight_Inputs.length; i++){
             Boeing.setDestination_from(Flight_Inputs[j++]);
             Boeing.setDestination_to(Flight_Inputs[j++]);
             Boeing.setName(Flight_Inputs[j++]);
         }

I am getting Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException error I tried also other variations and got an error that I was trying to access index 1 of Flight_Inputs but compiler says it doesnt exist.

What I want to do do is simply get some lines from the text file and assign them to variables of Boieng object... Any help would be appreciated!

1
  • 5
    Please try and respect Java naming conventions (variable names should begin with a lowercase letter); it makes it easier for people used to Java to give answers! Commented Jun 10, 2013 at 17:35

3 Answers 3

4

You never initialized Flight_Inputs array, that is why you get java.lang.NullPointerException. Anyway in case you don't know how many lines file will contain you should use List instead like ArrayList<String>.

Try maybe this way:

List<String> lines= new ArrayList<String>();
Scanner in = new Scanner(f); // f = input file
while(in.hasNextLine()) {
    lines.add(in.nextLine());
}
in.close(); 

Now to iterate over this list you can use

for(String line: lines){
    System.out.println(line);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot man you saved me once again, works, I also guessed about unknown size but I was using ArrayList for almost everything, every Object list ... and wanted to avoid simple string ...thought strings can have unlimited size, because you can concatenate them etc. well I was wrong need more practice! Thanks again!
Although I used ArrayList<String> lines= new ArrayList<String>(); List gave the error : The type List is not generic; it cannot be parameterized with arguments <String>, I would like to know why for self improvement! :)
@Anarkie I suspect that you imported java.awt.List instead java.util.List.
4

You should change your Flight_Inputs to a dynamic size list, for example:

change:

//as Pshemo pointed out, this is null, 
//and is causing your NullPointerException
String[] Flight_Inputs = null;
...
Flight_Inputs[counter++] = in.nextLine();
...
for(int i = 0,j=0; i < Flight_Inputs.length; i++){

to:

List<String> Flight_Inputs = new ArrayList<String>();
...
Flight_Inputs.add(in.next());
...
for(String filghtInput : Flight_Inputs){

1 Comment

+1 for nice format of the answer. BTW OP should probably use nextLine() instead of next().
2

Well yes, String[] Flight_Inputs = null; means that... Flight_Inputs is null.

You would need to instantiate it with a non null value, for example:

String[] Flight_Inputs = new String[size];

except that you probably don't know the size beforehand.

A simpler alternative if y ou use Java 7:

List<String> lines = Files.readAllLines(file, charset);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.