0

我在让我的程序从同一目录读取输入文件时遇到问题。代码在 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);
4

2 回答 2

0

尝试:

File inputFile = new File(infile);
System.out.println(inputFile.getAbsolutePath());

这会给你一个关于你的工作目录的提示,你可以修复你的输入(绝对或相对路径)

此外,如果您不确定相对位置并且不希望使用绝对路径,建议您使用File.exists()API 在您的代码中做出决定。

于 2013-05-02T08:15:29.657 回答
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任何地方增加。

于 2013-05-02T08:27:01.967 回答