Not sure what the problem is here. When I run this code it creates directories just fine. If I hit the "cancel" button, however, it creates a directory called "null". When I attempt to check the string for null, I get a NullPointerException. Any help is greatly appreciated.

If tname is null, then there is no object with an equals() method to dereference. Hence a NPE
The way to test if an object reference such as tname is null it to use ==
For example: if (tname == null) ...

July 2nd, 2010, 02:08 AM

Charlie

Re: NullPointerException when canceling JOptionPane.showInputDialog

Or to try/catch them NPE's. Try/catching is good practice, youll need to be adept at it later anyways :). Not to mention the fact that teachers love error-handling.