My current assignment involves me outputting these 2 classes. Yet I'm not really sure in what manner I should go about doing this. I have tried creating a separate class and outputting my toString methods there but for some reason I am getting an error. If anyone could assist me with either resolving the error or at least pointing me in the right direction it would be much appreciated.
The error message is thus;
Exception in thread "main" java.lang.NullPointerException at Vehicle.toString(Vehicle.java:91) at Run.main(Run.java:17) Process completed.
import java.util.Calendar; public class HireContract { String reference; Calendar startDate = null; Calendar endDate = null; Vehicle vehicle = null;; String customerRef; double rate; public HireContract() { super(); } public HireContract(String reference, Calendar startDate, Calendar endDate, Vehicle vehicle, String customerRef, double rate) { super(); this.reference = reference; this.startDate = startDate; this.endDate = endDate; this.vehicle = vehicle; this.customerRef = customerRef; this.rate = rate; } public String getCustomerRef() { return customerRef; } public void setCustomerRef(String customerRef) { this.customerRef = customerRef; } public Calendar getEndDate() { return endDate; } public void setEndDate(Calendar endDate) { this.endDate = endDate; } public double getRate() { return rate; } public void setRate(double rate) { this.rate = rate; } public String getReference() { return reference; } public void setReference(String reference) { this.reference = reference; } public Calendar getStartDate() { return startDate; } public void setStartDate(Calendar startDate) { this.startDate = startDate; } public Vehicle getVehicle() { return vehicle; } public void setVehicle(Vehicle vehicle) { this.vehicle = vehicle; vehicle.setAvailable(false); } public void terminateHire(Calendar finish) { this.setEndDate(finish); vehicle.returnVehicle(finish); vehicle.setMostRecentContract(this); } public String toString() { int startMonth=0, startDay=0, startYear =0; int endMonth=0, endDay=0, endYear =0; if (startDate != null) { startMonth = startDate.get(Calendar.MONTH) +1; startDay = startDate.get(Calendar.DATE); startYear = startDate.get(Calendar.YEAR); } if (endDate != null) { endMonth = endDate.get(Calendar.MONTH)+1; endDay = endDate.get(Calendar.DATE); endYear = endDate.get(Calendar.YEAR); } String str= ""; str= "Rental Contract\n"+reference+"\n"+customerRef+"\n"+"Start of item details\n"+vehicle.toString()+"\n"+"End of Item Details"; str = str + "\n"+rate+"\n"+startDay+"/"+startMonth+"/"+startYear+"\n"+endDay+"/"+endMonth+"/"+endYear+"\n"+"\n"; return str; }// end toString }
import java.util.Calendar; public class Vehicle { String id; String make; String model; HireContract mostRecentContract = null; Calendar lastReturned = null; boolean available; public Vehicle() { super(); } public Vehicle(String id, String make, String model, Calendar lastReturned, boolean available) { super(); this.id = id; this.make = make; this.model = model; this.lastReturned = lastReturned; this.available = available; } public boolean isAvailable() { return available; } public void setAvailable(boolean available) { this.available = available; } public String getId() { return id; } public void setId(String id) { this.id = id; } public Calendar getLastReturned() { return lastReturned; } public void setLastReturned(Calendar lastReturned) { this.lastReturned = lastReturned; } public String getMake() { return make; } public void setMake(String make) { this.make = make; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } public HireContract getMostRecentContract() { return mostRecentContract; } public void setMostRecentContract(HireContract mostRecentContract) { this.mostRecentContract = mostRecentContract; } public void returnVehicle(Calendar last) { this.setLastReturned(last); this.setAvailable(true); } public String toString() { int retMonth=0, retDay=0, retYear =0; String str = ""; if (lastReturned!=null) { retMonth = lastReturned.get(Calendar.MONTH) +1; retDay = lastReturned.get(Calendar.DATE); retYear = lastReturned.get(Calendar.YEAR); } str= "ID: "+id + "\n"+"Make "+make+"\n"+"Model: "+model+"\n"+"Return Date: "+retDay+"/"+retMonth+"/"+retYear+"\n"+"Available: "+available+"\n"; str= str + "\nMost Recent Customer: " + mostRecentContract.getCustomerRef(); return str; } } // end class Vehicle
public class Run { public static void main(String [] args) { HireContract hireObject = new HireContract(); Vehicle vehicleObject = new Vehicle(); vehicleObject.toString(); hireObject.toString(); } }