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

When do I need to use the =null part? On the String arrays no problems occur but when I use the integer arrays netbeans wants me to initialise them as null.

I can post more of my code if need be but the arrays are used in similar ways so I don't think they have any impact.

Edit: The small part is where the errors start to occur but the large part is the whole code so far.

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) {
        }
    }
}
bcsb1001
  • 2,834
  • 3
  • 24
  • 35
  • 3
    Actually, the rest of the code does have an impact. Could you post in with a [mcve]? See also http://stackoverflow.com/questions/26160236/difference-between-local-variable-initialize-null-and-not-initialize – Tunaki Jul 01 '16 at 20:15
  • If it's a local variable, you have to [*definitely assign*](https://docs.oracle.com/javase/specs/jls/se8/html/jls-16.html) a value to a variable before you can refer to it in a non-assignment expression. – Andy Turner Jul 01 '16 at 20:18
  • 2
    My guess is these variables are local (class members default to null), and you have a path in your code which doesn't initialize the int arrays, whereas the string arrays are always initialized later. – shmosel Jul 01 '16 at 20:18
  • From what I'm seeing now, your code doesn't even attempt to initialize `playerOneBase`. That'll give you a NullPointerException in the loop . – shmosel Jul 01 '16 at 20:26
  • int[] playerOneBase; <- Doesn't that initialize it? I don't attempt to read from it until after I give it values. For now I'll just be a good little coder and use it whenever I get an error. – followmylogic Jul 01 '16 at 20:32
  • That's declaring it, not initializing it. – shmosel Jul 01 '16 at 20:40
  • So declaring is reserving space and Initializing is putting something in that space. And unless I intend on filling the space with something specific the moment I declare it, I should initialize it as null? – followmylogic Jul 01 '16 at 20:45
  • You don't have to initialize it the moment you declare it. The compiler just needs to know it'll be initialized before you read from it. – shmosel Jul 07 '16 at 07:03

0 Answers0