Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help fixing error

 
tj reed
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone!

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!











 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

You've made a common mistake with arrays: you have declared the array (line 10 of your second code snippet), but you haven't put any Cat objects in it. You have an array with three null elements.

You'll need to create new Cat objects and put them in the array, before you try to do something like MyCat1[i].isDeclawed() - if MyCat1[i] is null in that line, you'll get a NullPointerException.

A NullPointerException usually happens because you try to call a method on a variable that is null.

In the first for-loop (lines 13-27) you should, after getting values from the user, create a Cat object with those values and put them in the array at position i.
 
tj reed
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Six Uno
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
tj reed
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A tip for using the forum: UseCodeTags, so that the forum properly formats your code, with line numbers, syntax highlighting etc. I added them in your posts above.
 
Tony Docherty
Bartender
Posts: 2942
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
tj reed
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony,

Thank you for the help, i was finally able to get it to work properly!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic