Hi,
I am trying to develop an application that has got a Jbutton and allows to search a file and then loads selected line of that file in the JTable. I am able to do that when I hard code it inside the code but when I try to let the user do it by adding FileChooser I am able getting runtime error like this:-
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Testing.TableCreator$1.actionPerformed(TableCreator.java:69)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6041)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5806)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4413)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2440)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
Here is the line the error report is referring to :-
File file = fc.getSelectedFile();
Can somebody guide me where am I making mistakes ?
Here is my code below:-
public class TableCreator {
public static JFrame fr = new JFrame("Item Menu");
Container c = new JPanel();
public JFileChooser fc;
public JTable table;
public TableCreator() {
JLabel jLabel1= new JLabel("Item Name");
JLabel jLabel4= new JLabel("File Name");
JLabel jLabel7 = new javax.swing.JLabel();
JButton jButton3 = new javax.swing.JButton();
jLabel7.setText("List :");
jButton3.setText("Browse");
//jButton3.addMouseListener(new FileOpenMouseListener());
jButton3.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser(".");
int status = fileChooser.showOpenDialog(c);
if (status == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
constructTable(file);
JScrollPane scroll = new JScrollPane(table);
c.add(scroll);
//c.pack();
} else if (status == JFileChooser.CANCEL_OPTION) {
System.out.println("canceled");
}
}
}
);
// Check Boxes start here
JCheckBox checkFixed = new JCheckBox("Pepsi");
JCheckBox dontFix = new JCheckBox("Coke");
checkFixed.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("coke");
}
}
);
dontFix.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("pepsi");
}
}
);
// Check Boxes End here
// Text Fields start here
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
jTextField1.setText("Item Name");
System.out.println(jTextField1.getText()); // get filename for C / C++ program
System.out.println(jTextField2.getText());
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("action");
}
});
jTextField2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("action");
}
});
// Text Fields end here
// define ScrollPane, Buttons and Mouse Listners
JScrollPane scroll = new JScrollPane(table);
JButton button1 = new JButton("Order");
JButton button2 = new JButton("No Thanks");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("call order program");
}
});
button2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
JPanel three = new JPanel();
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(three);
three.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
//.addComponent(jLabel2)
.addComponent(jLabel1))
.addGap(24, 24, 24)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
//.addComponent(comboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(498, Short.MAX_VALUE)
.addComponent(button1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(button2)
.addGap(30, 30, 30))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(scroll, javax.swing.GroupLayout.DEFAULT_SIZE, 629, Short.MAX_VALUE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
//.addComponent(jLabel5)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
//.addComponent(jLabel3)
.addComponent(jLabel4))
.addGap(27, 27, 27))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
//.addComponent(jLabel6)
.addComponent(jLabel7))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton3)
//.addComponent(comboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(dontFix)
.addComponent(checkFixed))))
.addContainerGap(370, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
//.addComponent(jLabel2)
//.addComponent(comboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
)
.addGap(33, 33, 33)
//.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
//.addComponent(comboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
//.addComponent(jLabel3)
)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton3)
.addComponent(jLabel4))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(dontFix)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(checkFixed))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel7)))
.addGap(1, 1, 1)
//.addComponent(jLabel6)
.addGap(18, 18, 18)
.addComponent(scroll, javax.swing.GroupLayout.PREFERRED_SIZE, 359, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(button2)
.addComponent(button1))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
c.add(three);
fr.add(c);
}
private void constructTable(File file) {
final DefaultTableModel dm = new DefaultTableModel();
try {
Scanner scan = new Scanner(file);
if (scan.hasNextLine()) {
final String line = scan.nextLine();
String col0 = "Field#";
String col1 = "Field Name";
final String col2 = "Menu";
final String col3 = "Item";
dm.addColumn(col0);
dm.addColumn(col1);
dm.addColumn(col2);
dm.addColumn(col3);
String arr[] = line.split("\t");
for(int i=0; i<arr.length;i++){
dm.insertRow(i,new Object[]{i+1,arr[i]});
System.out.println(arr[i]);
}
TableColumn column0 = table.getColumnModel().getColumn(0);
column0.setPreferredWidth(1);
TableColumn column1 = table.getColumnModel().getColumn(1);
column1.setPreferredWidth(160);
TableColumn column2 = table.getColumnModel().getColumn(2);
column2.setPreferredWidth(240);
TableColumn column3 = table.getColumnModel().getColumn(3);
column3.setPreferredWidth(1);
table = new JTable(dm);
table.setPreferredScrollableViewportSize(new Dimension(800,400));
table.setFillsViewportHeight(true);
String[] answer = { "Item"};
String[] keyword = {"Detail"};
int count = dm.getRowCount();
table.getColumn(col2).setCellRenderer(new RadioButtonRenderer(answer));
table.getColumn(col2).setCellEditor(new RadioButtonEditor(new RadioButtonPanel(answer)));
table.getColumn(col3).setCellRenderer(new CheckBoxRenderer(keyword));
table.getColumn(col3).setCellEditor(new CheckBoxEditor(new CheckBoxPanel(keyword)));
}
} catch (FileNotFoundException ev) {
ev.printStackTrace();
}
}
// Cell base
class RadioButtonPanel extends JPanel {
private JRadioButton[] buttons;
private JRadioButton noSelectionButton;
RadioButtonPanel(String[] str) {
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
buttons = new JRadioButton[str.length];
ButtonGroup verticalGroup = new ButtonGroup();
ButtonGroup horizontalGroup = new ButtonGroup();
noSelectionButton = new JRadioButton();
verticalGroup.add(noSelectionButton);
horizontalGroup.add(noSelectionButton);
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JRadioButton(str[i]);
buttons[i].setFocusPainted(false);
add(buttons[i]);
verticalGroup.add(buttons[i]);
}
for (int j = 0; j < buttons.length; j++) {
buttons[j] = new JRadioButton(str[j]);
buttons[j].setFocusPainted(false);
horizontalGroup.add(buttons[j]);
}
}
public void addActionListener(ActionListener anActionListener) {
for (int i = 0; i < buttons.length; i++) {
buttons[i].addActionListener(anActionListener);
}
}
public void removeActionListener(ActionListener anActionListener) {
for (int i = 0; i < buttons.length; i++) {
buttons[i].removeActionListener(anActionListener);
}
}
public void setSelectedIndex(int index) {
if (index < 0 || index >= buttons.length) {
noSelectionButton.setSelected(true);
return;
}
for (int i = 0; i < buttons.length; i++) {
if (i == index) {
buttons[i].setSelected(true);
return;
}
}
}
public int getSelectedIndex() {
for (int i = 0; i < buttons.length; i++) {
if (buttons[i].isSelected()) {
return i;
}
}
return -1;
}
public JRadioButton[] getButtons() {
return buttons;
}
}
class CheckBoxPanel extends JPanel {
private JCheckBox[] buttons;
private JRadioButton noSelectionButton;
CheckBoxPanel(String[] str) {
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
buttons = new JCheckBox[str.length];
ButtonGroup group = new ButtonGroup();
noSelectionButton = new JRadioButton();
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JCheckBox(str[i]);
buttons[i].setFocusPainted(false);
add(buttons[i]);
group.add(buttons[i]);
}
}
public void addActionListener(ActionListener anActionListener) {
for (int i = 0; i < buttons.length; i++) {
buttons[i].addActionListener(anActionListener);
}
}
public void removeActionListener(ActionListener anActionListener) {
for (int i = 0; i < buttons.length; i++) {
buttons[i].removeActionListener(anActionListener);
}
}
public void setSelectedIndex(int index) {
if (index < 0 || index >= buttons.length) {
noSelectionButton.setSelected(true);
return;
}
for (int i = 0; i < buttons.length; i++) {
if (i == index) {
buttons[i].setSelected(true);
return;
}
}
}
public int getSelectedIndex() {
for (int i = 0; i < buttons.length; i++) {
if (buttons[i].isSelected()) {
return i;
}
}
return -1;
}
public JCheckBox[] getButtons() {
return buttons;
}
}
class RadioButtonRenderer extends RadioButtonPanel implements TableCellRenderer {
RadioButtonRenderer(String[] strs) {
super(strs);
}
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
if (value instanceof Integer) {
setSelectedIndex(((Integer)value).intValue());
}
return this;
}
}
class CheckBoxRenderer extends CheckBoxPanel implements TableCellRenderer {
CheckBoxRenderer(String[] strs) {
super(strs);
}
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
if (value instanceof Integer) {
setSelectedIndex(((Integer)value).intValue());
}
return this;
}
}
class RadioButtonEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
private RadioButtonPanel theRadioButtonPanel;
public RadioButtonEditor(RadioButtonPanel aRadioButtonPanel) {
theRadioButtonPanel = aRadioButtonPanel;
theRadioButtonPanel.addActionListener(this);
}
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) {
if (value instanceof Integer) {
theRadioButtonPanel.setSelectedIndex(((Integer) value).intValue());
}
return theRadioButtonPanel;
}
public Object getCellEditorValue() {
return new Integer(theRadioButtonPanel.getSelectedIndex());
}
public void actionPerformed(ActionEvent e) {
fireEditingStopped();
}
}
class CheckBoxEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
private CheckBoxPanel theRadioButtonPanel;
public CheckBoxEditor(CheckBoxPanel aRadioButtonPanel) {
theRadioButtonPanel = aRadioButtonPanel;
theRadioButtonPanel.addActionListener(this);
}
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) {
if (value instanceof Integer) {
theRadioButtonPanel.setSelectedIndex(((Integer) value).intValue());
}
return theRadioButtonPanel;
}
public Object getCellEditorValue() {
return new Integer(theRadioButtonPanel.getSelectedIndex());
}
public void actionPerformed(ActionEvent e) {
fireEditingStopped();
}
}
public static void main(String[] args) {
TableCreator wow = new TableCreator();
TableCreator.fr.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
TableCreator.fr.pack();
TableCreator.fr.setVisible(true);
}
}
Thanks