Results 1 to 8 of 8
  1. #1
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default Why am I getting a NullPointerException

    I am my program working properly but I am trying to use an input() method in a different class to perform the readLine() operation from the data file. I am passing a BufferedReader object to the input() method. I now can not get rid of the NullPointerException. I commented out the original while loop that was the while loop of the original working program. What is causing my NullPointerException problem?

    Thanks.


    private static BufferedReader checkSorted(BufferedReader filePath) throws IOException, FileUnsorted {

    String numbers = null;

    Polynomial polyObjA = null;
    Polynomial polyObjB = null;
    Polynomial p = null;


    numbers = p.input(filePath);



    //while ((numbers = filePath.readLine()) != null) {
    while(numbers != null) {

    polyObjB = new Polynomial(numbers);
    sortedList.add(polyObjB);

    if (polyObjA != null) {

    if (polyObjA.compareTo(polyObjB) > 0) {

    throw new FileUnsorted("Error: File is not sorted");
    }
    }
    polyObjA = polyObjB;

    }
    return new BufferedReader(filePath);
    }

    public String input(BufferedReader filePath) throws IOException{

    String number = "";

    System.out.println("I'm in input");

    number = filePath.readLine();

    return number;
    }
    Last edited by dragstang86; 06-20-2011 at 12:56 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Java Code:
    Polynomial p = null;
    numbers = p.input(filePath);
    "Why a, I getting a NullPointerException?"

    You tell us.

  3. #3
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Java Code:
    Polynomial p = null;
    numbers = p.input(filePath);
    "Why a, I getting a NullPointerException?"

    You tell us.
    LOL...that was dumb. However the compiler complains if I don't initialize p too. When I run the program with out p initialized it gives me "Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - variable p might not have been initialized." How else can I initialize p without being null?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Gee I don't know. How about with a Polynomial Object?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by dragstang86 View Post
    LOL...that was dumb. However the compiler complains if I don't initialize p too. When I run the program with out p initialized it gives me "Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - variable p might not have been initialized." How else can I initialize p without being null?
    So initialize it to something that makes sense. Hard to say more given the snippet of unformatted code that's been provided.

  6. #6
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default

    how do I add the code tags for posting code. I didn't want to post all the code since I couldn't figure out how to format it. I tried using the # tag but that didn't work.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Place [ code ] before and [ /code ] after your code (without the spaces).

  8. #8
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default

    Thanks. I was able to add another constructor to initialize it with. I don't know why I seem to only struggle with the simple things but fly right through the tougher parts of the program. I just tend to get stuck and needed somebody to show me I was over thinking it. Thanks for the help.

Similar Threads

  1. NullPointerException
    By Diz in forum New To Java
    Replies: 10
    Last Post: 05-13-2011, 02:58 AM
  2. NullPointerException
    By maxspyderweb in forum New To Java
    Replies: 5
    Last Post: 03-02-2011, 10:42 AM
  3. NullPointerException
    By GPB in forum New To Java
    Replies: 8
    Last Post: 02-21-2010, 03:05 PM
  4. NullPointerException
    By Juuno in forum New To Java
    Replies: 1
    Last Post: 02-11-2010, 05:43 PM
  5. NullPointerException help?
    By fab5freddy in forum New To Java
    Replies: 2
    Last Post: 02-04-2010, 08:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •