Results 1 to 5 of 5
- 10-09-2008, 10:26 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 1
- Rep Power
- 0
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.
- 10-09-2008, 10:36 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 10-09-2008, 10:40 AM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
You are reading the info from the file, but never assigning the line read to your array. I.E. you are missing a
myself I would changeJava Code:capacities[index] = line;
toJava Code:line = in.readLine(); // read next line in file lengthFilled++;
Java Code:capactities[lengthFilled++] = in.readLine(); // read next line in file
- 10-09-2008, 02:19 PM #4
What object is referenced at line 80? Is it null?Project1.sortingByZipCode<Project1.java:80>
How did you come to be null? Can you test for it being null?
- 10-09-2008, 06:08 PM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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
which will only cause an NPE when the element is null. I am about 99% certain the error has been found.Java Code:lines[j].compareTo(lines[i])
Similar Threads
-
Exception in thread "main" java.lang,NullPointerException
By ljk8950 in forum AWT / SwingReplies: 15Last Post: 10-12-2010, 05:51 PM -
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
By hemanthjava in forum AWT / SwingReplies: 3Last Post: 01-29-2008, 01:37 AM -
ERROR: Exception in thread "main" java.lang.NoSuchMethodError: main
By barney in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:10 AM -
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 10:55 PM -
ArrayList: Exception in thread "main" java.lang.NullPointerException
By susan in forum New To JavaReplies: 1Last Post: 07-16-2007, 06:32 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks