Results 1 to 6 of 6
  1. #1
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

    Default Null Pointer Exception

    Can someone tell me why this tester is returning a null pointer exception??

    I have just posted the tester. let me know if you require the complete program..

    Thanks.

    Java Code:
    public class Tester {
    	public static void main (String[] args)
    	{
    		ParkedCar[] p = new ParkedCar[9];
    		p[0] = new ParkedCar("Nissan", "Pulsar", "Silver", "AF15ZA" );
    		p[1] = new ParkedCar("Toyota", "Yarris", "Navy", "ASF123" );
    		p[2] = new ParkedCar("Honda", "Civic", "Red", "SER456");
    		p[3] = new ParkedCar("Suburu", "Forester", "White", "TIM879");
    		p[4] = new ParkedCar("Suburu", "Outback", "White", "GOG987");
    		p[5] = new ParkedCar("Ford", "Fiesta", "Green", "YIS432");
    		p[6] = new ParkedCar("Fird", "Falcon", "Green", "JUL912");
    		p[7] = new ParkedCar("Holden", "Commodore", "Silver", "FEG543");
    		p[8] = new ParkedCar("Volvo", "S69", "Brown", "GHY459");                              
    		                              
    		p[0].setMinutesParked(135);
    		p[1].setMinutesParked(35);
    		p[2].setMinutesParked(65);
    		p[3].setMinutesParked(105);
    		p[4].setMinutesParked(2135);
    		p[5].setMinutesParked(5);
    		p[6].setMinutesParked(35);
    		p[7].setMinutesParked(45);
    		p[8].setMinutesParked(15);//sets time parked
    		
    		ParkingMeter[] pm = new ParkingMeter[9];
    		pm[0] = new ParkingMeter(45);
    		pm[1] = new ParkingMeter(55);
    		pm[2] = new ParkingMeter(60);
    		pm[3] = new ParkingMeter(135);
    		pm[4] = new ParkingMeter(300);
    		pm[5] = new ParkingMeter(5);
    		pm[6] = new ParkingMeter(67);
    		pm[7] = new ParkingMeter(34);
    		pm[8] = new ParkingMeter(66);
    		
    		PoliceOfficer po = new PoliceOfficer("Jony cop", "123456A");
    		
    		for(int i = 0; i < p.length; i++)
    		{
    		ParkingTicket pt = po.examineCar(p[i], pm[i]);
    		System.out.println(pt.issueTicket());
    		}
    	}
    }

  2. #2
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    in which line do you get NullPointerException

  3. #3
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

    Default

    This is my error. Does this mean I have an error in line 42? Not sure.

    Exception in thread "main" java.lang.NullPointerException
    at Tester.main(Tester.java:42)

  4. #4
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    yes, exactly... and how do we know which is line 42 in your code unless you mention :p

  5. #5
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

    Default

    Ok line 42 is this line:

    Java Code:
    System.out.println(pt.issueTicket());

    Here is the Class it relates to.

    Java Code:
    public class ParkingTicket {
    	
    	private String ticketDetails;
    	
    	
    	public ParkingTicket(ParkedCar p , ParkingMeter pm, PoliceOfficer po)
    	{
    		int fine;
    		int timeOver = p.getMinutesParked() - pm.getParkingTimePurchased();
    		int hoursOver = timeOver / 60;
    	
    		if (timeOver > 1 && timeOver < 60)
    		{
    			fine = 25;
    		}
    		else
    		{
    			fine = 25 + hoursOver * 10;
    		}
    		ticketDetails ="****************************************\nYour car " + p.getMake()+ " " + p.getModel()+" " + p.getColor()+ " " + p.getRego();
    		ticketDetails += "\nis fined $" + fine + " for exceeding time limit";
    		ticketDetails += "\nThis fine is issued by " + po.getPoliceDetails();
    		
    	}
    	public String issueTicket()
    	{
    		return ticketDetails;
    	}
    	
    }

  6. #6
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    The problem is in this line
    Java Code:
    ParkingTicket pt = po.examineCar(p[i], pm[i]);
    The method examineCar() returns a null value and the ParkingTicket reference "pt" is assigned a null value.

    Now in line 42 you are trying access a method (issueTicket) in ParkingTicket class using the object "pt", but it is null. That's why you get NullPointerException.

    So examine your examineCar() method ;) and analyze why it is returning null.

Similar Threads

  1. null pointer exception
    By marvelk in forum Advanced Java
    Replies: 8
    Last Post: 02-01-2011, 10:02 AM
  2. Null pointer exception?
    By coffee in forum New To Java
    Replies: 4
    Last Post: 08-03-2009, 04:22 AM
  3. Null Pointer Exception
    By andre1011 in forum Advanced Java
    Replies: 4
    Last Post: 02-07-2009, 04:30 AM
  4. Null Pointer Exception
    By ScKaSx in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 12:27 PM
  5. Null Pointer Exception
    By demiser55 in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 07:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •