Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Cali Beneke
Courses Plus Student 1,352 PointsFailed to open data.txt Exception in thread "main" java.lang.NullPointerException at Lab1.java.Lab1.main(Lab1.java:41)
I keep getting this error and I have no idea why. Is it because my code is wrong or is it because it isn't able to connect to the data.txt file for another reason?
Thank you!
package Lab1.java;
import java.util.Scanner;
import java.io.File;
/**
* Lab1.java <br>
* Lab1 class reads the data.txt file in and calculates the number of doubles the file contains, <br>
* the total sum of all the doubles and integers in the file <br>
* and calculates the number of non-numbers in the file.
*
* @author Cali
*
*/
public class Lab1 {
/**
*
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String currentString = null;
int numDoubles = 0;
int numNotNumbers = 0;
double sumNumbers = 0;
//File file = new File("data.txt");
Scanner in = null;
try{
in = new Scanner(new File("data.txt"));
while(in.hasNext()){
if(in.hasNextInt()){
sumNumbers += in.nextInt();
} else if(in.hasNextDouble()){
numDoubles++;
sumNumbers += in.nextDouble();
} else {
currentString = in.next();
numNotNumbers++;
}
}
} catch (Exception FileNotFoundException){
System.err.println("Failed to open data.txt");
} finally {
in.close();
System.exit(1);
}
System.out.println(numDoubles + " " + numNotNumbers + " " + sumNumbers);
}
}
2 Answers
Steve Hunter
57,712 PointsI've copied your code and this is just going straight to the finally block. There, in is bound to be null as that is what it has been set to.
This means that the line in = new Scanner(new File("data.txt")); is failing to put anything inside in.
I'll see if I can break that line down to see where the error exists.
Steve Hunter
57,712 PointsHi there,
Can you let me know which line is numbered 41?
Is data.txt in the same directory as your code file?
Steve.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsOK - I've run this in debug mode to see where the code gets to.
The code works, to an extent. It adds up the totals of the data file that I put together, and correctly works out whether the number is an integer or a double.
It then reaches the end of the data file, moving code execution to the
finallyblock. In that block, the code callsSystem.exit(1);so execution stops!Move the
System.exit(1);out of thefinallyblock and put it after the current last line like this:And you'll get your output. Alternatively, just delete that line; code exits anyway as it runs out of lines!
I hope this works as you intended! The output is a little odd but I'm not parsing the same data file as you. If you copy the data file in here, I can test it with that.
Steve.