Home > Blockchain >  how to print array elements on a new line and fix null pointer exception in java
how to print array elements on a new line and fix null pointer exception in java

Time:11-06

I am working on a java code where I should make two classes Vehicle and SecondHandVehicle, and then create a main method where it should take an array V of 50 objects and an array SHV of 50 objects when I am displaying output for different objects it is displayed on the same line but I want to make it display each object on a new line and when I run the code I get: 0Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Vehicle.toString()" because "V[i]" is null. I could not figure a solution until now. Vehicle class:

public class Vehicle{
   public String regNo;
   public String make;
   public int yearOfManufacture;
   public double value;
   public Vehicle(String regNo, String make, int yearOfManufacture, double value){
      this.regNo = regNo;
      this.make = make;
      this.yearOfManufacture = yearOfManufacture;
      this.value = value;
   }
   public String getRegNo(){
      return regNo;
   }
   public String getMake(){
      return make;
   }
   public int getYearOfManufacture(){
      return yearOfManufacture;
   }
   public double getValue(){
      return value;
   }
   public void setValue(double value){
      this.value = value;
   }
   public int calculateAge(int currentYear){
      int age;
      age = currentYear - yearOfManufacture;
      return age;
   }
   public String toString(){
      String V = "Register number is "  regNo  ", make "  make  ", year of manufacture "  yearOfManufacture  ", and it costs "  value;
      return V;
   }
  }

SecondHandVehicle class:

public class SecondHandVehicle extends Vehicle{
   private int numberOfOwners;
   public SecondHandVehicle(String regNo, String make, int yearOfManufacture, double value, int numberOfOwners){
      super(regNo, make, yearOfManufacture, value);
      this.numberOfOwners = numberOfOwners;
   }
   public int getNumberOfOwners(){
      return numberOfOwners;
   }
   public boolean hasMultipleOwners(){
      if (numberOfOwners > 1)
         return true;
      else
         return false;
   }
   public String toString(){
      String SHV = "Register number is "  regNo  ", make "  make  ", year of manufacture "  yearOfManufacture  ", and it costs "  value  ", it has "  numberOfOwners  " owner/s";
      return SHV;
   }
}

main method:

import java.util.*;
public class TestVehicle{
   public static void main(String [] args){
      Scanner scnr = new Scanner(System.in);
      String regNo, make;
      int yearOfManufacture, numberOfOwners;
      double value;
      Vehicle V[] = new Vehicle[50];
      SecondHandVehicle SHV[] = new SecondHandVehicle[50];
      int choice = 0;
      int v = 0;
      int s = 0;
      while (choice != 5) {
         System.out.println(
                    " What do you want to do?\n\n"
                      " 1. Add a vehicle to the list V.\n"
                      " 2. Display the details of all vehicles available in V\n"
                      " 3. Add a second-hand vehicle to the list SHV.\n"
                      " 4. Display the details of all second-hand vehicles available in SHV with only one owner.\n"
                      " 5. Exit the program.");
         choice = scnr.nextInt();
         if(choice == 1){
            System.out.println(" Enter the register number of the Vehicle: ");
            regNo = scnr.next();
            System.out.println(" Enter the make of the Vehicle: ");
            make = scnr.next();
            System.out.println(" Enter the year of manufacture of the Vehicle: ");
            yearOfManufacture = scnr.nextInt();
            System.out.println(" Enter the value of the Vehicle: ");
            value = scnr.nextDouble();
            V[v] = new Vehicle(regNo, make, yearOfManufacture, value);
            v  ;
         }
         else if(choice == 2){
            for(int i=0;i<V.length;i  ){
               if (V[v]!=null) {
                  System.out.println(V[v]);
               }
            }      
            printVehicles(V, 0);
         }
         else if(choice == 3){
            System.out.println(" Enter the register number of the Vehicle: ");
            regNo = scnr.next();
            System.out.println(" Enter the make of the Vehicle: ");
            make = scnr.next();
            System.out.println(" Enter the year of manufacture of the Vehicle: ");
            yearOfManufacture = scnr.nextInt();
            System.out.println(" Enter the value of the Vehicle: ");
            value = scnr.nextDouble();
            System.out.println(" Enter the number of owners: ");
            numberOfOwners = scnr.nextInt();
            SHV[s] = new SecondHandVehicle(regNo, make, yearOfManufacture, value, numberOfOwners);
            s  ;
         }
         else if(choice == 4){
            for(int i=0;i<SHV.length;i  ){
               if (SHV[s]!=null) {
                  System.out.println(SHV[s]);
               }
            }      
            printSecondHandVehicles(SHV, 0);
         }
         else if(choice == 5){
            System.out.println(" Thank you. ");
         }
         else{
            System.out.println(" Wrong choice... Try again ");
         }
      }
   }
   public static void printVehicles(Vehicle V[], int i){
      if(i < V.length){
         System.out.print(V[i].toString());
         printVehicles(V, i 1);
          }
   }
   public static void printSecondHandVehicles(SecondHandVehicle SHV[], int i){
      if(i < SHV.length){
         System.out.print(SHV[i].toString());
         printSecondHandVehicles(SHV, i 1);
      }
   }
}

CodePudding user response:

For choice 2, you don't need that for loop, just call:

else if (choice == 2) {
  printVehicles(V, 0);
}

Inside printVehicles:

if (V == null || i < 0 || i >= V.length) {
  return;
}
if (V[i] != null) {
  System.out.println(V[i].toString());
}
printVehicles(V, i 1);

Similar for choice 4 and printSecondHandVehicles

  • Related