A casting error

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

A casting error

Smiler7

Excuse my java as it's a new language for me.

I have modified the initial tariff on the sample Broker.

I can't cast the array list to TariffRepo. Have you any tips as to how I can solve this.


------------------------------------------------------------------------------------------------



private void createInitialTariffs ()
  {


  double marketPrice = marketManager.getMeanMarketPrice() / 1000.0;



   Random ranGen = new Random();
   
  List<Tariff> list;

   Tariff t;
   
   for (PowerType pt : customerProfiles.keySet()) {

     double rateValue,c,rangeMin,rangeMax,R;


     rateValue=0.0;


     if (pt.isConsumption())
      {
[460] --  list = ((TariffRepo) tariffRepo.findAllTariffSpecifications()).findActiveTariffs (pt);
   

 [463]--  t = ((TariffRepo) tariffRepo.findAllTariffSpecifications()).findActiveTariffs (pt).get(list.size());
----------------------------------------------------------------------------------------------

Every time I run the broker I get the following error message from spring


Options:
Starting session 1
Exception in thread "Thread-3" java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.powertac.common.repo.TariffRepo
        at org.powertac.samplebroker.PortfolioManagerService.createInitialTariffs(PortfolioManagerService.java:460)
        at org.powertac.samplebroker.PortfolioManagerService.activate(PortfolioManagerService.java:418)
        at org.powertac.samplebroker.core.PowerTacBroker$AgentRunner.run(PowerTacBroker.java:652)
Reply | Threaded
Open this post in threaded view
|

Re: A casting error

gbuijs
You and the exception message are both right, you can't cast ArrayList to TariffRepo.

But i'm not sure what you're trying to do.
I think you're trying to get the last active tariff for powertype pt, right?

// Get a list of all active tariffs for powertype pt
List<Tariff> activeTariffs = tariffRepo.findActiveTariffs(pt);
// Get the last tariff in that list
Tariff lastTariff = activeTariffs.get(activeTariffs.size() - 1);

1) Notice the - 1, Java uses zero-based numbering.
    activeTariffs.get(activeTariffs.size()) would throw an exception.

2) You should check the length of tariffs before getting the last item.
    If the size is 0, the code above would try to get(-1), throwing an exception.

3) If you're new to programming : teach yourself proper naming convention.
    Don't use 't' and 'list' as that becomes confusing very fast.
    Take Johns code as an example.
Reply | Threaded
Open this post in threaded view
|

Re: A casting error

Smiler7
Thank you, that was very kind of you. You are correct that is exactly what I was "trying" to do.

I will study the things you suggested.