0

I am very new to java and I have been stuck on this for a few hours now. Whevenever I try to call organizeBallots() I get the exception. From what I read, I have not initialized regionBallots correctly or that something is null. To me, it looks like I have initialized the array correctly and since its an int, it should be 0, not null. ...But I guess Im wrong, any help is greatly appreciated!

Edit: As I said I am new so Im not sure if it helps, but organizeBallots() is being called by another method in another class.

int[] regionBallots;        
regionBallots = new int[9];



public int[] organizeBallots( int incoming )
{

    if ( incoming >= 1 && incoming <= 10 )
        regionBallots[0]++;    // I get the exception here

    else if ( incoming >= 11 && incoming <= 20 )
        regionBallots[1]++;


    return regionBallots;

}
3
  • 3
    Where are the first two lines located? What method? Commented Apr 13, 2012 at 20:13
  • 1
    What exception you get? NullPointerException? Try to System.out.println(regionalBallots) to check if it is null. Commented Apr 13, 2012 at 20:18
  • the first line is a decleration at the beginning of the class while the second line is in the constructor of the same class. @PiotrKochanski yeah its a NullPointerException. Commented Apr 13, 2012 at 20:33

2 Answers 2

2

Make it like this: int[] regionBallots = new int[9];

You can't write code outside methods, except in initializers. To use an initializer write:

{
  //here the initializing code
}

To use a static initializer write:

static {
    //here the static initializing code
}
Sign up to request clarification or add additional context in comments.

9 Comments

Wouldn't that result in a compiler error, rather than an exception as the OP stated?
-1: this is patently false. Java supports expressions in field initialisers.
@Inerdial - Yes, but he hasn't put the code in an initialiser.
I do Set foo = new HashSet(); to declare fields all the time. That's not where the problem is.
Yes, the OP hasn't given enough info. My guess is he's initializing regionBallots in a constructor but calling organizeBallots before he manages to do that.
|
1

This code worked for me without any issues:

public class T {

    int[] regionBallots;

    public T() {
        regionBallots = new int[9];
    }

    public int[] organizeBallots(int incoming) {

        if (incoming >= 1 && incoming <= 10)
            regionBallots[0]++; //

        else if (incoming >= 11 && incoming <= 20)
            regionBallots[1]++;

        return regionBallots;

    }

    public static void main(String[] args) {
        T t = new T();
        for (int i = 0; i < 100; i++) {         
              System.out.println(t.organizeBallots(i));
              System.out.println(t.organizeBallots(i)[0]);
              System.out.println(t.organizeBallots(i)[1]); 
        }
    }

}

2 Comments

Just out of curiousity, what did you do differently? This looks like exactly what I did haha
Hard to say. I did not see any problem with your code. Try to debug your code to see if regionBallots is not null in organizeBallots method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.