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]
# 1

Once you enter the loop

while((line = read.readLine()) !=null)

{.....}

you read again using

line = read.readLine();

words = line.split ("\\t+") // You would get the Exception here

This is returning null as the content's exhausted and you are trying to split line since it's null and thats why you get the Exception

qUesT_foR_knOwLeDgea at 2007-7-10 2:49:48 > top of Java-index,Java Essentials,New To Java...
# 2

while((line = read.readLine()) !=null){

words = line.split ("\\t+");

if(userName.equals(words[2]) && passWord.equals(words[4])){ // Fick vi in en tr鋐f? bra d?abbryter vi loopen

return true;

//break;

}

}

I have removed line = read.readLine();

Try it and see.

qUesT_foR_knOwLeDgea at 2007-7-10 2:49:48 > top of Java-index,Java Essentials,New To Java...
# 3

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();

}

prometheuzza at 2007-7-10 2:49:48 > top of Java-index,Java Essentials,New To Java...
# 4

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;

}

}

HolaJawna at 2007-7-10 2:49:48 > top of Java-index,Java Essentials,New To Java...
# 5

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]?

prometheuzza at 2007-7-10 2:49:49 > top of Java-index,Java Essentials,New To Java...