Problem with StringTokenizer and String.split()
I have this class for a golf game I'm writing:
import java.io.*;
import java.util.StringTokenizer;
publicclass CourseListReader
{
publicstatic Course[] getCourseList()
{
try
{
BufferedReader courseReader =new BufferedReader(new FileReader("courses\\courses.dat"));
Course[] courses =new Course[20];
for (int i = 0; i < courses.length; i++)
{
String courseLine = courseReader.readLine();
StringTokenizer st =new StringTokenizer(courseLine,":");
Course thisCourse =new Course();
thisCourse.name = removeQuotes(st.nextToken());
thisCourse.directory = removeQuotes(st.nextToken());
courses[i] = thisCourse;
}
return courses;
}
catch (IOException ioe)
{
ioe.printStackTrace();
returnnull;
}
}
publicstatic String removeQuotes(String input)
{
StringBuffer output =new StringBuffer();
char[] characters = input.toCharArray();
for (int i = 0; i < characters.length; i++)
{
char currentChar = characters[i];
if (currentChar !='\"' && currentChar !='\'')
{
output.append(currentChar);
}
}
return output.toString();
}
}
class Course
{
String name;
String directory;
}
However, when I call getCourseList(), I get this exception:
Exception in thread"AWT-EventQueue-0" java.lang.NullPointerException
at java.util.StringTokenizer.<init>(Unknown Source)
at java.util.StringTokenizer.<init>(Unknown Source)
at CourseListReader.getCourseList(CourseListReader.java:17)
at GolfGameCourseChooserPanel.<init>(GolfGameCourseChooserPanel.java:8)
at GolfGameWaitingPanel.actionPerformed(GolfGameWaitingPanel.java:76)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour
ce)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Using String.split also gives me a NullPointerException.
In case it helps, here's courses\courses.dat:
"Test Course":"testcourse"
It's just a test file, but it should work.

