NullPointerException help
Hi, im pretty new to java
i get
Exception in thread "main" java.lang.NullPointerException
at Login.validateUser(Login.java:25)
import java.util.*;
import java.io.*;
publicclass Login
{
publicstaticboolean validateUser(String userName, String passWord)// throws FileNotFoundException//, IOException
{
String line, user, pwd;
String[] words;
BufferedReader read =null;
try
{
read =new BufferedReader(new FileReader("persondata.txt"));
}
catch(FileNotFoundException fnfe)
{
}
try
{
while((line = read.readLine()) !=null){
line = read.readLine();
words = line.split ("\\t+");
if(userName.equals(words[2]) && passWord.equals(words[4])){// Fick vi in en tr鋐f? bra d?abbryter vi loopen
returntrue;
//break;
}
}
read.close();
}
catch(IOException ioe)
{
}
returnfalse;
}
}
BoEk650326-2781105abc123
NisseSvensson801202-122378,5aqw183
HassenKhameli790412-3423120jonas
LisaAndersson760203-343485kla12
JakobNilsson820603-444483sdlksq
EliziReveiro790923-125690lksfj
labkbnjfgjdfg 252
It worked before =/ dont know why im getting that error
Best Regards
Jawn
[2601 byte] By [
HolaJawna] at [2007-11-26 21:12:09]

Not related to your problem (yet), but never swallow exceptions!
When you do this:catch(IOException ioe)
{
}
and a IOException is being thrown, you know nothing about it. At least print the stacktrace, so you know why your application is not doing as you expect. You can print a stacktrace like this:catch(IOException ioe)
{
ioe.printStackTrace();
}
Thanks guys for finding the problem so fast i really appreciate it.
It now how ever always return false when trying to validate.
I have checked to see that it split out the correct words from persondata.txt and it does.
But it however always returns false
import java.util.*;
import java.io.*;
public class Login
{
public static boolean validateUser(String userName, String passWord)
{
String line, user, pwd;
String[] words;
BufferedReader read = null;
try
{
read = new BufferedReader(new FileReader("persondata.txt"));
}
catch(FileNotFoundException fnfe)
{
fnfe.printStackTrace();
}
try
{
while((line = read.readLine()) !=null){
words = line.split ("\\t+");
if(userName.equals(words[2]) && passWord.equals(words[4])){
return true;
}
}
read.close();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
return false;
}
}
Instead of splitting it using tab's:words = line.split ("\\t+");
try using thiswords = line.split ("\\s+");
which will split any whitespace character (tabs, whitespaces etc.). There might be one (or more) whitespace before or after the username or password when comparing them: "\\s+" will remove them as well.
And just to be sure: your username is stored in words[2]?