I am working on an assignment for school and i am getting the error "Exception in thread "main" java.lang.NullPointerException at week6_7Prog.main(week6_7Prog.java:38)" I am new to programming and i am still trying to get the hang of it. I have two files the class and the driver and i am trying to get them to output the cats that are over 3 years old and have claws. I have attached the two files and any help would be greatly appreciated!
Thank you so much for the help. I think I'm getting closer but now it skips the name when you enter the data for cat 2 and i don't think it is picking out the cats over 3 with claws. This is what i have so far.
Funny, I'm doing the same program @ Park University ( week 6 ).
Check line 34 on your driver.
I think it should be >3 not >=3 to check for cats that are "more" than 3 years of age.
Joined: Sep 28, 2013
this seems like it's a pretty common java assignment but everyone does it a little different. For some reason i don't think my code is even looking at the age/declawed condition. I think it is just skipping right over it.
I think I'm getting closer but now it skips the name when you enter the data for cat 2
Your problem is down to the way Scanner works when calling one of the getXXX methods to return a primitive value. The methods don't remove the end of line character(s), whereas calling nextline() according to the API docs "Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.".
Because on the second time loop of your code the nextBoolean() method has left the line separator char in the buffer the input name goes after it and the call to nextLine() just steps over that leading line separator char, returning an empty string.
The way to handle this is to call nextLine() after calling nextBoolean() to remove the line separator.
Joined: Sep 28, 2013
Thank you for the help, i was finally able to get it to work properly!