?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Null Pointer Exception When Calling a Method With Parameters

APCSA-6JGTYN

Consider the following method.

// Assume that Truck is a valid class with a working calcMPG() method
public static double getMPG(String owner, Truck t, int[] gallons)
{
     if (owner.length()>0)
          return 0;

     int totalGallons = 0;
     for (int i=0; i<gallons.length; i++)
     {
         totalGallons += gallons[i];
     }

     return t.calcMPG(totalGallons);
}

In which of the following situations would this method always throw a NullPointerException?

A

When owner points to null and t points to a valid Truck and gallons is a full array of ints.

B

When t points to null and owner is a valid owner name and gallons has at least one element.

C

When gallons points to null and owner points to "Smith" and t points to a Truck object.

D

When both t and gallons point to null.

E

When either t or gallons points to null.