When I put this code between my script. It didn't work. I suppose to put a number in the input, but If I didn't put anything in the INPUT BOX, then the Error Should display.

if( Cost.getText().length() == 0 )

		    {

		     			JOptionPane.showMessageDialog( null, "Cost required",

		    			"Input Required", JOptionPane.WARNING_MESSAGE );

		    			return;

            }
            if( Hours.getText().length() == 0 )

			{

				        JOptionPane.showMessageDialog( null, "Hours required",

				        "Input Required", JOptionPane.WARNING_MESSAGE );

				        return;

            }

I can't see the rest of your code, but it's possible that the string has a null value and thus doesn't have a length of zero. There is a difference between an empty string and a null string. An empty string is like a thread with no beads on it. Adding each character is like adding a bead to the thread and you get a necklace in the end. But the thread is still there in an empty string, and so it has a length of zero. If there is nothing in the input box, it's possible that it's returning null, which means even the thread of the string is not there. Try running the code with the if statement reading: if (Cost.getText() == null)... and see if that solves your problem. You should also usually post your whole code because the problem can be in a place where you least expect it.

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.