+ 1
String automatically turning Null in java????
Actually I'm making a project with Java Swing, and it has a sign up & login page, when i add usernane & password on Signup and then enter on Login page again & then I've put a check to check if both are same or not, but it says your password & username is incorrect, when i print both of the Signup info & logininfo i say that the variables that were to display logininfo are displaying null instead of string. I can't understand why? I'm storing the login info from text fields to their variables.... But it's still messing up.
6 Answers
+ 4
I don't know exactly what is wrong but I want to point out that you shouldn't use == when comparing strings, use the .equals() method.
== compare object references which might not be what you want
.equals() compare string values.
do this instead: if(s.regname.equals(s.logname))
+ 1
Andrés Sebastiån López I've explained What's happening but i don't have the code as of now
+ 1
Hard to find the problem without your code.
Try to launch your project in mode
debug and set stop points when you get the login and password.
Iâm not sure the problem comes from == instead of equals, you would just have false in response and not null.
0
Can you share your code? At least the part that compare Strings
0
Simle two public String variables regname & logname
s is thr object name of the class I've kept these variables in.
Now I'm comparing like this:
if(s.regname==s.logname)
But comparison is not the issue since when I'm printing values logname is printing null. That's the problem...
0
As Apollo says, you need to compare with equals, and not ==
Strings don't accept == comparison, that's because it get null