0

I have the code below:

public class PatientAgent extends Agent {

  private final String HYPHEN = "-";

  private ArrayList<HashSet<Integer>> prefs;

  private AID provider ;

  private boolean hasAppointment;

  private int appointmentNo;

  @Override
  protected void setup() {

      hasAppointment = false;

      appointmentNo = 0; // A value of zero means agent does not have any 
                         // allocated appointments (yet)

      initPrefs(getArguments());

      System.out.println(prefs.toString());

      // Build the description used as template for the subscription
      DFAgentDescription template = new DFAgentDescription();
      ServiceDescription templateSd = new ServiceDescription();
      templateSd.setType("allocate-appointments");
      template.addServices(templateSd);


      SearchConstraints sc = new SearchConstraints();
      // We want to receive 10 results at most
      sc.setMaxResults(new Long(10));

      addBehaviour(new SubscriptionInitiator(this, DFService.createSubscriptionMessage(this, getDefaultDF(), template, sc)) {
          protected void handleInform(ACLMessage inform) {
              System.out.println("Agent "+getLocalName()+": Notification received from DF");
              try {
                  DFAgentDescription[] results = DFService.decodeNotification(inform.getContent());

                  if (results.length > 0) {

                      // Assume there is only one hospital agent
                      assert(results.length == 1);
                      DFAgentDescription dfd = results[0];


                      Iterator it = dfd.getAllServices();
                      while (it.hasNext()) {
                          ServiceDescription sd = (ServiceDescription) it.next();
                          if (sd.getType().equals("allocate-appointments")) {
                              provider = dfd.getName(); 
                              System.out.println("Allocate-appointments service found:");
                              System.out.println("- Service \""+sd.getName()+"\" provided by agent "+provider.getName());

                          }
                      }



                  }

              }
              catch (FIPAException fe) {
                  fe.printStackTrace();
              }
          }
      } );


public AID getProvider() { return provider; }

I clearly init provider with the line

   provider = dfd.getName();

However the line

       public AID getProvider() { return provider; }

returns null for it which I dont get why. Anyone has an idea about whats going on ?

2
  • did you really miss that if + while + if ? Commented Mar 3, 2012 at 0:06
  • I put a break after the assignment still the same.. Commented Mar 3, 2012 at 0:09

2 Answers 2

2

Assuming you are hitting your assignment statement, here are some things to consider:

  • Is getProvider() being called called before handleInform?

  • Are handleInform and getProvider() called on separate threads, creating a race or memory visibility issue?

  • Is the provider variable being overwritten some time between assignment in handleInform and retrieval with getProvider()?

I can't think of anything else. Your variable scoping looks OK. You should add a log statement to the getProvider() method to see when it's being called with respect to handleInform, and log any other places in the program that may overwrite the variable's value.

Sign up to request clarification or add additional context in comments.

1 Comment

It was indeed the case that the functions were not being called linearly (the anonymous class was called last) causing the initialization of other classes (the statements below anonymous class) that used the variable received null value. Thanks a lot.
2

You only initialize the variable when all of these occur:

  • setup() is called
  • The handleInform method in your anonymous class is called
  • An exception isn't thrown before the line is hit
  • results.length > 0
  • dfd.getAllServices() has at least one service
  • At least one service description has a type of "allocate-appointments"

Are you sure all of those are the case? Are you hitting your logging lines of:

  System.out.println("Allocate-appointments service found:");
  System.out.println("- Service \""+sd.getName()+
                     "\" provided by agent "+provider.getName());

? Is that call to provider.getName() working as expected?

Are you sure you're calling getProvider() later on the same instance?

Basically there's a lot which could go wrong here, and you haven't given us any information about what you're seeing, beyond a method call returning null...

2 Comments

Yeah as far as I checked these are all correct. Everything works fine it's just for some reason that accessor method doesnt work as I expected
I noticed that it is fine everywhere inside the anonymous class. Once I try to print it outside it it prints null. Maybe there is a scoping issue ? Also, when I try to use the like this.provider inside the anonymous class it keeps telling me its not a field..

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.