File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes null pointer exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

null pointer exception

andrew cassato
Ranch Hand

Joined: Nov 14, 2010
Posts: 37
I am trying to write a program that takes in data from a text file which is just a list of random integers. I have three classes, one for Fractions, one for sorting them and one to run everything.

Somehow all the data is getting lost in the array "A" and I am getting a null pointer exception in the Fractions class (which was given to us so there is nothing wrong with it) at the line 155.

Fractions Class

Sort Fractions Class

Test Class

Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
Without trying to debug your program, see if this gives you enough clues to find the problem.

You say you have a null pointer exception on the line " int a = frac1.getNumerator();" that means that frac1 is null, and therefore the program cannot "dereference" it (that's what it is called when you use a period to access a method or other element inside an object). So you are passing a null into the compare method somehow. If you put a trace statement indicating where you are in the array, you could determine what elements are getting passed in.

Your selection sort does not look right, but I'm afraid it's too late at night for me to take it apart now. try debugging that on a 4- or 5-element array, and step through the different loops.

rc
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19542
    
  16

Please UseOneThreadPerQuestion: http://www.coderanch.com/t/530544/java/java/sorting-fractions

Closing this one.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: null pointer exception
 
Similar Threads
Passing values between classes in two separate files
; expected
I/O problem
sorting fractions problem
Wrong output