-3

这段代码将 X 初始化为一个新的格式化程序变量。

 private Formatter X;
     public void Create() {
                    try{
                        X = new Formatter("users.txt");
                        X.format("%1$20s %2$20s %3$20s %4$20s %5$20s %6$20s %7$20s %8$20s %9$20s \n","Firstname","Lastname","Password","ID","Addressln1","Addressln2","Addressln3","EstimatedValue","Tax Owed");
                        System.out.println("Due to there not being any users file present one has been created.");
                    }
                    catch(Exception e){
                        System.out.println("There is a users file present so i will not create another one.");
                    }
                }
            }

这是我遇到问题的一段代码,在 X.format 位,它一直给我一个空指针错误,我已经尝试了一切来修复它。

public void WriteToFilecmd(){
                Scanner input = new Scanner(System.in);
                System.out.println("Please enter your First name");
                String Fname = input.next();
                System.out.println("Please enter your Last name");
                String Lname = input.next();
                System.out.println("Please enter your Password");
                String Password = input.next();
                System.out.println("Please enter your user ID");
                String ID = input.next();
                System.out.println("Please enter the first address line of your Property");
                String addressln1 = input.next();
                System.out.println("Please enter the second address line of your Property");
                String addressln2 = input.next();
                System.out.println("Please enter the third address line of your Property");
                String addressln3 = input.next();
                System.out.println("Please enter the properties estimated market value");
                String EstimatedPropertyValue = input.next();
                System.out.println("Please enter your tax owed");
                String Taxowed = input.next();
                input.close();
                X.format("%1$20s %2$20s %3$20s %4$20s %5$20s %6$20s %7$20s %8$20s %9$20s \n",Fname,Lname,Password,ID,addressln1,addressln2,addressln3,EstimatedPropertyValue,Taxowed);


    }
4

3 回答 3

0

密切关注这一行:

X.format("%1$20s %2$20s %3$20s %4$20s %5$20s %6$20s %7$20s %8$20s %9$20s \n",Fname,Lname,Password,ID,addressln1,addressln2,addressln3,EstimatedPropertyValue,Taxowed);

变量X从何而来?如果它定义在高于此方法的级别(可能是类),则其中的值为null,并且您不能取消引用 null。

于 2012-11-22T02:12:23.453 回答
0

确保 X(无论它是什么)已被初始化。

如果您不知道这意味着什么,例如:

Scanner scanner; // not initialized
Scanner scanner = new Scanner(System.in); // initialized

不过,您不必在声明中初始化变量。这也将初始化变量:

Scanner scanner; // not initialized
scanner = new Scanner(System.in); // good job you init'd it!

如果你不使用“new”设置变量,你会得到空指针异常。确保在调用 WriteToFilecmd 之前执行此操作。

显然,不要将你的x变量设置为“new Scanner(System.in)”,调用X类型的构造函数。

于 2012-11-22T02:16:44.497 回答
0

NullPointerException在线上

X.format("%1$20s %2$20s %3$20s %4$20s %5$20s %6$20s %7$20s %8$20s %9$20s \n",Fname,Lname,Password,ID,addressln1,addressln2,addressln3,EstimatedPropertyValue,Taxowed);

意味着要么它X本身为空,要么方法抛出它。通常,Java 基础类null可以很好地处理参数(并且Scanner#next()无论如何都不返回 null)并且不包含错误,因此X目前很可能null正在使用它。

如果您的create方法捕获异常(请参阅控制台输出)、X稍后覆盖或根本不调用该create方法或调用它太晚,则可能会发生这种情况。

另请注意,口袋妖怪异常处理(“必须全部捕获” catch(Exception e))是一种不好的做法。抓住你能处理的,而不是你能抓住的。

于 2012-11-22T02:19:25.320 回答