Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Program Error - Please help

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    1
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Program Error - Please help

    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();
     
        }
     
     
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Program Error - Please help

    What line is the NullPointerException on? What variable is null on that line?

    Step through your program with a debugger, or at least add some print statements, to answer that question. When you have that question answered, you can figure out the "why" and finally the "how to fix it" parts.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Program Error - Please help

    The reasons for this exception are described in the NullPointerException class description. See NullPointerException (Java Platform SE 7 ). The key to solving NullPointerExceptions is in the stack trace:
    Quote Originally Posted by Optimus_Prime View Post
    Exception in thread "main" java.lang.NullPointerException
    	at Vehicle.toString(Vehicle.java:91)
    	at Run.main(Run.java:17)
     
    Process completed.
    The stack trace indicates the exception was thrown from the Vehicle class's line 91. This line corresponds to:
    str = str + "\nMost Recent Customer: " + mostRecentContract.getCustomerRef();
    We know str cannot be the source of the exception, and so this leaves us with mostRecentContract.getCustomerRef(). From NullPointerException's class description, one of its causes is "calling the instance method of a null object." In order to be sure, all you need to do as to print out the variables/objects from the line. Add the following before this line:
    System.out.println("str: " + str);
    System.out.println("mostRecentContract: " + mostRecentContract);
    Run your program again, and you shall discover the cause of the exception.

Similar Threads

  1. 1 MINOR ERROR IN PROGRAM
    By Java girl in forum What's Wrong With My Code?
    Replies: 19
    Last Post: March 31st, 2014, 01:00 AM
  2. error in my java program
    By ayushgarg11 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 1st, 2013, 11:54 PM
  3. Cannot Execute Program Error=26
    By frozen java in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 27th, 2012, 08:23 AM
  4. I/O Program output error
    By gatorsgirl in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: April 11th, 2012, 04:18 PM
  5. Error of data types and type casting in java program
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 2nd, 2009, 10:22 AM

Tags for this Thread