如何解决此java.nullpointexception错误?

时间:2019-12-02 22:39:23

标签: java

enter image description here

public class 002PA4

{

  public static void main(String[] args)

  {

    LaptopInventory linv = new LaptopInventory();   

    linv.processLaptops(); linv.displayLaptops();

    System.exit(0);

  }

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public class Laptop {

  private Scanner input = new Scanner(System.in);//scanner gets information from keyboard

  private String laptopName = " ";// variable to hold laptopName

  private double laptopCost = 0.0;// variable to hold laptopCost

  private int laptopQty = 0;// variable to hold laptopQty



  public Laptop(){}//Empty laptop constructor



  //Overloaded laptop constructor; accepts the data for all the fields

  public Laptop(String laptopName, double laptopCost, int laptopQty)

  {

    //Set class variables equal to parameter values

    laptopName = laptopName;

    laptopCost = laptopCost;

    laptopQty = laptopQty;

  }//END Laptop(String laptopName, double laptopCost, int laptopQty)



  //Prompts for the name of the laptop

  public void setLaptop(int laptopNumber)

  {

    System.out.printf("%nEnter laptop no. %d:", laptopNumber);

    laptopName = input.nextLine();

  }//END setLaptop(int laptopNumber)





  //Prompts for the cost of the laptop

  public void setLaptopCost()

  {

    System.out.printf("%nEnter the cost for the %s: ", laptopName);

    laptopCost = input.nextDouble();

  }//END setLaptopCost()



  //Prompts for the number of laptops to be added to inventory

  public void setLaptopQty()

  {

    System.out.printf("%nAdding how many %s to the inventory? ", laptopName);

    laptopQty = input.nextInt();

    input.nextLine();

  }//END public void setLaptopQty()



  //Returns laptop name

  public String getLaptopName()

  {

    return laptopName;

  }//END getLaptopName()



  //Returns laptop cost

  public double getLaptopCost()

  {

    return laptopCost;

  }//END getLaptopCost()



  //Returns laptop qty

  public int getLaptopQty()

  {

    return laptopQty;

  }//END getLaptopQty()



  //Returns inventory cost

  public double getInventoryCost()

  {

    return laptopCost * laptopQty;//Multiply laptopCost by laptopQty to get the inventory cost

  }//END getInventoryCost()



}//END class Laptop

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import java.util.Scanner;

import java.util.Calendar;

public class LaptopInventory

{

  private Laptop laptops[];



  Calendar dateTime = Calendar.getInstance();

  boolean print = false;

  Laptop laps= new Laptop();



  public LaptopInventory()

  {



  }



   public void processLaptops()

  {

    Scanner input = new Scanner(System.in);

    char choice = 0;

    String laptop = " ";

    int laptopQty = 0;

    double laptopCost = 0;

    int size = 0;



    System.out.printf("%nAre there laptops to be added to inventory? Enter 'Y' or 'N': ");

    choice = input.nextLine().charAt(0);



    if(Character.toUpperCase(choice) == 'N')

    {

      System.out.printf("Thank You! Exiting Program.%n");

    }



    else

    {







      System.out.printf("%nHow many different laptops to add to inventory? ");

      size = input.nextInt();

      input.nextLine();

      laptops = new Laptop[size];

      print = true;

    }//END else do processLaptops

  }  

  public void displayLaptops()

  {

    String laptopInventory = "";



    double total = 0.0;



    for(int i = 0; i < laptops.length; i++)

    {

      switch(i)

      {

        case 0://For $ sign

        {

          laptopInventory += String.format("%n%-30s %,9d @ $%,8.2f ea. %8s $%,17.2f",

                                           laptops[i].getLaptopName(),

                                           laptops[i].getLaptopQty(),

                                           laptops[i].getLaptopCost()," ",

                                           laptops[i].getInventoryCost());

        }

        break;



        default://For no $ sign

        {

          laptopInventory += String.format("%n%-30s %,9d @ $%,8.2f ea. %9s %,17.2f",

                                           laptops[i].getLaptopName(),

                                           laptops[i].getLaptopQty(),

                                           laptops[i].getLaptopCost()," ",

                                           laptops[i].getInventoryCost());

}

      }      

      total += laptops[i].getInventoryCost();

    }



    laptopInventory += String.format("%n%n%27s TOTAL COST OF LAPTOP INVENTORY " + "%6s $%,17.2f%n","","",total);



    if(print == true)

    {

      System.out.printf("%n%nLAPTOP INVENTORY AS OF %TB %Td, %TY\n", dateTime, dateTime, dateTime);

      System.out.printf(laptopInventory);

    }

  }

}

enter image description here

0 个答案:

没有答案