Return-Path: Delivered-To: apmail-lucene-java-dev-archive@www.apache.org Received: (qmail 85489 invoked from network); 9 Aug 2005 01:01:07 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 9 Aug 2005 01:01:07 -0000 Received: (qmail 44250 invoked by uid 500); 9 Aug 2005 01:01:05 -0000 Delivered-To: apmail-lucene-java-dev-archive@lucene.apache.org Received: (qmail 44226 invoked by uid 500); 9 Aug 2005 01:01:05 -0000 Mailing-List: contact java-dev-help@lucene.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: java-dev@lucene.apache.org Delivered-To: mailing list java-dev@lucene.apache.org Received: (qmail 44212 invoked by uid 99); 9 Aug 2005 01:01:05 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 08 Aug 2005 18:01:04 -0700 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: local policy) Received: from [69.55.225.129] (HELO ehatchersolutions.com) (69.55.225.129) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 08 Aug 2005 18:01:26 -0700 Received: by ehatchersolutions.com (Postfix, from userid 504) id 6882D13E2007; Mon, 8 Aug 2005 21:00:59 -0400 (EDT) Received: from [172.16.1.101] (va-71-48-138-146.dyn.sprint-hsd.net [71.48.138.146]) by ehatchersolutions.com (Postfix) with ESMTP id 6F89C13E2006 for ; Mon, 8 Aug 2005 21:00:23 -0400 (EDT) Mime-Version: 1.0 (Apple Message framework v730) In-Reply-To: <42F77673.8010506@saic.com> References: <42F3D463.9070108@saic.com> <42F77673.8010506@saic.com> Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: Content-Transfer-Encoding: 7bit From: Erik Hatcher Subject: Re: NullPointerException: FSDirectory.create(FSDirectory.java:174) Date: Mon, 8 Aug 2005 21:00:22 -0400 To: java-dev@lucene.apache.org X-Mailer: Apple Mail (2.730) X-Spam-Checker-Version: SpamAssassin 3.0.1 (2004-10-22) on javelina X-Spam-Level: X-Virus-Checked: Checked by ClamAV on apache.org X-Old-Spam-Status: No, score=-5.9 required=5.0 tests=ALL_TRUSTED,BAYES_00 autolearn=ham version=3.0.1 X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Mike, I have committed your patch and a slightly modified (reformatted, added Apache Software License) test case. Thanks for the patch AND test case! Erik On Aug 8, 2005, at 11:12 AM, Michael Goddard wrote: > Hi All, > > To follow up that earlier post: here is a JUnit test to illustrate > why I felt the patch was worthwhile. The test, along with the > suggested patch, are attached to this message. > > Thanks. > > Mike > > > Michael Goddard wrote: > > >> Hello, >> >> Today, while using Lucene 1.9-rc1-dev, I encountered this exception: >> >> java.lang.NullPointerException >> at org.apache.lucene.store.FSDirectory.create(FSDirectory.java:174) >> at org.apache.lucene.store.FSDirectory.init(FSDirectory.java:150) >> at org.apache.lucene.store.FSDirectory.getDirectory >> (FSDirectory.java:122) >> at org.apache.lucene.index.IndexWriter.(IndexWriter.java:225) >> >> Puzzled, I added some debugging println() statements in >> FSDirectory and found out that java.io.tmpdir did not exist in my >> setup. It may be that I am in a unique situation, having >> accidentally changed that value to some non-existent directory. >> If I'm not, someone else might encounter the same problem. Just >> in case, below is the little patch I generated from the change I >> came up with. >> >> Mike >> >> Index: org/apache/lucene/store/FSDirectory.java >> =================================================================== >> --- org/apache/lucene/store/FSDirectory.java (revision 230511) >> +++ org/apache/lucene/store/FSDirectory.java (working copy) >> @@ -146,6 +146,13 @@ >> else { >> lockDir = new File(LOCK_DIR); >> } >> + // Ensure that lockDir exists and is a directory. >> + if (!lockDir.exists()) { >> + if (!lockDir.mkdirs()) >> + throw new IOException("Cannot create directory: " + >> lockDir); >> + } else if (!lockDir.isDirectory()) { >> + throw new IOException("Found regular file where directory >> expected: " + lockDir); >> + } >> if (create) { >> create(); >> } >> >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: java-dev-unsubscribe@lucene.apache.org >> For additional commands, e-mail: java-dev-help@lucene.apache.org >> > > > import java.io.File; > import java.io.IOException; > > import junit.framework.TestCase; > > import org.apache.lucene.analysis.standard.StandardAnalyzer; > import org.apache.lucene.index.IndexWriter; > > /** > * Test to illustrate the problem I found when trying to open an > IndexWriter in > * a situation where the the property > org.apache.lucene.lockDir > * was not set and the one specified by java.io.tmpdir > had been > * set to a non-existent path. What I observed is that this > combination of > * conditions resulted in a NullPointerException being > thrown in > * the create() method in FSDirectory, where > * files.length is de-referenced, but files code> is > * null. > * > * @author Michael Goddard > * > */ > > public class FSDirectoryNullPointerExceptionTest extends TestCase { > > /** > * What happens if the Lucene lockDir doesn't exist? > * > * @throws Exception > */ > public void testNonExistentTmpDir() throws Exception { > orgApacheLuceneLockDir = System.setProperty( > "org.apache.lucene.lockDir", NON_EXISTENT_DIRECTORY); > String exceptionClassName = openIndexWriter(); > if (exceptionClassName == null > || exceptionClassName.equals("java.io.IOException")) > assertTrue(true); > else > fail("Caught an unexpected Exception"); > } > > /** > * What happens if the Lucene lockDir is a regular file instead > of a > * directory? > * > * @throws Exception > */ > public void testTmpDirIsPlainFile() throws Exception { > shouldBeADirectory = new File(NON_EXISTENT_DIRECTORY); > shouldBeADirectory.createNewFile(); > String exceptionClassName = openIndexWriter(); > if (exceptionClassName == null > || exceptionClassName.equals("java.io.IOException")) > assertTrue(true); > else > fail("Caught an unexpected Exception"); > } > > public static final String FILE_SEP = System.getProperty > ("file.separator"); > > public static final String NON_EXISTENT_DIRECTORY = System > .getProperty("java.io.tmpdir") > + FILE_SEP + "highly_improbable_directory_name"; > > public static final String TEST_INDEX_DIR = System > .getProperty("java.io.tmpdir") > + FILE_SEP + "temp_index"; > > private String orgApacheLuceneLockDir; > > private File shouldBeADirectory; > > public FSDirectoryNullPointerExceptionTest(String name) { > super(name); > } > > public void setUp() { > } > > public void tearDown() { > if (orgApacheLuceneLockDir != null) > System.setProperty("org.apache.lucene.lockDir", > orgApacheLuceneLockDir); > if (shouldBeADirectory != null && shouldBeADirectory.exists > ()) { > try { > shouldBeADirectory.delete(); > } catch (Exception e) { > e.printStackTrace(); > } > } > File deletableIndex = new File(TEST_INDEX_DIR); > if (deletableIndex.exists()) > try { > rmDir(deletableIndex); > } catch (Exception e) { > e.printStackTrace(); > } > } > > /** > * Open an IndexWriter
> * Catch any (expected) IOException
> * Close the IndexWriter > */ > private static String openIndexWriter() { > IndexWriter iw = null; > String ret = null; > try { > iw = new IndexWriter(TEST_INDEX_DIR, new > StandardAnalyzer(), true); > } catch (IOException e) { > ret = e.toString(); > e.printStackTrace(); > } catch (NullPointerException e) { > ret = e.toString(); > e.printStackTrace(); > } finally { > if (iw != null) { > try { > iw.close(); > } catch (IOException ioe) { > // ignore this > } > } > } > return ret; > } > > private static void rmDir(File dirName) throws Exception { > if (dirName.exists()) { > if (dirName.isDirectory()) { > File[] contents = dirName.listFiles(); > for (int i = 0; i < contents.length; i++) > rmDir(contents[i]); > dirName.delete(); > } else { > dirName.delete(); > } > } > } > } > Index: org/apache/lucene/store/FSDirectory.java > =================================================================== > --- org/apache/lucene/store/FSDirectory.java (revision 230511) > +++ org/apache/lucene/store/FSDirectory.java (working copy) > @@ -146,6 +146,13 @@ > else { > lockDir = new File(LOCK_DIR); > } > + // Ensure that lockDir exists and is a directory. > + if (!lockDir.exists()) { > + if (!lockDir.mkdirs()) > + throw new IOException("Cannot create directory: " + lockDir); > + } else if (!lockDir.isDirectory()) { > + throw new IOException("Found regular file where directory > expected: " + lockDir); > + } > if (create) { > create(); > } > --------------------------------------------------------------------- > To unsubscribe, e-mail: java-dev-unsubscribe@lucene.apache.org > For additional commands, e-mail: java-dev-help@lucene.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: java-dev-unsubscribe@lucene.apache.org For additional commands, e-mail: java-dev-help@lucene.apache.org