2

I'm just starting with Java and I'm having trouble with the following code. I was using something like this to call the non-static apply method from a static method, but I dont think its very efficient. I set an array list of the rules that need to be applied but I can't get it to work.

    ClassificationRule rules = new RuleFirstOccrnc();
    ClassificationRule rules1 = new RuleOccrncCount();
    rules.apply(aUserInput);
    rules1.apply(aUserInput); 

I'm getting this error when trying to call the apply() method from ClassificationRule "The method apply(String) is undefined for the type ArrayList". Any help would be greatly appreciated!

package tweetClassification;

import java.util.ArrayList;

public class PrioritRuls {

    //Set of rules to be applied
    final static ArrayList<ClassificationRule> rulesA
        = new ArrayList<ClassificationRule>();

    static{
        rulesA.add( new RuleFirstOccrnc() );
        rulesA.add( new RuleOccrncCount() );
    }

    // ******************************************* 
    public static void prioritize( final String aUserInput ){

        rulesA.apply(aUserInput); //ERROR
       // The method apply(String) is undefined
       // for the type ArrayList<ClassificationRule>
        }
} 
package tweetClassification;

public class ClassificationRule {

     // *******************************************
     public void apply (final String aUserInput) {  

        apply( aUserInput );
        }
}

2 Answers 2

3

Right, because you're calling the apply method on the array list object, not the contents of the array list.

Change it to something like

rulesA.get(0).apply()

Or, if you want to call it on every element, you need to iterate through the list.

for (ClassificationRule rule:rulesA){
  rule.apply(aUserInput);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the fast response that makes perfect sense. Greatly appreciated!
1

You are trying to invoke apply() on the ArrayList instead on the ClassificationRule object. ArrayList does not have this method, so as expected - you get a compilation error.

You might want to iterate the ArrayList and apply() on each ClassificationRule object with a for-each loop:

for (ClassificationRule rule : rulesA) rule.apply(aUserInput)

or to apply() on a specific element:

rulesA.get(someIndex).apply(aUserInput)

One more thing:

public void apply (final String aUserInput) {  
   apply( aUserInput );
}

will cause an infinite recursive calls to apply() [well, not exactly infinite, it will throw eventually an exception]. This is not the error you are currently having, since this is run-time error, while you are still stuck at compile-time errors.

2 Comments

You are most welcome @tom3322, and good luck! don't forget to accept one of the answers later.
about apply() how do you suggest I fix that, i'm not getting any errors.. I'm trying to call the non-static apply() method of the rules using inheritance.

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.