String automatically turning Null in java???? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

13th Jun 2022, 2:07 PM
Farzam Baig
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))
13th Jun 2022, 2:43 PM
Apollo-Roboto
Apollo-Roboto - avatar
+ 1
Andrés Sebastián López I've explained What's happening but i don't have the code as of now
13th Jun 2022, 2:37 PM
Farzam Baig
+ 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.
14th Jun 2022, 6:28 AM
Roland
Roland - avatar
0
Can you share your code? At least the part that compare Strings
13th Jun 2022, 2:15 PM
Andrés Sebastián López
Andrés Sebastián López - avatar
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...
13th Jun 2022, 2:37 PM
Farzam Baig
0
As Apollo says, you need to compare with equals, and not == Strings don't accept == comparison, that's because it get null
13th Jun 2022, 3:03 PM
Andrés Sebastián López
Andrés Sebastián López - avatar