954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem creating dialog box

I'm creating a word processor similar to Microsoft Word (but much simpler). I'm having a problem with a dialog box.

When a user presses the 'new document' button, it prompts the user to enter the file name, and when the user does so, he presses ok, and the file name is stored in a variable. That works out fine, the problem is when the user presses cancel! I could think of no way to manipluate what happens when the user presses cancel, (the default action is that the file name is set as null, rather than just leaving it as it was before) so I added some code myself by identifying the length of the string - basically, if the length of the string is 0, then cancel was pressed because there was no input. However, I am getting an error. This is the code:

private void newDocument() {
        String tempFileName = JOptionPane.showInputDialog(null, "Please enter file name", "Please enter file name", JOptionPane.INFORMATION_MESSAGE);
        if (tempFileName.length() == 0) {
            tempFileName = "";

        } else {
            fileName = tempFileName;
            textArea.setText(null);
        }

        textArea.setEditable(true);
        textArea.setEnabled(true);
        enableButtons();
    }
SeanC
Light Poster
47 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

If a variable is null, you can't test its length - it hasn't got a length!.
Use var == null

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
If a variable is null, you can't test its length - it hasn't got a length!. Use var == null

Thanks! :)

SeanC
Light Poster
47 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: