Win a copy of Java Challengers this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Null Exception

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,

I'm new here.

I've been trying for over a week now to write a code that reads data from a txt file,Saves that in array.The user attempts to login using username and password. If the username and password exists in the file read,then I welcome the user.Else,I will give him few more attempts before locking him out.

Logically all is well.When I run my code I get :
"Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.split(String)" because "lineRead" is null"

But when I debug it , lineRead is not null...and the values read are successfully put in the array ...

where did I go wrong in your opinion?

Thank you for your insight.

 
Rancher
Posts: 3849
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most likely, the lineRead is often not null - but perhaps sometimes, it is.  When you get to the end of the input, perhaps?  It's hard to say without seeing the code that is throwing the exception...
 
Delara Ana
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for your response.

I copied/pasted below.

I created three classes.One with main method, User class  and UserServices class.



----- UserServices class------------



------ User POJO class ____________



__________UserApp________






------data.txt--------

username1,password1,user1
username2,password2,user2
username3,password3,user3
username4,password4,user4
username5,password5,user5
 
Master Rancher
Posts: 4284
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code calls the readLine method two times: line 28 and line 29
The value read on line 28 is not used.
The results would be that the code reads every other line with the possibilty that the code on line 29 can return a null value.

It is better to read into a variable in the while statement and test for null all in one go.  Something like this(untested):
 
Delara Ana
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:The code calls the readLine method two times: line 28 and line 29
The value read on line 28 is not used.
The results would be that the code reads every other line with the possibilty that the code on line 29 can return a null value.

It is better to read into a variable in the while statement and test for null all in one go.  Something like this(untested):



I'm very grateful.
I would never have figured it on my own.
 
Marshal
Posts: 72657
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are using a buffered reader, you are probably better off using its lines() method, which returns a Stream<String>, which you can use to process your inputs. If you want to use a loop, do it like this:-Java™ Tutorial references: About the new form of try: link. About using a Path rather than an explicit file reader: start from this section. You would never guess line 28. It needs an additional pair of () to make sure the reading and assignment take place before the null text. There is a method called readAllLines() which will produce the whole of the file as a List.

I trust you have been told that keeping real life passwords in a text file is a very low‑security practice.
 
Ranch Hand
Posts: 35
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Delara,

Welcome to the ranch. I hope you find it as helpful and friendly as I have.

You've created a very neat sample authentication system. I think I found a logic error in your code (or maybe I copy-paste erred). After fixing the reader.readLine() problem, when you enter the password (correct or incorrect), the application cycles through every user, printing the "You've entered wrong username or password..." message (when on the wrong user) and either prints the "Welcome..." message or exits. I'm not certain how your system is supposed to work but I would imagine it's supposed to be for a single login with multiple attempts allowed. There's no way for the user to try more password attempts (the scanner only gets password input once so that value persists). The for statement (line 72) cycles through every user in the DB testing the password with that value. Also, the readDataStoreInArray() method is called numerous times in that loop which is causing odd output (and unnecessary overhead). It's a simple fix. First, move the password scanner statements inside the while loop to allow multiple attempts. The for loop on line 72 can be removed entirely. Instead of cycling through every user with aforementioned for loop, just above the while loop on line 71, you can find the appropriate user object and use that to test the password against. Then your if test inside the while can be simplified. So something like:

And


Prior to the above modifications, the console output looks like:


Once the code is updated, it looks like:


 
He does not suffer fools gladly. But this tiny ad does:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic