Java NullPointerException when calling method - within JApplet

FratBro23
Category:
Programming
Price: $10 USD

Question description

/*

 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.

 */

package EddieExamples;

import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

//import javax.swing.JFrame;

import javax.swing.JTextField;

import javax.swing.JApplet;

import javax.swing.JOptionPane;

/**

 *

 * @author Ramon

 */

public class Assign7ex extends JApplet implements ActionListener {

    boolean econfull = false;

    boolean firstfull = false;

    boolean[] seats;

    //

    JButton firstBtn;

    JButton econBtn;

    JTextField input;

    boolean isFirst = false;

    int seated = 0;

           

    @Override

   public void init(){  // sets up window for display - JLabel and JTextField

        for (int t = 0; t == 11; t++) {

            seats[t] = false;

        }      

      // sets up container window

      Container c = getContentPane();

      c.setLayout( new FlowLayout() );

     

      // adds JLabel

      firstBtn = new JButton();

      firstBtn.setText("First Class");

      add(firstBtn);

      firstBtn.addActionListener(this);

     

      econBtn = new JButton();

      econBtn.setText("Economy");

      add(econBtn);

      econBtn.addActionListener(this);

     

      // adds JTextField and connects it to actionListener

      input = new JTextField(25);

      add(input);

   }

    @Override

    public void actionPerformed(ActionEvent e) {

    Assign7ex assigner = new Assign7ex();    

    if (e.getSource() == firstBtn) {

            input.setText("1st button clicked");

            isFirst = true;

            seated = reserve(isFirst);  // ERROR HAPPENS HERE

            System.out.println("I am here!");

            switch (seated) {

            case 1: case 2: case 3: case 4: case 5:

                input.setText("Boarding Pass: Seat " + seated + " First Class");

                break;

            case 6: case 7: case 8: case 9: case 10:

                input.setText("Boarding Pass: Seat " + seated + " Economy Class");

                break;

        }

    } else if (e.getSource() == econBtn) {

            input.setText("2nd button clicked");

            isFirst = false;

            System.out.println("I am big!");

            assigner.seated = assigner.reserve(isFirst); // ERROR HAPPENS HERE

    }

    //seated = reserve(isFirst);

        switch (seated) {

            case 1: case 2: case 3: case 4: case 5:

                input.setText("Boarding Pass: Seat " + seated + " First Class");

                break;

            case 6: case 7: case 8: case 9: case 10:

                input.setText("Boarding Pass: Seat " + seated + " Economy Class");

                break;

        }

        if (econfull == true && firstfull == true) {

            seated = -2;

        }

    System.out.println("Airplane is Full! - Thank you for playing Airplane Seating!");

    }

     public int reserve(boolean isFirstClass) {

       

        boolean done = false;

        int seat = 0;

       

        if (econfull == true && firstfull == true) {

            return -2;

        }

        if (isFirstClass == true && firstfull != true) {

            for (int i = 1; i <= 5; i++) {

                if (done == false && seats[i] == false) {

                    seats[i] = true;

                    done = true;

                    seat = i;

                    return seat;

                }

            }

            if (done == false) {

                firstfull = true;

                if (econfull == false) {

                String n = JOptionPane.showInputDialog(null,

                            "Are you OK with Economy Class? (click OK or cancel)",

                            "Change of Class", 3);

                if (n != null) {

                        System.out.println("Hello OK");

                    }

                } else {

                        System.out.println("Hello CANCEL");

                        }

                }

        } else if (econfull != true && isFirstClass == false) {

            done = false;

            for (int i = 6; i <= 10; i++) {

                if (done == false && seats[i] == false) {

                    seats[i] = true;

                    done = true;

                    seat = i;

                    return seat;

                }

            }

            if (done == false) {

                econfull = true;

                if (firstfull == false) {

                String n = JOptionPane.showInputDialog(null,

                            "Are you OK with First Class? (click OK or cancel)",

                            "Change of Class", 3);

                if (n != null) {

                        System.out.println("Hello OK");

                    }

                } else {

                        System.out.println("Hello CANCEL");

                        }

                }

        }

return -2;

}

}

Tutor Answer

(Top Tutor) Daniel C.
(997)
School: University of Maryland
PREMIUM TUTOR
Studypool has helped 1,244,100 students
Ask your homework questions. Receive quality answers!

Type your question here (or upload an image)

1831 tutors are online

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors