0

I have an int variable that is set a certain value, when a JButton is clicked on the GUI the buttons visibility is set to false, and the counter variable should decrease by one. Once it hits 0 an if statement is executed. The problem is the counter variable keeps getting reset everytime I click a button. This is my code

public int someMethod(){
    intVar= 3;
    return intVar;
}

public void anotherMethod(){
    intVar--;
}
public void actionPerformed(ActionEvent e) {
if (someCondition){
            clickedButton.setVisible(false);
            anotherMethod();
            if (someMethod()==0){
  //Do something
  }
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
ugCode
  • 29
  • 3
  • 1
    `The problem is the counter variable keeps getting reset everytime ` Why does your `someMethod()` always reset the variable to 3? – camickr Dec 26 '15 at 20:21
  • `so intVar really equals myarraylist.size().` - then you should be using myArrayList.size() and get rid of the variable. There is no need for you to keep duplicate information. It causes problems as you can see. – camickr Dec 26 '15 at 20:41
  • It works as a counter though, I need to store what number its at so i can use the if statement I posted a pastebin of my actual code below – ugCode Dec 26 '15 at 20:44
  • The ArrayList method is your counter. Just invoke the method every time you want to check how many items are in the ArrayList. – camickr Dec 26 '15 at 20:53

2 Answers2

3

Your someMethod() resets the counter to 3. Thus it will always return 3. Thus your if block will never be executed.

someMethod() is logically equivalent to the following, barring any concurrency shenanigans:

public int someMethod() {
    intVar = 3;
    return 3;
}

Thus your actionPerformed(...) is logically equivalent to the following (ibid):

public void actionPerformed(ActionEvent e) {
    if (someCondition){
        clickedButton.setVisible(false);
        anotherMethod();
        if (3==0){
            //Do something
        }
    }
}

If the counter should just decrease, and the if block should be executed when intVar == 0 just use the intVar directly in your comparison:

public int anotherMethod(){
    return --intVar;
}

public void actionPerformed(ActionEvent e) {
    if (someCondition){
        clickedButton.setVisible(false);
        anotherMethod();
        if (intVar==0){
            //Do something
        }
    }
}

Or, remove the assignment from someMethod():

public int someMethod() {
    return intVar;
}

Or, get rid of someMethod() call here and return the updated counter value from 'anotherMethod()' to change code to:

public int anotherMethod(){
    return intVar--;
}

public void actionPerformed(ActionEvent e) {
    if (someCondition){
        clickedButton.setVisible(false);
        if (anotherMethod()==0){
            //Do something
        }
    }
}
Mshnik
  • 7,032
  • 1
  • 25
  • 38
1

Its because in method someMethod() you always end up resetting the variable to 3. Instantiate the variable outside the method only once.

Another interesting fact (not related to this), Integers from -127 to +128 are stored in the integer pool (cached) and any reference to value in the range is done to object in pool. (Applicable for Integer only and not ints)

  • I believe that the integer pool information is for Integer objects only and not ints, and so does not apply to this application. 1+ for the other piece of information though. – Hovercraft Full Of Eels Dec 26 '15 at 20:31
  • I actually have another method, where inside that method it reads data from another class,it takes an array size.so intVar really equals myarraylist.size(). When i instantitae it outside a method and use it inside an if statement i get null pointer – ugCode Dec 26 '15 at 20:32
  • Your second paragraph has no connection to anything in the question. There was no question about memory consumption, and there is no reason to assume that any of the variables in that code is of type `Integer` rather than `int`. – RealSkeptic Dec 26 '15 at 20:32
  • @HovercraftFullOfEels Thanks for correcting me here. – Aaditya Gavandalkar Dec 26 '15 at 20:34
  • @RealSkeptic I did mention that it was not related to this question and I was just trying to share the info. Not sure why it hurts (corrected info) – Aaditya Gavandalkar Dec 26 '15 at 20:35
  • @ugCode: then it's time to debug that NullPointer. Search this site for how to do that. Edit: look here: [NullPointerExceptions](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it/218510#218510). – Hovercraft Full Of Eels Dec 26 '15 at 20:36
  • 1
    Answers should be just that: answers. They should not contain information that is irrelevant to the question. Share that information in answers to questions about the `Integer` type when the information is relevant. Irrelevant information is confusing, may damage the ability to search for the answer properly, and is not helpful. – RealSkeptic Dec 26 '15 at 20:40
  • @RealSkeptic Never looked at it from that perspective but would agree to it till some extent. Thanks :) – Aaditya Gavandalkar Dec 26 '15 at 20:44