• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can't see where NullPointerException error is

 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Hoping someone here can help, I'm writing a program whereby the user must enter the name and password before they can access the rest of the program, the name and password are stored in array, the problem is when I enter in the username and password wrong three times it gives me the nullpointerexception error, it gives me the line where the error is but I can't see it, when the user enters in the wrong username and password three times what should happen is it should bring you to a methond where it will tell you the wrong info was entered and if you want to try again, the full error is "exception in thread main java.lang.nullpointerexception"







Thanks for the help.

 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the ranch, Ronnie, Kevin,

On line 27 and line 30 you declare userName and passWord once again as local variables, hence not assigning values to instance variables of the same names. Just remove String data type declaration from line 27 and 30 and supposed to be "ok" on this matter.

There might be more mistakes, so report to us in case of that. However, there are more things to improve, but since this program reminds me one of mine first programs, I think it isn't that bad
 
Marshal
Posts: 79179
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch again

Don't create multiple Scanners to read from System.in. Use one Scanner per application for System.in and never close it. Beware of Scanner#nextLine. What does your book say it does?
Please explain why you have password[0] and password[1] in an array; I can't see why you have that feature.
 
Kevin O'Sullivan
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Welcome to the ranch, Ronnie, Kevin,

On line 27 and line 30 you declare userName and passWord once again as local variables, hence not assigning values to instance variables of the same names. Just remove String data type declaration from line 27 and 30 and supposed to be "ok" on this matter.

There might be more mistakes, so report to us in case of that. However, there are more things to improve, but since this program reminds me one of mine first programs, I think it isn't that bad



Thanks for the welcome , I am new to programming, I started a course in February and its ending start of July, the above code is not an assignment btw it was just me messing around trying to get use to loops and arrays, I like writing programs that have user input.

That worked, much appreciated thanks, they're indeed loads of mistakes, for instance when I press 0 to  exit the program, the wrongPassWord() method and exit method are called which is not what I want, how can I fix this? or can you at least put me on the right track?
 
Kevin O'Sullivan
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch again

Don't create multiple Scanners to read from System.in. Use one Scanner per application for System.in and never close it. Beware of Scanner#nextLine. What does your book say it does?
Please explain why you have password[0] and password[1] in an array; I can't see why you have that feature.



Thanks for the welcome .

nextLine(); reads in input from user and positions cursor on the next line. As for your other question, I wanted password[0] to match up name[0], at least that was my thinking behind it, I'm guessing its not good practice or its wrong?
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kevin O'Sullivan wrote:. . . nextLine(); reads in input from user and positions cursor on the next line.

That is a lot better than in some books I have seen. Another way to put it is that it reads the remainder of the current line. See the official documentation. If you look here, you will see you can get nasty problems if you get the previous line.

As for your other question, I wanted password[0] to match up name[0] . . .

I don't like parallel arrays, in case you get them out of synch. Can you create a NameAndPassword class which will encapsulate both?

You realise that non‑training apps never store a password?
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kevin O'Sullivan wrote:That worked, much appreciated thanks, they're indeed loads of mistakes, for instance when I press 0 to  exit the program, the wrongPassWord() method and exit method are called which is not what I want, how can I fix this? or can you at least put me on the right track?


Here is some of your code properly indented:
Can you see the problem now?  Tip: breaks are not what you want here.
 
Kevin O'Sullivan
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Kevin O'Sullivan wrote:Can you create a NameAndPassword class which will encapsulate both?

You realise that non‑training apps never store a password?



Thanks for the suggestion, I'll look into that, not to sure how to do it.

On your second do you mean I should be storing the usernames and passwords externally?

 
Kevin O'Sullivan
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:

Kevin O'Sullivan wrote:That worked, much appreciated thanks, they're indeed loads of mistakes, for instance when I press 0 to  exit the program, the wrongPassWord() method and exit method are called which is not what I want, how can I fix this? or can you at least put me on the right track?


Here is some of your code properly indented:
Can you see the problem now?  Tip: breaks are not what you want here.



Taking out break sorted but I still have the problem of it looping back around when the user exits the program, any ideas how to fix that?
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I confused you, sorry.  The breaks may be okay, but the if statements (or one in particular) are not.  I get a NullPointerException after exiting the menu, which I think was the original question.

This is not to say that a for-loop and breaks are the way I would code this, but let's take one problem at a time.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kevin O'Sullivan wrote:. . .  do you mean I should be storing the usernames and passwords externally?

No.
If this is simply a training exercise, keep the password wherever it is. In applications exposed to the big wide world, you don't store passwords at all; you hash them. For more details search for hashing with “salt”.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic