Pleaseee help me guys. I can't figure out why the elements in my array are null. I'm reading in from a file and take each line and saving it as a variable in the object and then adding the object to the array.
This is what I have so far...when i print out the array each element is null :/

Scanner fileIn=null;
		try{
			fileIn=new Scanner(new File("flights.txt"));
			
		}catch(FileNotFoundException e){
			System.out.print("Cannot find file.");
			System.exit(-1);
		}
		
		int count=0;
		while(fileIn.hasNext()) {
			  fileIn.nextLine();
			  count++;
			}
		
		listFlights=new Flight[count/3];
		num=listFlights.length;
		int i=0;
		String d;
		String n;
		int h;
		int m;
		while(fileIn.hasNextLine()){
			i++;
			n=fileIn.nextLine();
			d=fileIn.nextLine();
			h=fileIn.nextInt();
			m=fileIn.nextInt();
			
			System.out.print(i);
			Time time=new Time(h, m);
			listFlights[i]=new Flight(n, d, time);		
		}

The file looks like

Fli815
The_Island
8 15
UA302Y
Chinle
8 14
TI2745
Ganado
14 22
DL762A
Dinne
2 45

Recommended Answers

All 21 Replies

Pleaseee help me guys. I can't figure out why the elements in my array are null. I'm reading in from a file and take each line and saving it as a variable in the object and then adding the object to the array.
This is what I have so far...when i print out the array each element is null :/

Scanner fileIn=null;
		try{
			fileIn=new Scanner(new File("flights.txt"));
			
		}catch(FileNotFoundException e){
			System.out.print("Cannot find file.");
			System.exit(-1);
		}
		
		int count=0;
		while(fileIn.hasNext()) {
			  fileIn.nextLine();
			  count++;
			}
		
		listFlights=new Flight[count/3];
		num=listFlights.length;
		int i=0;
		String d;
		String n;
		int h;
		int m;
		while(fileIn.hasNextLine()){
			i++;
			n=fileIn.nextLine();
			d=fileIn.nextLine();
			h=fileIn.nextInt();
			m=fileIn.nextInt();
			
			System.out.print(i);
			Time time=new Time(h, m);
			listFlights[i]=new Flight(n, d, time);		
		}

The file looks like

Fli815
The_Island
8 15
UA302Y
Chinle
8 14
TI2745
Ganado
14 22
DL762A
Dinne
2 45

They are null because you set it too null. When you initialised fileIn=new Scanner(new File("flights.txt")); that is only visible inside your try..catch block. Therefore, the fileIn you use throughout the program is actually null.

Wait..so how do i fix it? :/

There are a number of ways, if you are lazy, you could forget about the try..catch and just declare and initialise at the top. Or you could put your code in a function, and pass the fileIn as a parameter.
e.g.

public static void main(String args[]){
    
		try{
			Scanner fileIn=new Scanner(new File("flights.txt"));
                        getFile(fileIn);

		}catch(FileNotFoundException e){
			System.out.print("Cannot find file.");
			System.exit(-1);
		}
    }
    public static void getFile(Scanner f)
    {

		int count=0;
		while(f.hasNext()) {
			  f.nextLine();
			  count++;
			}

		listFlights=new Flight[count/3];
		num=listFlights.length;
		int i=0;
		String d;
		String n;
		int h;
		int m;
		while(f.hasNextLine()){
			i++;
			n=f.nextLine();
			d=f.nextLine();
			h=f.nextInt();
			m=f.nextInt();

			System.out.print(i);#
                        Time time=new Time(h, m);
                        listFlights[i]=new Flight(n, d, time); 

		}
    }

edit: I only put the main method there so I could test it.

commented: He took the time to help me +1

Now its giving me the Null Pointer exception on this line:
Hmm..

//searches flightList for correct flight
	public boolean booking(Passenger x){

		for(int i=0; i<num; i++){

			System.out.print("Hiii");
			if(listFlights[i].bookPassenger(x)){
				return true;
			}

		}

		return false;
	}

There isn't much I can do with snippets of your code. Are you sure there is a passenger object being passed? Do a check to see if it is null.
Try and post more of your code

I printed out the parameter variables and they weren't null.

This is the function it calls

//takes in Passenger param, determines if person can be booked on flight
	public boolean bookPassenger(Passenger x){
		
		if(!x.getDestination().equals(dest)){
			System.out.print("Cannot be booked");
			return false;
		}
			
		Time arrival=x.getArrival();
		if(arrival.compareTo(depart)>-1){
			System.out.print("Cannot be booked.");
			return false;
		}

		//check if flight is full
		else if(numPass==maxSeats){
			System.out.print("Cannot be booked.");
			return false;
		}
		
		numPass++;
		Passenger[] copy=new Passenger[numPass];
		for(int i=0; i<numPass; i++)
			copy[i]=Passengers[i];
		
		copy[numPass]=x;
		Passengers=copy;
		
		System.out.println("Passenger has been booked. Passenger #:" + numPass);
		return true;
	}

I printed out

System.out.print(listFlights[i]);

and it still says it's null even though i added the getFile function and in the while loop assigned the listflight array

while(f.hasNextLine()){
			i++;
			n=f.nextLine();
			d=f.nextLine();
			h=f.nextInt();
			m=f.nextInt();

			Time one=new Time(h, m);
			listFlights[i]=new Flight(n, d, one);
			System.out.print(listFlights[i]);
		}

It's still saying that my listFlights is null and I added the code you wrote

Try making a small "test" program, that will read the lines of your file, put them in an array, and then print the array. (Use a string array). Just to make sure it works.
Or, in that while loop, try printing out n,d,h and m. Just to make sure it is getting the lines from the file properly

It seems as if my program is not even going into this loop. No hello is outputted to the screen

while(f.hasNextLine()){
			System.out.print("hello");
			i++;
			n=(String)f.nextLine();
			d=(String)f.nextLine();
			h=f.nextInt();
			m=f.nextInt();

			Time one=new Time(h, m);

Ok, that means it is not accessing your file correctly. Try declaring your Scanner declaration globally, so we can be sure it is nothing to do with scope.
Scanner fileIn = new Scanner(new File("flights.txt"));
Then of course change the f back to fileIn. (just comment out the method braces).

In Fact, print out the value of count, after this loop:

int count=0;
		while(fileIn.hasNext()) {
			  fileIn.nextLine();
			  count++;
			}

count say 27 which is correct for the number of lines
what do you mean globally? When i put this line Scanner fileIn = new Scanner(new File("flights.txt")); alone in my readFlightData method is highlighted it and gave me a FileNotFound exception

Globally means declare it above all methods, but inside the class braces. So right at the top.

Ok i put it at the top and now there's no error there but it's still not reading the file correctly.
It doesn't go in the while loop

while(fileIn.hasNextLine()){

				i++;
				n=(String)fileIn.nextLine();
				d=(String)fileIn.nextLine();
				h=fileIn.nextInt();
				m=fileIn.nextInt();

				System.out.print(n);
				Time one=new Time(h, m);
				listFlights[i]=new Flight(n, d, one);
			}

Ok, change your while loop to a do..while loop, so it will enter at least once. And then print the values of n and d. Try setting the size of the Array to 27. Instead of count/3. (count/3 = 27/3 which is 9) so your array size is 9.

If that doesn't produce anything helpful, then create a new test file, with your Scanner declaration, a String array, and the same loop. And make sure you can print the array, this will tell you if your methodology of doing this problem actually works.

Nothing is printed out, not even the "hi"

do{
				System.out.print("hi");
				i++;
				n=(String)fileIn.nextLine();
				d=(String)fileIn.nextLine();
				h=fileIn.nextInt();
				m=fileIn.nextInt();

				Time one=new Time(h, m);
				listFlights[i]=new Flight(n, d, one);
			}while(fileIn.hasNextLine());

The code must be breaking before you get to the loop then, put a print statement before the loop and after any declaration you made that could stop the program

Oh wow i made a really stupid mistake...left off a bracket in the catch which was why it was skipping all the code below it.

Now it's giving me a NoSuchElementException on n=(String)fileIn.nextLine();

Oh wow i made a really stupid mistake...left off a bracket in the catch which was why it was skipping all the code below it.

Now it's giving me a NoSuchElementException on n=(String)fileIn.nextLine();

Ahhhh :) I knew there was something not right above!


I also just read something that was quite interesting:

"Never use java.util.Scanner for general input purpose. It could bring any kind of weird resluts and behavior because it is basically a tokenizer, not an input device. Use java.io.BufferedReader for line read of a file and String.split() method for retrieving each element of a line."

Ok, disregard that part about the fileReader. But I did notice something else, why do you cast n as a String? it is already a String and nextLine() returns a String, take out that cast because it may be causing a problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.