made a card
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at QuizCardPlayer.showNextCard(QuizCardPlayer.java:172)
at QuizCardPlayer$NextCardListener.actionPerformed(QuizCardPlayer.java:106
import javax . swing . *;
import java . awt . *;
import java . awt . event . * ;
import java . io . * ;
import java . util . * ;
public class QuizCardPlayer
{
JTextArea display ;
JTextArea answer ;
ArrayList<QuizCard> cardList ;
JFrame frame;
boolean isShowAnswer ;
int currentCardIndex ;
QuizCard currentCard;
JButton nextButton;
public static void main (String[] args)
{
QuizCardPlayer qcp = new QuizCardPlayer ( );
qcp . go ( );
}
public void go ( )
{
frame = new JFrame ("Quiz Card Player");
frame . setDefaultCloseOperation (JFrame . EXIT_ON_CLOSE);
Font bigFont = new Font ("微软雅黑" , Font . PLAIN , 15 ) ;
JPanel mainPanel = new JPanel ( ) ;
JTextArea display = new JTextArea ( 10 , 10) ;
display . setLineWrap (true) ;
display . setWrapStyleWord (true) ;
display . setFont (bigFont) ;
JScrollPane qScroller = new JScrollPane ( display ) ;
qScroller . setVerticalScrollBarPolicy (ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS) ;
qScroller . setHorizontalScrollBarPolicy (ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER) ;
JButton nextButton = new JButton ("Begin") ;
nextButton . addActionListener ( new NextCardListener ( ) );
JMenuBar menuBar = new JMenuBar ( ) ;
JMenu fileMenu = new JMenu ("file");
JMenuItem loadMenuItem = new JMenuItem ("Load card set");
loadMenuItem . addActionListener ( new LoadMenuListener ( ) );
fileMenu . add (loadMenuItem);
menuBar . add (fileMenu);
mainPanel . setLayout ( new BoxLayout (mainPanel , BoxLayout . Y_AXIS)) ;
mainPanel. add (menuBar) ;
mainPanel. add (qScroller);
mainPanel. add (nextButton);
frame. getContentPane ( ) . add (BorderLayout . CENTER, mainPanel) ;
frame . setSize (500, 600) ;
frame . setVisible (true);
}
public class LoadMenuListener implements ActionListener
{
public void actionPerformed (ActionEvent ev)
{
JFileChooser fileOpen = new JFileChooser ( );
fileOpen . showOpenDialog (frame) ;
loadFile (fileOpen. getSelectedFile ( ));
}
}
public class NextCardListener implements ActionListener
{
public void actionPerformed (ActionEvent ev)
{
if (isShowAnswer)
{
display . setText (currentCard . getAnswer ( ) );
nextButton . setText ("Next Card");
isShowAnswer = false;
}
else
{
if (currentCardIndex < cardList . size ( ))
{
showNextCard ( );
}
else
{
display . setText ("That's the last card ");
nextButton .setEnabled (false);
}
}
}
}
void loadFile (File file)
{
cardList = new ArrayList <QuizCard> ( );
try
{
BufferedReader reader = new BufferedReader (new FileReader (file)) ;
String line = null ;
while ( (line = reader . readLine ( )) != null)
{
makeCard (line) ;
}
reader . close ( ) ;
}
catch (Exception ex)
{
System. out . println ("Could't make a card") ;
ex. printStackTrace ( ) ;
}
}
void makeCard ( String lineToParse )
{
String [ ] result = lineToParse . split ("/") ;
/*display = new JTextArea ( ) ;
QuizCard card = new QuizCard (result [0] , result [1] ) ;
cardList . add (car
System .out. println ("made a card") ;
}
void showNextCard ( )
{
currentCard = cardList . get (currentCardIndex);
currentCardIndex ++;
display . setText (currentCard . getQuestion ( ));
nextButton . setText ("Show Answer");
isShowAnswer = true;
}
}
class QuizCard
{
String question;
String answer;
public QuizCard (String q , String a)
{
question = q;
answer = a;
}
public String getQuestion ( )
{
return question;
}
public String getAnswer ( )
{
return answer ;
}
}