0

Essentially, this code should take input from user for:trip description, miles of trip, and gallons of gas used. That info should be placed into an array for each, then have the MPG for each trip calculated. In the end I want it to display a listing that includes: name, miles traveled and the MPG for each trip.

The problem I am having is that the code compiles, but when executed I receive:

Exception in thread "main" java.lang.NullPointerException at week6challenge.Week6Challenge.main(Week6Challenge.java:25) Java Result: 1"

I am still VERY new to programming and I have searched for solutions all over, but I cannot find how to fix it. I learned what the problem is, but an clueless as to how to fix it. Any help would be GREATLY appreciated. Thanks!

Code:

import java.util.Scanner;
public class Week6Challenge {

    public static void main(String[] args) {
        Scanner scan = new Scanner (System.in);
        int count = 0;
        String[] tripName = null;
        double[] tripMiles = null;
        double[] tripMPG = null;
        double miles = 0, gallons = 0;

        while (count <= 10){
            System.out.println("Enter a description of this trip");
            tripName[count] = scan.next();
            count++;

            System.out.println("How many miles did you drive?");
            tripMiles[count] = scan.nextDouble();

            System.out.println("How many gallons of gas did you use on this trip");
            tripMPG[count] = answer(miles, gallons);
        }
        int k = 0;
        System.out.println("Trip Name \t Miles Traveled \t MPG");

        while(k < 10){
            System.out.println(tripName[k]+ "\t" + tripMiles[k] + "\t" + tripMPG[k]);
            k = k++;
        }
    }

    public static double answer(double num1, double num2){
        return (num1 / num2);
    }    
}
nikis
  • 10,833
  • 2
  • 32
  • 45

0 Answers0