0

when I try to run this program the following error message appears:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Quiz1.<init>(Quiz1.java:21)
at Opening_Screen.jButton1ActionPerformed(Opening_Screen.java:138)
at Opening_Screen.access$000(Opening_Screen.java:16)
at Opening_Screen$1.actionPerformed(Opening_Screen.java:57)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)    

As I'm extremely new to Java I have absolutely no idea what any of this means, if someone could show me where in my code this is happening I'd be extremely appreciative!

Here is the code.

public class Quiz1 extends javax.swing.JFrame {

ArrayList <String> quiz1Answers = new ArrayList();
ArrayList <String> quiz1UserAnswers = new ArrayList();

String q1 = this.txtInputQ1.getText();
String q2 = this.txtInputQ2.getText();
String q3 = this.txtInputQ3.getText();
String q4 = this.txtInputQ4.getText();
String q5 = this.txtInputQ5.getText();
String q6 = this.txtInputQ6.getText();
String q7 = this.txtInputQ7.getText();
String q8 = this.txtInputQ8.getText();

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    JOptionPane.showMessageDialog(null, "This is a practice quize to test what you have learned.", "Instructions", +  JOptionPane.INFORMATION_MESSAGE);
    JOptionPane.showMessageDialog(null, "<html>To check your answers press the <b><i>'Check'</b></i> button and enter which question to check", "Instructions", +  JOptionPane.INFORMATION_MESSAGE);
    JOptionPane.showMessageDialog(null, "<html>When completed click the <b><i>'Start Quiz'</b></i> button", "Instructions", +  JOptionPane.INFORMATION_MESSAGE);
}                                        

private void txtInputQ6ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    // TODO add your handling code here:
}                                          

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    if (q1.equals("") || q2.equals("") || q3.equals("") || q4.equals("") || q5.equals("") || q6.equals("") || q7.equals("") || q8.equals(""))
    {
        JOptionPane.showMessageDialog(null, "Make sure all fields are filled in!", "Error!", +  JOptionPane.ERROR_MESSAGE);
    }
    else
    {
        quiz1UserAnswers.add(q1);
        quiz1UserAnswers.add(q2);
        quiz1UserAnswers.add(q3);
        quiz1UserAnswers.add(q4);
        quiz1UserAnswers.add(q5);
        quiz1UserAnswers.add(q6);
        quiz1UserAnswers.add(q7);
        quiz1UserAnswers.add(q8);
        JOptionPane.showMessageDialog(null, "<html>Press the <b><i>'Check Answer'</b></i> button now!", "", +  JOptionPane.INFORMATION_MESSAGE);
    }
}                                        

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
}                                        

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    Collections.addAll(quiz1Answers, "ottawa", "toronto", "tokyo", "northamerica", "seoul", "Asia", "unitedstates", "50");

}      

Thank you in advance!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
AJC
  • 133
  • 1
  • 2
  • 12
  • 1
    Google for Null Pointer Exception. Or rather, search on this site for it. The problem lies here `String q1 = this.txtInputQ1.getText();` You are trying to use the `this` keyword before an object is created. In fact, I don't see where you create any of the `txtInputQ1`, `txtInputQ2`, etc... The only objects you have created are the 2 ArrayLists. – takendarkk Jul 20 '14 at 22:19
  • @Takendarkk Will do! Ah, dumb mistake on my part. Anyways thank you very much for the help! – AJC Jul 20 '14 at 22:27
  • Sure thing. If you do some research and still have problems you can edit your question or open a new one. – takendarkk Jul 20 '14 at 22:28
  • Important thing to note is that you should be using `List quiz1Answers = new ArrayList();` – EpicPandaForce Jul 20 '14 at 22:39

1 Answers1

0

I'm probably going to assume that you're trying to get the values before even initializing the object. Make sure that the object txtInputQ1 till txtInputQ2 is first created before you call the .getText(); method.

Talha Tanveer
  • 1,196
  • 8
  • 16