472,958 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

"Main" Java.lang NullPointerException Please Help

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
Expand|Select|Wrap|Line Numbers
  1. "Main" Java.lang NullPointerException
  2.                  at Project1.sortingByZipCode<Project1.java:80>
  3.                  at Project1.main<Project1.java:31>
  4.  
Here is the Source Code

Expand|Select|Wrap|Line Numbers
  1. // StringTokenizerDemo2.java
  2.  
  3. import java.util.StringTokenizer;
  4. import javax.swing.JOptionPane;
  5.  
  6.  
  7. public class Project1 
  8. {
  9.  
  10.        public static void main(String[] args)
  11.        {
  12.           final int MAX_NUMBER_OF_NUMBERS = 40;
  13.  
  14.           if ( args.length == 0 )
  15.           {
  16.              System.out.println("This program sorts short integers");
  17.              System.out.println("from the file specified by a");
  18.              System.out.println("command-line argument.");
  19.              System.exit(0);
  20.           } // if
  21.  
  22.           final String inputFileName = args[0];
  23.           final String outputFileName = "sorted-" + inputFileName;
  24.  
  25.           String[] numbersArray = new String[MAX_NUMBER_OF_NUMBERS];
  26.  
  27.           // Read numbers into numbersArray from input file:
  28.           int subArrayLength = readFile(inputFileName, numbersArray);
  29.  
  30.           //Sorts the zipcode by compareTo method
  31.           sortByZipCode(numbersArray, subArrayLength);
  32.  
  33.  
  34.         }  // method main
  35.  
  36.  
  37.  
  38.  
  39.  
  40.    private static int readFile(String filename, String[] capacities)
  41.    {
  42.            TextFileInput in = new TextFileInput(filename);
  43.  
  44.            // Read capacities into array:
  45.            int lengthFilled = 0;
  46.            String line = in.readLine();   // read first line in file
  47.            while ( lengthFilled < capacities.length && line != null )
  48.                {
  49.  
  50.                    line = in.readLine();       // read next line in file
  51.                    lengthFilled++;
  52.                } // while
  53.  
  54.            // Check to see if all the capacities in the file were read.
  55.            // If not, then the array wasn't big enough to hold them all.
  56.            // In that case, print an error message and quit.
  57.            if ( line != null )            // i.e. if end-of-file not reached
  58.            {
  59.                    System.out.println("File contains too much capacity.");
  60.                    System.out.println("This program can process only "
  61.                            + capacities.length + " capacities.");
  62.                    System.exit(1);
  63.            }  // if
  64.  
  65.            // Release file for re-use:
  66.            in.close();
  67.  
  68.            return lengthFilled;
  69.        }  // method inputFromFile
  70.  
  71.  
  72.  
  73.  
  74.    private static void sortByZipCode (String[] lines, int length) 
  75.    { 
  76.            for ( int i = 0; i < length-1; i++ ) 
  77.            { 
  78.                for ( int j = i + 1; j < length; j++ ) 
  79.                {
  80.                    if ( lines[j].compareTo(lines[i]) > 0 ) 
  81.                    {
  82.                            String Temp = lines[i];
  83.                            lines[i] = lines[j];
  84.                            lines[j]= Temp;
  85.                    }
  86.  
  87.                }//for loop with the j 
  88.  
  89.            }//for loop with the i
  90.    }//Method SortByZipCode
  91. }//Project1
Oct 9 '08 #1
2 3905
JosAH
11,448 Expert 8TB
You don't fill your array when you're reading the file content. All you do is check
whether or not your aray is large enough to store the entire content of the file.

When the method is finished all the entries in the array are still all null and your
sorting method doesn't like that; hence the NullPointerException.

kind regards,

Jos
Oct 9 '08 #2
Thank you very much for the help. It turned out that I didnt store my lines into the array. THank you.
Oct 9 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Phillip Montgomery | last post by:
Hello all; I'm trying to debug an issue with a java script called, SelectSockets. It appears to be a fairly common one found on the web. I downloaded the SGI Java v1.4.1 installation from SGI's...
1
by: Andy Howells | last post by:
Can anybody help me on this? I am getting the below error but have not got a clue why. The file in my classpath eing used has the class that it says is not defined. Any ideas? I am running java...
7
by: news | last post by:
I am pretty new to Java and I just install the JDK1.4.2 03 I am getting an error when I run the class file TestChart.class with the java.exe: Exception in thread "main"...
2
by: dave | last post by:
(Forgive all caps... they are there to differentiate btw question and code) THIS IS THE ERROR I KEEP GETTING. IT COMPILES BUT WHEN IT RUNS, THE FOLLOWING POPS UP: Exception in thread "main"...
12
by: sandyw | last post by:
Exception in thread "main" java.lang.NullPointerException at company.Employee.inputEmployee(Employee.java:172) at company.Employee.main(Employee.java:267) which I got before... Can someone please...
3
by: Ananthu | last post by:
Hi This is my codings in order to access mysql database from java. Codings: import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement;
9
by: tiyaramunna | last post by:
I am trying to configure my system with Java program just to practice on the coding....when i compile a test.java program i am able to see the class file but i cant run the program ... I am getting...
3
by: ohadr | last post by:
hi, i get Exception in thread "main" java.lang.NullPointerException when i run my application. the exact error is: "Exception in thread "main" java.lang.NullPointerException at...
1
by: onlinegear | last post by:
HI i am writing this for college i know i have loads of combo boxes with nothing in the i havent got that far yet. but every time i run this is comes up with this erro run: Exception in thread...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.