Hi seem to be getting a null pointer exception. I think its occuring in the delete() function.
//Reservation.java - class that encapsulates the data of the hotel
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.border.*;
class Reservation{
private int id;
private String name;
private String surname;
private String start;
private String end;
private String type;
private String amount;
public Reservation(){
this(0, null, null, null, null, null, null);
}
public Reservation(int id, String name, String surname, String start, String end, String type, String amount){
this.id = id;
this.name = name;
this.surname = surname;
this.start = start;
this.end = end;
this.type = type;
this.amount = amount;
}
//setters
public void setId(int id){
this.id = id;
}
public void setNeme(String name){
this.name = name;
}
//getters
public int getId(){
return id;
}
public String getName(){
return name;
}
public String getSurname(){
return surname;
}
public String getStart(){
return start;
}
public String getEnd(){
return end;
}
public String getType(){
return type;
}
public String getAmount(){
return amount;
}
}//end of Reservation class
//Hotel.java - Program that displays Gui and allows data to be modified
class Hotel extends JFrame implements ActionListener{
int id =0;
int i = 0;
int col = 0;
int row = 0;
private static final int MAX = 10 + 1;
Object [][] data =new Object[MAX][7];
int rows, cols;
private Reservation[] reservation = new Reservation[MAX];
//table variables
String[] headings = new String[] {"Id", "Name", "Surname", "Check-in", "Check-out", "Room type", "Amount"};
/*TableModel dataModel = new AbstractTableModel() {
public String getColumnName(int column){ return headings[column];}
public int getColumnCount() { return 7; }
public int getRowCount() { return 10;}
public Object getValueAt(int row, int col) { return new Integer(row*col); }
};*/
//Second frame
JFrame g = new JFrame("View all");
JTable table = new JTable(data, headings);
JScrollPane scrollpane = new JScrollPane(table);
//global variables
String nm;
JTextField idField = new JTextField(15);
JTextField field = new JTextField(15);
JTextField surnameField = new JTextField(15);
JTextField startField = new JTextField(15);
JTextField endField = new JTextField(15);
JTextField typeField = new JTextField(15);
JTextField amountField = new JTextField(15);
JButton next = new JButton("New Reservation");
JButton view = new JButton("View all");
JButton delete = new JButton("Delete Reservation");
JButton modify = new JButton("Modify Reservation");
JButton records = new JButton("View Records");
JButton calendar = new JButton("Calendar");
JLabel idLabel = new JLabel("Id");
JLabel nameLabel = new JLabel("Name");
JLabel surnameLabel = new JLabel("Surname");
JLabel startLabel = new JLabel("Start");
JLabel endLabel = new JLabel("End");
JLabel typeLabel = new JLabel("Type");
JLabel amountLabel = new JLabel("Amount");
JPanel zero = new JPanel();
JPanel one = new JPanel();
JPanel two = new JPanel();
JPanel three = new JPanel();
JPanel border = new JPanel();
//Hotel constructor
public Hotel(){
super("Hotel v1.0");
setSize(650, 400);
setLocation(0, 0);
amountField.addActionListener(this);
next.addActionListener(this);
view.addActionListener(this);
delete.addActionListener(this);
Container content = getContentPane();
content.setLayout(new FlowLayout());
String filename = "Header.jpg";
Image image = Toolkit.getDefaultToolkit().getImage(filename);
Component img = new ImageComponent(image);
content.add(img);
zero.add(idLabel);
zero.add(idField);
idField.setEditable(false);
one.add(nameLabel);
one.add(field);
one.add(surnameLabel);
one.add(surnameField);
content.add(zero);
content.add(one);
two.add(startLabel);
two.add(startField);
two.add(endLabel);
two.add(endField);
two.add(typeLabel);
two.add(typeField);
content.add(two);
three.add(amountLabel);
three.add(amountField);
three.add(next);
content.add(three);
border.add(view);
border.add(delete);
border.add(modify);
border.add(records);
border.add(calendar);
border.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
content.add(border);
idField.setText(assign());
field.setText(null);
surnameField.setText(null);
startField.setText(null);
endField.setText(null);
typeField.setText(null);
amountField.setText(null);
Container content2 =g.getContentPane();
content2.add(scrollpane);
g.setLocation(675, 0);
g.setSize(450, 400);
g.setVisible(true);
g.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){g.setVisible(false);}
});
}
public void setValueAt(Object value, int row, int col) { data[row][col] = value; }
public void actionPerformed(ActionEvent e){
if(e.getSource()==view){
g.setVisible(true);
}
if(e.getSource()==delete){
String delString = JOptionPane.showInputDialog(null, "Enter id to delete");
int choice = Integer.valueOf(delString)-1;
int result = JOptionPane.showConfirmDialog(null, "Delete: "+reservation[choice].getName() +" " +reservation[choice].getSurname());
switch (result){
case JOptionPane.YES_OPTION:
delete(choice);break;
case JOptionPane.NO_OPTION:
break;
case JOptionPane.CANCEL_OPTION:
break;
case JOptionPane.CLOSED_OPTION:
break;
}
}
if(e.getSource()==next){
idField.setText(assign());
field.setText(null);
surnameField.setText(null);
startField.setText(null);
endField.setText(null);
typeField.setText(null);
amountField.setText(null);
field.requestFocus();
}
if(e.getSource()==amountField){
String id = idField.getText();
String n = field.getText();
String s = surnameField.getText();
String st = startField.getText();
String en = endField.getText();
String t = typeField.getText();
String a = amountField.getText();
try{
int idInt = Integer.parseInt(id);
//Get all user input and call constructor,
reservation=new Reservation(idInt,n, s, st, en, t, a);
setTable();
}catch (ArrayIndexOutOfBoundsException ex){JOptionPane.showMessageDialog(null, "The system is full");}
//catch(NumberFormatException nfe) {JOptionPane.showMessageDialog(null, "Validation Error");}
}
}
public void delete(int choice){
reservation[choice] = new Reservation();
for (i = 0; i<MAX; i++){
setTable();
}
}
public void setTable(){
int idValue = reservation[i].getId();
setValueAt(idValue, row, col);
col++;
String value = reservation[i].getName();
setValueAt(value, row, col);
col++;
value = reservation[i].getSurname();
setValueAt(value, row, col);
col++;
value = reservation[i].getStart();
setValueAt(value, row, col);
col++;
value = reservation[i].getEnd();
setValueAt(value, row, col);
col++;
value = reservation[i].getType();
setValueAt(value, row, col);
col++;
value = reservation[i].getAmount();
setValueAt(value, row, col);
i++;
row++;
col=0;
//table.revalidate();
// table.tableChanged(new TableModelEvent(table.getModel()));
//This one works:
SwingUtilities.updateComponentTreeUI(scrollpane);
}
public String assign(){
id++;
return String.valueOf(id);
}
public static void main(String[] args){
JFrame f = new Hotel();
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){System.exit(0);}
});
f.setVisible(true);
}
}