Java/jsp help.

Caporegime
Joined
12 Mar 2004
Posts
29,962
Location
England
I'm trying to run this simple code which checks if a password exists in an xml user file.

Code:
for (int y = 0; y < passwords.getLength(); y++) {
	if (password == passwords.item(y).getTextContent()) {
		out.println("yes");
	}	
}

But everytime I run this nothing prints out on the web page! I have no problem using the .getTextContent to print out a list of all users passwords, but this comparison statement never evaluates to true for some reason, using .equals() even gives me a nullpointer exception!
 
Last edited:
String comparisons must use .equals() so change your statement to:
Code:
for (int y = 0; y < passwords.getLength(); y++) {
	if (passwords.item(y).getTextContent() != null && passwords.item(y).getTextContent().equals(password)) {
		out.println("yes");
	}	
}
 
String comparisons must use .equals() so change your statement to:
Code:
for (int y = 0; y < passwords.getLength(); y++) {
	if (passwords.item(y).getTextContent() != null && passwords.item(y).getTextContent().equals(password)) {
		out.println("yes");
	}	
}

Yup, just as an extra note try compiling the following and see what you get

Code:
String s1 = "Hello";
String s2 = "Hello";
String s3 = new String("Hello");
String s4 = new String("Hello");
System.out.println(s1==s2);
System.out.println(s3==s4);
System.out.println(s3.equals(s4));

You'll get true false true, this is all to do with the String pool and reference variables.
 
I found out why I was getting a null pointer exception when using .equals(), I had given the password textbox the wrong id. :o
 
Back
Top Bottom