Java is not recognizing an object created as an array

I am creating a simple mechanical computer emulator, with virtual counters and punch cards, but I keep getting errors in java. It creates counters as an array of objects like this:

private static void createcounters(int counternums, int digits,
        int[] countervals){
    for (int i=0; i<counternums; i++){
        try {
            if (digits < 1){
                System.out.println("Invalid number of digits, reverting to 1 digit");
                digits = 1;
            }
            Counter[] counter = null;
            counter[i] = new Counter(digits, countervals[i]);

        } catch (Exception ex) {

        }
    }

}

The object is referenced at a different point in a program to read values and put those in a integer array:

public int[] getcounters(){
    int[] countervals = null;
    for (int i=0; i<counternums; i++){
        countervals[i] = counter[i].ReturnVal;           
    }
    return countervals;
}

Java gives this error on compiliation:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
  symbol:   variable counter
  location: class mechanicalcomputeremulator.Computer
    at mechanicalcomputeremulator.Computer.getcounters(Computer.java:49)
    at mechanicalcomputeremulator.MechanicalComputerEmulator.main(MechanicalComputerEmulator.java:20)
Java Result: 1

If I reference the counter in the method that the objects are created, the error doesn't appear.

Jon Skeet
people
quotationmark

You've got at least four problems:

  • You're declaring the counter variable as a local variable inside your method. I suspect you meant to declare a field somewhere - or return a reference from the method, and assign it to an instance variable there.

  • You're initializing the variable to null and then immediately trying to dereference it. How do you expect that to work?

  • You're declaring a different variable for each iteration of the loop. I strongly suspect you mean to declare one variable and populate the same array in all iterations.

  • You're catching Exception and just ignoring it. Never do that. (Ideally don't do either part of it.)

So sample better code:

private static void createCounters(int counterNums,
                                   int digits,
                                   int[] counterVals) {
    // Moved out of the loop, as it's pointless there.
    if (digits < 1) {
        // TODO: Throw an exception instead?
        System.out.println("Invalid number of digits, reverting to 1 digit");
        digits = 1;
    }
    counter = new Counter[counterNums];
    for (int i=0; i < counterNums; i++) {
        counter[i] = new Counter(digits, counterVals[i]);
    }    
}

Or:

private static Counter[] createCounters(int counterNums,
                                        int digits,
                                        int[] countervals) {
    if (digits < 1) {
        // TODO: Throw an exception instead?
        System.out.println("Invalid number of digits, reverting to 1 digit");
        digits = 1;
    }
    Counter[] counter = new int[counterNums];
    for (int i=0; i < counterNums; i++) {

        counter[i] = new Counter(digits, counterBals[i]);
    }
    return counter;
}

Note that if counterNums is the same as the length of counterVals, you can remove that parameter and just use counterVals.length.

people

See more on this question at Stackoverflow