*注意:我有指向所有已创建对象的指针。 read在代码的构造函数中初始化,就像写一样。所以我仍然对为什么发生异常感到困惑。 首先,我提前为可能存在的脏和未定义的编码道歉。我没有从事这个职业,也没有学习计算机科学。我试图制作一个(相对)简单的程序来跟踪一些统计数据。其他一切都有效,但是当我尝试将收集的统计信息写入文本文件时,我会在readFile()函数的第一个read.nextLine()中获得nullpointerexception。我们的想法是首先读取文件中已存在的值,将这些值添加到程序会话期间收集的值,然后将组合值重写到文件中。以下是完整的文件:
`import java.util.Scanner; import java.io。*;
公共类StatTracker {
private static int numCustomers = 0, BOGO = 0, PERC = 0, BDAY = 0;
private static final String[] couponTypes = {"BOGO","PERC","BDAY"};
private static boolean run = true;
private static final Scanner input = new Scanner(System.in);
private static final String fileName = "Statistics.txt";
private static PrintWriter write;
private static Scanner read;
public StatTracker() throws IOException, FileNotFoundException{
Scanner read = new Scanner(fileName);
PrintWriter write = new PrintWriter(fileName);
} //End constructor
public static void main(String[] args) throws IOException {
StatTracker stat = new StatTracker();
stat.program(run);
} //End main
public static void program(boolean r) throws IOException{
while (r){
getInfo();
System.out.println("Continue? (y/n): ");
cont(input.next());
} //End while
} //End program
public static int[] readFile() throws FileNotFoundException{
int custTemp = 0, BOGOtemp = 0, PERCtemp = 0, BDAYtemp = 0;
read.nextLine();
custTemp = read.nextInt();
read.nextLine();
BOGOtemp = read.nextInt();
read.nextLine();
PERCtemp = read.nextInt();
read.nextLine();
BDAYtemp = read.nextInt();
read.close();
int[] currentValues = {custTemp,BOGOtemp,PERCtemp,BDAYtemp};
return currentValues;
} //End readFile
public static void writeToFile(int[] vals) throws IOException{
int custTemp = getNumCustomers() + vals[0], BOGOtemp = getBOGO() + vals[1], PERCtemp = getPERC() + vals[2], BDAYtemp = getBDAY() + vals[3];
write.println("Customers:");
write.println(custTemp);
write.println("Buy One Get One Discounts Used:");
write.println(BOGOtemp);
write.println("Percent Off Discounts Used:");
write.println(PERCtemp);
write.println("Birthday Discounts Used:");
write.println(BDAYtemp);
write.close();
} //End writeToFile
//Checks whether to continue running the program
public static void cont(String answer) throws IOException, FileNotFoundException {
int[] temp = new int[4];
if (answer.equals("y")) {
System.out.println("Program still running. Current totals:");
System.out.println("Customers: " + getNumCustomers());
System.out.println("BOGO coupons used: " + getBOGO());
System.out.println("Percent off coupons used: " + getPERC());
System.out.println("Birthday coupons used: " + getBDAY());
} else if (answer.equals("n")){
System.out.println("Writing statistics to file.");
temp = readFile();
writeToFile(temp);
System.out.println("Closing program.");
switchRun();
} //End if
} //End cont
public static int getBOGO(){
return BOGO;
} //End getBOGO
public static int getPERC(){
return PERC;
} //End getBOGO
public static int getBDAY(){
return BDAY;
} //End getBOGO
public static int getNumCustomers(){
return numCustomers;
} //End getNumCustomers
public static void switchRun(){
if (getRun()){
run = false;
} else {
run = true;
} //End if
} //End switchRun
public static boolean getRun(){
return run;
} //End getRun
public static String getCouponType(int index){
return couponTypes[index];
} //End getCouponType
public static void getInfo(){
int temp = 0, BOGOtemp = 0, PERCtemp = 0, BDAYtemp = 0;
boolean checkTemp = false;
System.out.println("Enter the number of customers:");
temp = input.nextInt();
addToNumCustomers(temp);
while (true){
System.out.println("If any coupons were used, please enter the number of each in this order:");
System.out.println(getCouponType(0)+ ", " + getCouponType(1) + ", " + getCouponType(2));
BOGOtemp = input.nextInt();
PERCtemp = input.nextInt();
BDAYtemp = input.nextInt();
if (checkTooMany(BOGOtemp,PERCtemp,BDAYtemp,temp)){
System.out.println("More coupons than customers. Please re-enter.");
} else {
addToCoupons("BOGO", BOGOtemp);
addToCoupons("PERC", BOGOtemp);
addToCoupons("BDAY", BOGOtemp);
break;
}//End if
}
} //End getInfo
public static boolean checkTooMany(int b, int p, int bd, int c){
//Checks to make sure there aren't more coupons than people.
if((b + p + bd) > c) {
return true;
} else {
return false;
} //End if
} //End check
public static void addToNumCustomers(int num){
numCustomers += num;
} //End addNumCustomers
public static void addBOGO(int num){
while(true){
if(num >= 0){
BOGO += num;
break;
} else {
System.out.println("Invalid number, please re-enter.");
num = input.nextInt();
}//End if
} //End while
} //End addBOGO
public static void addPERC(int num){
while(true){
if(num >= 0){
PERC += num;
break;
} else {
System.out.println("Invalid number, please re-enter.");
num = input.nextInt();
}//End if
} //End while
} //End addPERC
public static void addBDAY(int num){
while(true){
if(num >= 0){
BDAY += num;
break;
} else {
System.out.println("Invalid number, please re-enter.");
num = input.nextInt();
} //End if
} //End while
} //End addBDAY
public static void addToCoupons(String type,int num){
while(true){
if (type.equalsIgnoreCase(getCouponType(0))){
addBOGO(num);
break;
} else if (type.equalsIgnoreCase(getCouponType(1))){
addPERC(num);
break;
} else if (type.equalsIgnoreCase(getCouponType(2))){
addBDAY(num);
break;
} else {
System.out.println("Invalid coupon type, please re-enter.");
type = input.nextLine();
} //End if
} //End while
} //End addToCoupons
} //结束StatTracker`
我尝试从程序本身创建一个新文件(使用File新文件),创建一个空的.txt文件,在每个必要的行上创建一个包含垃圾数据的文件,并在每一行创建一个空格。我们非常感谢您提供的任何帮助。