在另一个类中调用时,此方法不起作用

时间:2012-11-22 02:03:30

标签: java nullpointerexception

这段代码将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);


    }

3 个答案:

答案 0 :(得分: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。

答案 1 :(得分: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”设置变量,则会得到nullpointerexception。 确保在调用WriteToFilecmd之前执行此操作。

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

答案 2 :(得分: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本身为null,或者方法抛出它。通常,Java基础类很好地处理null参数(并且Scanner#next()不会返回null)并且不包含错误,因此X最有可能是null它被使用的那一刻。

如果您的create方法捕获异常(请参阅控制台输出),稍后覆盖X或者您根本没有调用create方法或者您称之为太晚了。

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