簡體   English   中英

空指針異常,比較java中的兩個字符串

[英]null pointer exception comparing two strings in java

我收到此錯誤消息,但我不太確定出了什么問題:

Exception in thread "main" java.lang.NullPointerException
    at Risk.runTeams(Risk.java:384)
    at Risk.blobRunner(Risk.java:220)
    at Risk.genRunner(Risk.java:207)
    at Risk.main(Risk.java:176)

這是代碼的相關位(我將通過代碼中的注釋以及在程序運行時輸入到程序中的輸入來提請注意錯誤消息中的行號)

public class Risk
{

...

public static void main (String[]arg) 
{
    String CPUcolor = CPUcolor () ; 
    genRunner (CPUcolor) ; //line 176

...

}

...

public static void genRunner (String CPUcolor) // when this method runs i select 0 and run blob since its my only option. Theres nothing wrong with this method so long as i know, this is only significant because it takes me to blob runner and because another one of our relelvent line numbers apears. 
{
    String[] strats = new String[1] ; 
    strats[0] = "0 - Blob" ;
    int s = chooseStrat (strats) ;
    if (s == 0) blobRunner (CPUcolor) ; // this is line 207 
}

...

public static void blobRunner (String CPUcolor) 
{ 
    System.out.println ("blob Runner") ; int turn = 0 ; boolean gameOver = false ; 
    Dice other = new Dice ("other") ; 
    Dice a1 = new Dice ("a1") ; Dice a2 = new Dice ("a2") ; Dice a3 = new Dice ("a3") ;
    Dice d1 = new Dice ("d1") ; Dice d2 = new Dice ("d2") ; 
    space (5) ; 
    Territory[] board = makeBoard() ; 
    IdiceRoll (other) ; 
    String[] colors = runTeams(CPUcolor) ; //this is line 220 
    Card[] deck = Card.createDeck () ;
    System.out.println (StratUtil.canTurnIn (deck)) ; 

    while (gameOver == false)
    {
        idler (deck) ; 
        board = assignTerri (board, colors) ; 
        checkBoard (board, colors) ; 
    }
} 

...

public static String[] runTeams (String CPUcolor) 
{  
    boolean z = false ; 
    String[] a = new String[6] ; 
    while (z == false) 
    { 
        a = assignTeams () ; 
        printOrder (a) ;
        boolean CPU = false ; 
        for (int i = 0; i<a.length; i++) 
        { 
            if (a[i].equals(CPUcolor)) CPU = true ; //this is line 384
        }
        if (CPU==false) 
        {
            System.out.println ("ERROR YOU NEED TO INCLUDE THE COLOR OF THE CPU IN THE TURN ORDER") ; 
            runTeams (CPUcolor) ;
        }
        System.out.println ("is this turn order correct? (Y/N)") ; 
        String s = getIns () ; 
        while (!((s.equals ("y")) || (s.equals ("Y")) || (s.equals ("n")) || (s.equals ("N")))) 
        {
            System.out.println ("try again") ; 
            s = getIns () ; 
        } 
        if (s.equals ("y") || s.equals ("Y") ) 
        z = true ; 
    } 
    return a ; 
}

...

} // This } closes the class 

我不認為應該得到Null:pointerException的原因是因為在這一行: a[i].equals(CPUcolor) a at index i擁有一個字符串,而CPUcolor是一個字符串。 在這點上,兩者肯定都有一個值都不為null。 誰能告訴我怎么了?

您應該看一下方法assignTeams() 對於i某個值, a[i]必須為null。 CPUcolor是否為null無關緊要,該方法equals處理。

使用調試器並單步執行代碼,在第384行放置一個斷點。這應該告訴您什么地方不對。

AssignTeams()返回帶有空條目的數組。 或其中至少一個為空。 您應該檢查一下。 您可以調試代碼嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM