Java Knapsack: NullPointerException

2019-09-23 05:41发布

When my code is trying to solve/create the greatest Knapsack (greatest Knapsack maximizes the overall total value of the Knapsack with items in it and minimizes amount of weight), I am trying to get my genetic algorithm to stop running after ten generations if the best Knapsack passed to it is the same as before.

The knapsack labeled reference is a knapsack that contains all potential items that can be put into the later generation knapsacks. The knapsack labeled copy is a copy of the reference, but items will be removed from that copy Knapsack to avoid duplicates within the new generated Knapsack. The portions commented with // ... is code removed that I believe are not apart of the problem, but I labeled the instructions on what the code would do.

ArrayList<Knapsack> knapsackPopulation = new ArrayList<Knapsack>();
  // creates initial population to find Fittest to compare to later
  // ...

     Knapsack generated = new Knapsack();
     boolean canRun = true;
     int currentWeight = 0;
     int itemsContained = 0;

     // this is the process to find the best/fittest Knapsack, which will be used later to compare to
     // ...

  Knapsack bestKnapsack = knapsackPopulation.get(indexOfBest); // best Knapsack found

  // Print out results, weight, and value of overall best Knapsack.

  // ...

  // Mutation portion of code, comparing it to the fittest Knapsack we found earlier 
  boolean hasNotChangedInTenGenerations = true;
  int generationsUnchanged = 0; 

  while ((hasNotChangedInTenGenerations != false) && (generationsUnchanged < 10)) {   
  for (int m=0; m<100; m++) {

     Knapsack copy = new Knapsack(); 
     for (int j=0; j<reference.size(); j++) {
        copy.add(reference.get(j));
     }

     Knapsack copyBest = new Knapsack();
     for (int p=0; p<bestKnapsack.size(); p++) {
        copyBest.add(bestKnapsack.get(p));
     }

     Knapsack generated = new Knapsack();
     boolean canRun = true;
     int currentWeight = 0;
     int itemsContained = 0;

     while (canRun && (currentWeight <= reference.getMaxWeight())) {
        int randomNum = (int)(Math.random() * (((copy.size()-1) + 1)));
        int randomNumBest = (int)(Math.random() * (((copyBest.size()-1) + 1)));
        if (((currentWeight + copy.get(randomNum).getWeight()) < reference.getMaxWeight()) && ((currentWeight + copyBest.get(randomNumBest).getWeight()) < reference.getMaxWeight()) && (copy.get(randomNum) != null) && (copyBest.get(randomNumBest) != null)){

           int randomTwoThirds = (int)(Math.random() * 3);

           if (randomTwoThirds >= 2) { 
              currentWeight += copy.get(randomNum).getWeight();
              generated.add(copy.get(randomNum));
              copy.remove(randomNum);
           } else {
              currentWeight += copyBest.get(randomNumBest).getWeight();
              generated.add(copyBest.get(randomNumBest));
              copyBest.remove(randomNumBest);
           }

           itemsContained++;

        } else {
           canRun = false;
        }
     }
     knapsackPopulation.add(generated);      
  }
  indexOfBest = 0;
  valueOfBest = 0;
  for(int k=0; k<knapsackPopulation.size(); k++) {
     if (knapsackPopulation.get(k).getTotalValue() > valueOfBest){
        indexOfBest = k;
     }         
  }
  if (bestKnapsack.getTotalValue() == knapsackPopulation.get(indexOfBest).getTotalValue()) {
     generationsUnchanged++;
  } else if (bestKnapsack.getTotalValue() < knapsackPopulation.get(indexOfBest).getTotalValue()) {
     bestKnapsack = knapsackPopulation.get(indexOfBest);
  }

  if(generationsUnchanged == 10){
     hasNotChangedInTenGenerations = false;
  }

Specifically, the error is at this line, returning an IndexOutOfBoundsException: Index: 0, Size: 0.

if (((currentWeight + copy.get(randomNum).getWeight()) < reference.getMaxWeight()) && ((currentWeight + copyBest.get(randomNumBest).getWeight()) < reference.getMaxWeight()) && (copy.get(randomNum) != null) && (copyBest.get(randomNumBest) != null)) {
// ...
}

I am unsure on how there could be an out of bounds error exception/null pointer exception as for every time an item is added to the new generated knapsack, the Knapsack labeled copy removes the same item from itself in order to remove future duplicates.

2条回答
不美不萌又怎样
2楼-- · 2019-09-23 05:47

İf You use List or something Like that in Knapsack , this call can be thrown an IndexOutOfBounds: copy.get(randomNum) != null . İf you control this copy.size()>randomNum and it can be solve.

查看更多
看我几分像从前
3楼-- · 2019-09-23 06:00

IndexOutOfBoundsException: Index: 0, Size: 0. means you are trying to get the first element (index 0) from an empty list (size 0).

查看更多
登录 后发表回答