2012-11-22 85 views
-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); 


    } 
+0

請附上您的堆棧跟蹤 – tanyehzheng

+3

您分配給甚至定義「X」的位置?當然不在這個代碼中。它有點歸結於此。 –

+0

向我們展示如何 - 何時 - 創建X –

回答

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))是一種不好的做法。抓住你能處理的東西,而不是你能抓住的東西。

+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。

+0

x是一個名爲users.txt的文本文件的新格式化程序 –

+0

'x'是明確定義的。否則,你會得到一個編譯時錯誤。 –

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類型的構造函數。

+0

更正式地說,你的線條被「聲明」和「聲明和初始化」。但你幾乎可以看到。 – Makoto

+0

@Makoto相當多的地方?我要買它! – Aaron

相關問題