Getting Null Pointer exception when using isEmpty () method

I am trying to implement a login function for my program, but it returns a null pointer exception. I understand that this happens when you refer to a place in memory in which there is nothing, but as far as I can see, I created all my objects correctly - please correct me if I am wrong!

I am trying to implement the function of adding users: I have a list of usernames and passwords, and I have an existing method that reads this file and stores it in an array. I want to write a new login to the list, so I wrote a new method that converts this array to an ArrayList and eventually writes a new login, and then writes the login file again. The problem is that I get a 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); } 
+4
source share
4 answers

You probably get a null pointer here

 while (!line.isEmpty()); 

change it to

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

If you pay attention to the trace of the exception stack, you should see the exact line where the exception occurs.

+19
source

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

 while (line != null && !line.isEmpty()) 
+1
source

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 empty lines, change it to this:

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

Other answers proposed so far will stop reading on the first empty line. (If you do not have empty lines, you will not need to call isEmpty() .)

0
source

I do not understand why you are adding null values ​​to listList in the readFile method. Anyway, I edited 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); } 
0
source

Source: https://habr.com/ru/post/1388049/


All Articles