The Vehicle.java is the file that I am modifying. The second code is the testdriver.

public class Vehicle
{
	String   make;
	String  model;
	int year;

	public Vehicle(String s1, String s2, int y)
	{

		if (y >= 1980 && y<= 2012)
		{
			year = y;
		}
			else
				{
					y = 2010;
				}



	}

	public String getMake()
	{
	return make;
	}


	public String getModel()
	{
	return model;
	}

	public int getYear()
	{
	return year;
	}

	public void setMake(String s)
	{
		make = s;
	}

	public void setModel(String s)
	{
		model = s ;
	}

	public void setYear(int y)
	{
		year = y;
	}

	public String toString()
	{
		String str = "Make:\t" + make + "\nModel:\t" + model + "\nYear:\t"+year;
     return str;


	}
}
]
import java.util.Scanner;

public class Assignment5
{
	public static void main(String[] args)
	{
		// for getting user input
		Scanner scan = new Scanner(System.in);

		//prompt the user and read in the information
		System.out.print("Enter the vehicle make:\t\t");
		String x = scan.nextLine();
		System.out.print("Enter the vehicle model:\t");
		String y = scan.nextLine();
		System.out.print("Enter the vehicle year:\t\t");
		int z = scan.nextInt();

		// clear the scanner buffer to the end of the line
		scan.nextLine();

		// I called the variables x, y and z to show they need not
		// be the same name as those in the vehicle class.
		Vehicle v1 = new Vehicle(x, y, z);

		// now that I have a vehicle object I can use the reference
		// to invoke the methods and print their results to the screen
		System.out.println("\n" + v1.toString());

		if(v1.getMake().equals("Ford"))
			System.out.println("\nThe vehicle is a Ford");
		else
			if(v1.getMake().equals("Chevrolet"))
				System.out.println("\nThe vehicle is a Chevy");
			else
				System.out.println("\nThe vehicle is neither a Ford nor a Chevy");

		// I'm going to change the state of the object and overwrite the user input
		v1.setMake("Dodge");
		v1.setModel("Viper");
		v1.setYear(2008);

		System.out.println("\nUpdated Vehicle Information:\n" + v1.toString());

	}
}

I get this error when I submit it

Program Input
Ford
F-150
2010


Program Output
Enter the vehicle make: Enter the vehicle model: Enter the vehicle year:
Make: null
Model: null
Year: 2010
Exception in thread "main" java.lang.NullPointerException
at Assignment5.main(Assignment5.java:29)

I checked the discussion board on my schools site and someone posted this answer

This is probably one of two things:
1) The member variable you're returning with getMake() was not assigned in the constructor, and as it's a String object, still has the default value of "null" (reference to nothing).
2) You're returning something else with getMake(); make sure it just returns the String member variable you assigned in the constructor.
Either way, if line 27 works but line 29 fails it's because you're calling ".equals" on a null String (a variable not actually pointing to any object).

Recommended Answers

All 2 Replies

The problem seems to be in your constructor -- it looks like you are missing the code that initializes the instance variables of the class to the variables passed into the constructor.

The reason it's giving you null is because when you created the Vehicle object, in your constructor in your vehicle class, you don't actually set the make and model.

public Vehicle(String s1, String s2, int y)
    {
make = s1;
model = s2;


    if (y >= 1980 && y<= 2012)
    {
        year = y;
    }
        else
            {
                y = 2010;
            }



}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.