Getting Null Pointer Exception when using isEmpty() method
Asked Answered
R

5

14

I'm trying to implement a login feature for my program but it's returning a null pointer exception. I understand that this happens when you refer to a place in memory that has nothing in it, but as far as I can see I have instantiated all my objects properly - please correct me if I am wrong!

I am trying to implement an add user feature: I have a list of usernames and passwords and I have an existing method which reads that file in and stores it in an array. I want to write a new login to the list so I wrote a new method which converts this array to an ArrayList and will eventually write a new login to it and then write the Login file out again. The problem is I am getting the Null Pointer Exception.

Method 1:

public String[] readFile(){
    ArrayList<String> dataList = new ArrayList<String>();
    String Line;
    try {
        String line = br.readLine();
        do {                
            dataList.add(Line);
            line = br.readLine();
        }
        while (!line.isEmpty());
        br.close ();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    String[] dataArr = new String[dataList.size()];
    dataArr = dataList.toArray(dataArr);

    return dataArr; // Returns an array containing the separate lines of the file
}

Method 2:

public void addNewUser (String username, String password){
    String[] dataArr = readFile();  // Read in the list of profiles and store it in an array
    ArrayList<String> dataAL = new ArrayList<String>(Arrays.asList(dataArr));   // store array of profiles as ArrayList
    dataAL.add(username + "\t" + password);

}
Renn answered 26/12, 2011 at 19:9 Comment(0)
D
30

You are probably getting null pointer here

while (!line.isEmpty());

change it to

while(line!=null && !line.isEmpty())

If you paid attention to the exception's stack trace you should see the exact line where the exception is being raised

Durfee answered 26/12, 2011 at 19:12 Comment(3)
Just to explain to @user1058210, this will solve the problem because if the left side of the expression (bool && bool2) is false the right side will not be evaluated. So if line is null then !line.isEmpty() won't be executed.Winton
Thanks for the quick response Pangea! And thanks for the explanation user1031312!Renn
@Renn accept this as the answer if it is indeed the answer to ur questionDurfee
C
1

BufferedReader.readLine() will return a null if the end of the stream is reached. So you want

while (line != null && !line.isEmpty())
Chessa answered 26/12, 2011 at 19:13 Comment(0)
B
0

Change the body of your try block to:

String line = br.readLine();
while (line != null) {                
    dataList.add(Line);
    line = br.readLine();
}
br.close ();

If you want to ignore blank lines, change it to this:

String line = br.readLine();
while (line != null) {                
    if (!line.isEmpty()) {
        dataList.add(Line);
    }
    line = br.readLine();
}
br.close ();

The other answers offered so far will stop the reading at the first blank line. (If you don't have blank lines, then you don't ever need to call isEmpty().)

Batt answered 26/12, 2011 at 19:24 Comment(0)
D
0

I don't understand why you are adding null values to the dataList in readFile method. Anyways, I've refactored your code:

Method 1:

public ArrayList<String> readFile(){
    ArrayList<String> dataList = new ArrayList<String>();

    try {
        //File & File Reader/Stream Initialization Logic

        while(line = br.readLine() != null && !line.isEmpty()) {               
            dataList.add(line);
        }
    } catch (IOException IOE) {
        //IO Exception Handling
    } catch (Exception e) {
        //General Exception Handling
    } finally {
        //close the reader/streams here
    }

    return dataList;
}

Method 2:

public void addNewUser (String username, String password){
    ArrayList<String> dataAL = readFile();
    dataAL.add(username + "\t" + password);
}
Dacoity answered 26/12, 2011 at 19:37 Comment(0)
K
0

`package -> org.springframework.util

!CollectionUtils.isEmpty(line)

Internal Implementation: public static boolean isEmpty(@Nullable Collection<?> collection) { return (collection == null || collection.isEmpty()); }`

Klaraklarika answered 18/8, 2022 at 8:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.