I'm doing a flashcards program and I cannot save cards because my .getText method is returning null values and I cannot figure out why:
package eFlashcards;
//lot of imports
public class QuizCardBuilder {
private JTextArea question;
private JTextArea answer;
private ArrayList<QuizCard> cardList;
private JFrame frame;
public static void main(String[] args) {
QuizCardBuilder builder = new QuizCardBuilder();
builder.go();
}
public void go() {
frame = new JFrame("Quiz Card Builder");
JPanel mainPanel = new JPanel();
Font bigFont = new Font("sanserif", Font.BOLD, 24);
question = new JTextArea(6,20);
question.setLineWrap(true);
question.setWrapStyleWord(true);
question.setFont(bigFont);
JScrollPane qScroller = new JScrollPane(question);
qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
answer = new JTextArea(6,20);
answer.setLineWrap(true);
answer.setWrapStyleWord(true);
answer.setFont(bigFont);
JScrollPane aScroller = new JScrollPane(answer);
aScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
aScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
JButton nextButton = new JButton("Next card");
cardList = new ArrayList<QuizCard>();
JLabel qLabel = new JLabel("Question:");
JLabel aLabel = new JLabel("Answer:");
mainPanel.add(qLabel);
mainPanel.add(qScroller);
mainPanel.add(aLabel);
mainPanel.add(aScroller);
mainPanel.add(nextButton);
nextButton.addActionListener(new NextCardListener());
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem newMenuItem = new JMenuItem("New");
JMenuItem saveMenuItem = new JMenuItem("Save");
newMenuItem.addActionListener(new NewMenuListener());
saveMenuItem.addActionListener(new SaveMenuListener());
fileMenu.add(newMenuItem);
fileMenu.add(saveMenuItem);
menuBar.add(fileMenu);
frame.setJMenuBar(menuBar);
frame.add(BorderLayout.CENTER,mainPanel);
frame.setSize(500,600);
frame.setVisible(true);
}
private class NextCardListener implements ActionListener {
public void actionPerformed(ActionEvent ev) {
QuizCard card = new QuizCard(question.getText(),answer.getText());
System.out.println(question);
System.out.println(answer);
cardList.add(card);
clearCard();
}
}
private class SaveMenuListener implements ActionListener {
public void actionPerformed(ActionEvent ev) {
QuizCard card = new QuizCard(question.getText(),answer.getText());
cardList.add(card);
JFileChooser fileSave = new JFileChooser();
fileSave.showSaveDialog(frame);
saveFile(fileSave.getSelectedFile());
}
}
private class NewMenuListener implements ActionListener {
public void actionPerformed(ActionEvent ev) {
cardList.clear();
clearCard();
}
}
private void saveFile(File file) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
for(QuizCard card:cardList) {
writer.write(card.getQuestion() + "/");
writer.write(card.getAnswer() + "\n");
}
writer.close();
} catch(IOException ex) {
System.out.println("Couldn't write the cardList out");
ex.printStackTrace();
}
}
public void clearCard() {
question.setText("");
answer.setText("");
question.requestFocus();
}
}
package eFlashcards;
public class QuizCard {
public String question;
public String answer;
public QuizCard(String q, String a) {
q = question;
a = answer;
}
public String getQuestion() {
return question;
}
public String getAnswer() {
return answer;
}
}
Let me know if more code is needed to understand the problem. I tried debugging, turns out the question and answer variables when I create a new QuizCard object are null by the time of saving...
Your
QuizCardconstructor assigns in the wrong direction. It assigns the fields to the parameters. Prevent this in the future by declaring the parameters asfinal: