Results 1 to 5 of 5
  1. #1
    lilyumestar is offline Member
    Join Date
    Oct 2008
    Posts
    1
    Rep Power
    0

    Default Definitely new to Java "Main" Java.lang NullPointerException Error

    This project is due by Tuesday and I haven't even gotten half of it done. Can anyone please help me with this Exception error? I've been trying to figure it out for several hours

    Error Message

    "Main" Java.lang NullPointerException
    at Project1.sortingByZipCode<Project1.java:80>
    at Project1.main<Project1.java:31>

    Here is the Source Code


    // StringTokenizerDemo2.java

    import java.util.StringTokenizer;
    import javax.swing.JOptionPane;


    public class Project1
    {

    public static void main(String[] args)
    {
    final int MAX_NUMBER_OF_NUMBERS = 40;

    if ( args.length == 0 )
    {
    System.out.println("This program sorts short integers");
    System.out.println("from the file specified by a");
    System.out.println("command-line argument.");
    System.exit(0);
    } // if

    final String inputFileName = args[0];
    final String outputFileName = "sorted-" + inputFileName;

    String[] numbersArray = new String[MAX_NUMBER_OF_NUMBERS];

    // Read numbers into numbersArray from input file:
    int subArrayLength = readFile(inputFileName, numbersArray);

    //Sorts the zipcode by compareTo method
    sortByZipCode(numbersArray, subArrayLength);


    } // method main





    private static int readFile(String filename, String[] capacities)
    {
    TextFileInput in = new TextFileInput(filename);

    // Read capacities into array:
    int lengthFilled = 0;
    String line = in.readLine(); // read first line in file
    while ( lengthFilled < capacities.length && line != null )
    {

    line = in.readLine(); // read next line in file
    lengthFilled++;
    } // while

    // Check to see if all the capacities in the file were read.
    // If not, then the array wasn't big enough to hold them all.
    // In that case, print an error message and quit.
    if ( line != null ) // i.e. if end-of-file not reached
    {
    System.out.println("File contains too much capacity.");
    System.out.println("This program can process only "
    + capacities.length + " capacities.");
    System.exit(1);
    } // if

    // Release file for re-use:
    in.close();

    return lengthFilled;
    } // method inputFromFile




    private static void sortByZipCode (String[] lines, int length)
    {
    for ( int i = 0; i < length-1; i++ )
    {
    for ( int j = i + 1; j < length; j++ )
    {
    if ( lines[j].compareTo(lines[i]) > 0 )
    {
    String Temp = lines[i];
    lines[i] = lines[j];
    lines[j]= Temp;
    }

    }//for loop with the j

    }//for loop with the i
    }//Method SortByZipCode
    }//Project1

    Thank you very much.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    What's TextFileInput? Is it another class in your application?

    And it's much better explain your question specifically, I mean where are you stuck. It's easy to help for others.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    You are reading the info from the file, but never assigning the line read to your array. I.E. you are missing a
    Java Code:
    capacities[index] = line;
    myself I would change
    Java Code:
    line = in.readLine(); // read next line in file
    lengthFilled++;
    to
    Java Code:
    capactities[lengthFilled++] = in.readLine(); // read next line in file

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Project1.sortingByZipCode<Project1.java:80>
    What object is referenced at line 80? Is it null?
    How did you come to be null? Can you test for it being null?

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Well, it has the method name in it and the only line in that method that will cause an NPE (unless the array itself is null, which it is not, as far as I can see) is this part
    Java Code:
     lines[j].compareTo(lines[i])
    which will only cause an NPE when the element is null. I am about 99% certain the error has been found.

Similar Threads

  1. Replies: 15
    Last Post: 10-12-2010, 06:51 PM
  2. Replies: 3
    Last Post: 01-29-2008, 02:37 AM
  3. Replies: 1
    Last Post: 08-07-2007, 08:10 AM
  4. Replies: 1
    Last Post: 07-25-2007, 11:55 PM
  5. Replies: 1
    Last Post: 07-16-2007, 07:32 AM

Posting Permissions

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