aspose file tools*
The moose likes Beginning Java and the fly likes Need help fixing error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help fixing error" Watch "Need help fixing error" New topic
Author

Need help fixing error

tj reed
Greenhorn

Joined: Sep 28, 2013
Posts: 4
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

Joined: Aug 16, 2005
Posts: 14156
    
  19

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
tj reed
Greenhorn

Joined: Sep 28, 2013
Posts: 4
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

Joined: Sep 28, 2013
Posts: 1
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

Joined: Sep 28, 2013
Posts: 4
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

Joined: Aug 16, 2005
Posts: 14156
    
  19

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

Joined: Aug 07, 2007
Posts: 2289
    
  49
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

Joined: Sep 28, 2013
Posts: 4
Tony,

Thank you for the help, i was finally able to get it to work properly!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help fixing error