hi ..

I want to display an error if the user didn't enter the id or the password in the Text fild.. but nothing happed when I compare it to null .. or to "" .. is there any way to do this? I also tried to compare it with constant .. but it did work as well .. any suggestions?? this is what I have..

if ( (userID.getString() == null ) || (userPW.getString() == null))
		   {
			    System.out.println("Missing required information");
			    doAlarm();

		   }

Dani AI

Generated

A few quick, concrete points that tie the replies here together.

  • The initial null check failed because most TextField implementations return an empty string (""), not null. was on the right track by checking length/isEmpty; that is the usual fix for "missing input" checks.
  • For comparing a returned message like "User Not Found", the problem is using == or != (reference comparison). correctly suggested using value comparison instead — but calls to equals() must be done in a null-safe way.

Safe examples (adapt accessor names to the UI/API in use):

String id = textField.getText();   // or assign the returned String
if (id == null || id.trim().isEmpty()) {
    // treat as missing input
}
 // null-safe comparison: literal on left avoids NullPointerException
if ("User Not Found".equals(response)) {
    // handle not-found case
} else {
    // proceed with user details
}

Alternative options: id.trim().isEmpty() handles whitespace-only input; equalsIgnoreCase(...) matches case-insensitively; Objects.equals(response, "User Not Found") is another null-safe comparator (requires Java 7+).

Troubleshooting tips: print the raw string and its length to confirm whether the value is null, empty, or contains only whitespace; log the exact bytes if non-printable characters are suspected. These checks explain why == appears to "sometimes work" (string interning) but should never be relied on for logical equality.

Recommended Answers

All 3 Replies

if ( (userID.getString().length() == 0 ) || (userPW.getString().length() == 0))
if ( (userID.getString().length() == 0 ) || (userPW.getString().length() == 0))

thank u .. I have done what u 've mentioned and its work well now.

but I faced other problem if u could help me..

if the data base could not find the user it should return
string response = "User Not Found"

otherwise it should return the user details .

I tried to check the return value from response but it also not work with me ..

I have written

if (response != "User Not Found")
				 {
					 menu();

				 }
				else
				 {
					 System.out.println("User Not Found,Please Make Sure from Your Login Details");
					 doAlarm();

				 }

You should be using String.equals() , or, in this case !String.equals() so, your example would then be:

if(!response.equals("User Not Found");
//blah blah, rest of code
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.