Error Message
Expand|Select|Wrap|Line Numbers
- "Main" Java.lang NullPointerException
- at Project1.sortingByZipCode<Project1.java:80>
- at Project1.main<Project1.java:31>
Expand|Select|Wrap|Line Numbers
- // 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