KingNothing
Diamond Member
I'm using Sun Java 1.4, and the idea here is to make the SpellChecker class so only one instance is around at any given time. I can't figure out how to create the instance the first time around though because it gives me this error:
Edit: Thanks for the tips on exceptions, this was actually a problem in the exception my constructor was throwing for file access.
===================================
public class SpellChecker
{
private static SpellChecker instance;
private static int numInstances = 0;
private SpellChecker() throws Exception
{
//code to read in words
}
public static SpellChecker getInstance()
{
try
{
if (numInstances == 0)
{
instance = new SpellChecker();
numInstances++;
}
}
catch (Exception e)
{
e.printStackTrace();
}
return instance;
}
==========================
import java.io.*;
import java.util.*;
public class test
{
public static void main(String arg[])
{
try
{
SpellChecker s = SpellChecker.getInstance();
if (null == s)
System.out.println("Cannot get spellchecker");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Edit: Thanks for the tips on exceptions, this was actually a problem in the exception my constructor was throwing for file access.
===================================
public class SpellChecker
{
private static SpellChecker instance;
private static int numInstances = 0;
private SpellChecker() throws Exception
{
//code to read in words
}
public static SpellChecker getInstance()
{
try
{
if (numInstances == 0)
{
instance = new SpellChecker();
numInstances++;
}
}
catch (Exception e)
{
e.printStackTrace();
}
return instance;
}
==========================
import java.io.*;
import java.util.*;
public class test
{
public static void main(String arg[])
{
try
{
SpellChecker s = SpellChecker.getInstance();
if (null == s)
System.out.println("Cannot get spellchecker");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}