1

嘿,我正在制作一个航班预订系统,当我点击一个座位进行预订时出现这个错误“AWT-EventQueue-0”的座位时出现此错误 java.lang.NullPointerException我认为我的 2D 对象初始化有误... PS:有吗一种我可以摆脱final前面的方法Seats[][] seats = new Seats[4][5];

这是我的座位类别:

public class Seats {

private int row_number;
private boolean booked;
private Passenger myPassenger;
private String seat_name;
private String type;
private int column_number;
private long booking_nr;

public Seats(){
    myPassenger = new Passenger();
    booked = false;
}

public boolean isBooked() {
    return booked;
}

public void setBooked(boolean booked) {
    this.booked = booked;
}

}

下面是我的图形用户界面:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class Book_GUI extends JFrame {

//private EconomyClass eco;
private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Book_GUI frame = new Book_GUI();
                frame.setTitle("Economy Class");
                frame.setVisible(true);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Book_GUI() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);

    int left = 3;
    int middle = 3;
    int right = 4;

    String[] singleRowAll = new String [left+middle+right];

    for(int i = 1;i<singleRowAll.length;i++){singleRowAll[i] = "";}
    singleRowAll[0] = "Window";
    singleRowAll[left-1] = "Aisle";
    singleRowAll[left] = "Aisle";
    singleRowAll[left+middle-1] = "Aisle";
    singleRowAll[left+middle] = "Aisle";
    singleRowAll[left+middle+right-1] = "Window";


    //eco = new EconomyClass(4,5,3,3,4);

    final Seats[][] seats = new Seats[4][10];
    for ( int i = 0; i < 4; i++) {
        char c= 'A';
        for ( int j = 0; j < 10; j++) {
            final int x = i;
            final int z = j;
        final JButton btnBookFlight = new JButton(" " + (i+1) + c++ + " " + singleRowAll[j] );
            btnBookFlight.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    if(seats[x][z].isBooked()){btnBookFlight.setBackground(Color.GREEN);}

                    seats[x][z].setBooked(true);

                    //JButton button = (JButton)arg0.getSource();
                    //button.setBackground(Color.RED);
                //  btnBookFlight.setBackground(Color.RED);
                    btnBookFlight.setOpaque(true);
                }
            });
            contentPane.add(btnBookFlight);
        }
    }

    contentPane.setLayout(new GridLayout(4, 10));
    pack();
}

}

感谢您的阅读和您的时间!

4

2 回答 2

1

您没有初始化 Seats[][] 数组。尝试将 Book_GUI.java 的构造函数替换为:

public Seats[][] seats = new Seats[4][10]; 

public Book_GUI() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);

    int left = 3;
    int middle = 3;
    int right = 4;

    String[] singleRowAll = new String[left + middle + right];

    for (int i = 1; i < singleRowAll.length; i++) {
        singleRowAll[i] = "";
    }
    singleRowAll[0] = "Window";
    singleRowAll[left - 1] = "Aisle";
    singleRowAll[left] = "Aisle";
    singleRowAll[left + middle - 1] = "Aisle";
    singleRowAll[left + middle] = "Aisle";
    singleRowAll[left + middle + right - 1] = "Window";

    // eco = new EconomyClass(4,5,3,3,4);

            //Delete this line:
    //final Seats[][] seats = new Seats[4][5];

    for(int i = 0; i < 4; i++) {
        for(int j = 0; j < 5; j++) {
            seats[i][j] = new Seats();
        }
    }

    for (int i = 0; i < 4; i++) {
        char c = 'A';
        for (int j = 0; j < 10; j++) {
            final int x = i;
            final int z = j;
            final JButton btnBookFlight = new JButton(" " + (i + 1) + c++
                    + " " + singleRowAll[j]);
            btnBookFlight.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    if (seats[x][z].isBooked()) {
                        btnBookFlight.setBackground(Color.GREEN);
                    }

                    seats[x][z].setBooked(true);

                    // JButton button = (JButton)arg0.getSource();
                    // button.setBackground(Color.RED);
                    // btnBookFlight.setBackground(Color.RED);
                    btnBookFlight.setOpaque(true);
                }
            });
            contentPane.add(btnBookFlight);
        }
    }

    contentPane.setLayout(new GridLayout(4, 10));
    pack();
}

这应该有帮助:)

于 2013-06-17T22:11:54.043 回答
1

您正在使用 创建一个新的座位数组,new Seats[4][5]但这只会创建一个空的引用数组。您实际上需要创建一个位于阵列中每个位置的新座位。

于 2013-06-17T22:11:47.857 回答