The Daily Click ::. Forums ::. Non-Klik Coding Help ::. Problem with Java NullPointerException error
 

Post Reply  Post Oekaki 
 

Posted By Message

AS Filipowski



Registered
  29/02/2004
Points
  444
29th March, 2006 at 17:37:38 -

Basically, im trying to read string from a file and parse it, but I keep getting a 'NullPointerException' error in lines 28 and 38. Can someone help me? It's due in tommorow and I don't know how to solve it.

"import java.io.*;
public class Assignment
{
static String inString; String currentstr; char character = ' ';
public static void main(String args[]) throws IOException
{
String filename = "Manchester.txt";
String words[] = new String[50];
int lines;

FileReader inStream = new FileReader(filename);
BufferedReader inFile = new BufferedReader(inStream);
inString = inFile.readLine();
int counter = 0;
while (inString != null)
{
inString=inFile.readLine( );
words[counter]=inString;
counter++;
}
inFile.close( );
lines = counter;
counter = 0;

while (counter<lines)
{
counter++;
Compare(words[counter]);
}

//System.out.println(words[1]); // Test to see if string copying works
}

public static void Compare(String words)
{
int counter2=0;
char character = ' ';
character = words.charAt(0);
return;
}
}"

 
I do not have a signature of my own

David Newton (DavidN)

Invisible

Registered
  27/10/2002
Points
  8322

Honored Admin Alumnus
29th March, 2006 at 19:02:08 -

NullPointerException exceptions don't exactly go out of their way to help you, as there's never an indication of what exactly caused it - to compensate, System.out.println the variables you're comparing first (in this case the contents at that point in the array). I can see you did try this, but it's in the wrong place - it should be done before Compare is called, not after.

 
http://www.davidn.co.nr - Games, music, living in America

AS Filipowski



Registered
  29/02/2004
Points
  444
29th March, 2006 at 20:34:32 -

Ok, i'll try that. Thanks

 
I do not have a signature of my own

mushi-games



Registered
  17/02/2005
Points
  136
30th March, 2006 at 01:49:33 -

Your loop is wrong, you are reading 2 lines before you save it to the array, so..

// get rid of first read and
while((inString = inFile.readLine()) != null)
{
// Stuff
}

or save to array in the loop BEFORE reading next line.
-peace

 

He did harken unto her, "hark hark," harkened he.

http://www.freepgs.com/mushi/
   

Post Reply



 



Advertisement

Worth A Click