在变量初始化中使用Null,Java

时间:2016-07-01 20:14:44

标签: java arrays null

String[] tempStrArry, tempStrArry2;
int[] playerOneBase = null, playerTwoBase = null;

我什么时候需要使用= null部分?在String数组上没有问题,但是当我使用整数数组时,netbeans希望我将它们初始化为null。

如果需要,我可以发布更多代码但是数组的使用方式类似,所以我认为它们没有任何影响。

编辑:小部分是错误开始发生的地方,但是大部分是到目前为止的整个代码。

for (int j = 0; j < tempStrArry.length; j++) {
    playerOneBase[j] = Integer.parseInt(tempStrArry[j]);
}

public class GameMain {
    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        // Variables
        int tempInt, tempInt2;
        String tempString, tempString2;
        String[] tempStrArry, tempStrArry2;
        int[] playerOneBase = null, playerTwoBase;
        int[] playerOneCurrent = null, playerTwoCurrent;
        String classStats = "RangeClasses.txt";
        //Constructors
        Scanner userInput = new Scanner(System.in);

        try {
            ReadFile classFile = new ReadFile(classStats);
            String[] aryLines = classFile.OpenFile();

            int i;
            for (i = 0; i < aryLines.length; i++) {
                System.out.println(aryLines[i]);
            }

        /* 
        Prompt user
        Get Input
        Input from string to int
        increment integer (stat Line)
        store stat line as string
        split stat line into string array
        *****make string array to int array
        make base = current
        */
            System.out.println("Player One Choose Class");
            tempString = userInput.next();
            tempInt = Integer.parseInt(tempString);
            tempInt++;
            tempString = aryLines[tempInt];
            tempStrArry = tempString.split(",", 4);
            for (int j = 0; j < tempStrArry.length; j++) {
                playerOneBase[j] = Integer.parseInt(tempStrArry[j]);
            }
            playerOneCurrent = playerOneBase;

            System.out.println("Player Two Choose Class");
            tempString = userInput.next();
            tempInt = Integer.parseInt(tempString);
            tempInt++;


        } catch (IOException e) {
            System.out.println(e.getMessage());
        }

        while (playerOneCurrent[0] > 0) {
        }
    }
}

0 个答案:

没有答案