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) {
        }
    }
}
8
  • 3
    Actually, the rest of the code does have an impact. Could you post in with a minimal reproducible example? See also stackoverflow.com/questions/26160236/… Commented Jul 1, 2016 at 20:15
  • If it's a local variable, you have to definitely assign a value to a variable before you can refer to it in a non-assignment expression. Commented Jul 1, 2016 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. Commented Jul 1, 2016 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 . Commented Jul 1, 2016 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. Commented Jul 1, 2016 at 20:32

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.