我在让程序从同一目录中读取输入文件时遇到问题。 代码是在Main中,包括整个只是因为我在main之外做了一些导致这种情况的事情。
import java.io.*;
import java.util.*;
public class Final
{
public static int readData(BusinessDirectory [] array, Scanner input,Scanner inputFile)
{
int lastChar = 0;
int count =0;
int dirChoice = 0;
int area,
exchange,
number,
extension;
String name;
while(inputFile.hasNextLine() && count < array.length)
{
String Og = inputFile.nextLine();
lastChar = (Og.length()-1);
dirChoice = Integer.parseInt(Og.substring(0,1));
if(dirChoice == 1)
{
area = Integer.parseInt(Og.substring(2,5));
exchange = Integer.parseInt(Og.substring(6,9));
number = Integer.parseInt(Og.substring(10,14));
name = Og.substring(15,lastChar);
array[count].DirectorySet(area, exchange, number, name);
}
if(dirChoice == 2)
{
area = Integer.parseInt(Og.substring(2,5));
exchange = Integer.parseInt(Og.substring(6,9));
number = Integer.parseInt(Og.substring(10,14));
extension = Integer.parseInt(Og.substring(15,19));
name = Og.substring(20,lastChar);
array[count].BusinessDirectorySet(area, exchange, number, extension, name);
}
}
return count;
}
public static void main(String[]args)throws IOException
{
String infile;
int count=0;;
//Directory[]array = new Directory[25];
BusinessDirectory[]array = new BusinessDirectory[25];
Scanner in = new Scanner(System.in);
System.out.print("What is the input file: ");
infile = in.next();
try
{
File inputFile = new File(infile);
Scanner fin = new Scanner(inputFile);
readData(array, in, fin);
System.out.println(BusinessDirectory.getName());
// System.out.println("test");
//count = readData(array,in,inputFile);
}
catch(Exception e)
{
System.out.println("\"" + infile + "\" not found. Program will terminate.");
System.exit(0);
}
}
}
它总是从Catch抛出异常。 (“test.txt”未找到。程序将终止。)
e.printStackTrace(); 抓住我
What is the input file: test.txt
java.lang.NullPointerException
"test.txt" not found. Program will terminate.
at Final.readData(Final.java:36)
at Final.main(Final.java:69)
错误似乎出现在我的目录类
中public class Directory
{
//data members
static int Area;
static int Exchange;
static int Number;
static String Name;
static int cause;
public Directory()
{
Area = 999;
Exchange = 999;
Number = 9999;
Name = "";
cause = 0;
}
public Directory(int area, int exchange, int number, String name)
{
DirectorySet(number, number, number, name);
}
public void DirectorySet(int area, int exchange, int number, String name)
{
try
{
if(area >= 200 && area <= 999 && area != 911)
{
if(exchange >= 200 && exchange <= 999 && exchange !=911)
{
if(number >= 0 && number <= 9999)
{
Area = area;
Exchange = exchange;
Number = number;
Name = name;
}else
{
cause = 1;
MyOwnException error = new MyOwnException();
MyOwnException.Message = error.setMessage(cause);
throw error;
}
}else if(exchange == 911 || area == 911)
{
cause = 4;
MyOwnException error = new MyOwnException();
MyOwnException.Message = error.setMessage(cause);
throw error;
}
cause = 2;
MyOwnException error = new MyOwnException();
MyOwnException.Message = error.setMessage(cause);
throw error;
}else
{
cause = 3;
MyOwnException error = new MyOwnException();
MyOwnException.Message = error.setMessage(cause);
throw error;
}
}
catch(MyOwnException error)
{
System.out.println(toString());
System.out.println(MyOwnException.Message);
//System.out.println(Directory.toString());
}
}
public void toString(int area, int exchange, int number, String name)
{
System.out.println(name + " (" + area + ") " + exchange + " -" + number);
}
public String toString()
{
return (Name + " (" + Area + ") " + Exchange + " -" + Number);
}
public static String getName()
{
return Name;
}
public static int getArea()
{
return Area;
}
public static int getExchange()
{
return Exchange;
}
public static int getNumber()
{
return Number;
}
public void setName(String name)
{
Name = name;
}
public void setArea(int area)
{
Area = area;
}
public void setExchange(int exchange)
{
Exchange = exchange;
}
public void setNumber(int number)
{
Number = number;
}
}
...
Final.readData(Final.java:37)
array[count].DirectorySet(area, exchange, number, name);
Final.main(Final.java:73)
readData(array, fin);
答案 0 :(得分:0)
尝试:
File inputFile = new File(infile);
System.out.println(inputFile.getAbsolutePath());
这将为您提供有关工作目录的提示,您可以修改输入(绝对或相对路径)
如果您不确定相对位置并且不希望使用绝对路径,建议您使用File.exists()
API在代码中做出决定。
答案 1 :(得分:0)
你的NullPointerException
似乎被抛到这一行:
array[count].DirectorySet(area, exchange, number, name);
问题是您以这种方式正确创建了array
:
BusinessDirectory[]array = new BusinessDirectory[25];
但是这只创建了25个对象指针的空间,实际上并没有创建25个对象。
你必须逐个创建每个对象,在你的情况下你应该这样做:
array[count] = new Directory(area, exchange, number, name);
而不是:
array[count].DirectorySet(area, exchange, number, name);
此外,您似乎无法在任何地方增加count
。