Exception in thread \"main\" java.lang.NullPointerException at pet.DogDemo.readR
ID: 3643666 • Letter: E
Question
Exception in thread "main" java.lang.NullPointerException
at pet.DogDemo.readRecord(DogDemo.java:37)
at pet.DogDemo.main(DogDemo.java:21)
Java Result: 1
AND HERE IS THE CODE:
package pet;
import java.util.Scanner;
/**
*
* @author Linette
*/
public class DogDemo {
private static int dog_no;
private static Dog[] dogs;
/**
* @param args
*/
public static void main(String[] args) {
readRecord();
printMinMax();
printAll();
}
private static void readRecord()
{
Scanner keyboard = new Scanner(System.in);
// get number of dogs
System.out.println("Please enter the number of dogs to input: ");
dog_no = keyboard.nextInt();
dogs = new Dog[dog_no];
for (int i = 0; i< dog_no; i++)
{
// get dog's name
System.out.println("Dog number " + i + " Dog name:");
dogs[i].setName(keyboard.next());
// get dog'age
System.out.println("Dog's age:");
dogs[i].setAge(keyboard.nextInt());
// get dog's weight
System.out.println("Dog's weight:");
dogs[i].setWeight(keyboard.nextDouble());
// get dog's breed
System.out.println("Dog's breed:");
dogs[i].setBreed(keyboard.next());
// get dog's owner
System.out.println("Dog's owner:");
dogs[i].setOwner(keyboard.next());
// get dog's booster shot
System.out.println("Has the dog had a booster shot within 2 years? Enter y (or Y) for yes and anything else for no.");
dogs[i].setBoosterShot(keyboard.next().toLowerCase().equals("y"));
// show infos
System.out.println("You have entered the following dog info:");
System.out.println("Name:" + dogs[i].getName());
System.out.print("Age:" + dogs[i].getAge());
if (dogs[i].getAge() < 2)
System.out.println("year");
else
System.out.println("years");
System.out.println("Weight:" + dogs[i].getWeight() + " pounds");
System.out.println("Breed:" + dogs[i].getBreed());
System.out.print("Has ");
if (!dogs[i].isBoosterShot())
System.out.print("NOT ");
System.out.println("had a booster shot.");
System.out.println("");
}
}
private static void printAll()
{
int in, out;
Dog temp = null;
for(out = 1 ; out < dog_no; out++ ) // out is dividing line
{
temp = dogs[out]; // remove marked person
in = out; //index of the end of sorted region
while(in>0 && dogs[in-1].getWeight() > temp.getWeight() )
{ // until smaller one found
dogs[in] = dogs[in-1]; // shift item to the right
--in; // go left one position
}
dogs[in] = temp; // insert marked item
} // end for
System.out.printf("%s %s %s %s %s ", new Object[] {"Owner","Pet
Explanation / Answer
If i am spacing these out correctly, which i would like to think that i am. It would appear that you are having an error at // get dog's name System.out.println("Dog number " + i + " Dog name:"); ==>dogs[i].setName(keyboard.next()); In my experience with scanner it is not always as simple as just typing in what you want it to do as it is taking it from a spool of inputs. You may need to experiment with the keyboard.next() as it could be getting an old next line character form a previous getInt() for example I wish that i could run your code and figure out what the problem is specifically but as you did not include code for pet as part of your files it is nearly impossible to run the code. Some easy ways to figure out if this is picking up a random character left over is to see if in the error, if you see the previous system.out statement. If you would still like help on error finding put up all of the code into codepad.org and ill debug it for you, but without being able to actually run it myself i cant truly tell you if thats the only problem you are having or not.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.