Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
    Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

null pointer exception

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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

 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
Sheriff
Posts: 22725
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please UseOneThreadPerQuestion: https://coderanch.com/t/530544/java/java/sorting-fractions

Closing this one.
 
I am not young enough to know everything. - Oscar Wilde This tiny ad thinks it knows more than Oscar:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
    Bookmark Topic Watch Topic
  • New Topic