So I'm trying to add two matrices based on input from the user, using two tables and a third to display the result. The result table displays the results of the addition in all cells, except the last cell. and i receive an error :
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
Netbeans indicates that this error is at the block of code where i perform the addition. Here's my code
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
*
* @author Kelvin Wellington
*
*
*/
public class MatrixApp extends javax.swing.JFrame {
DefaultTableModel tableModelA = new DefaultTableModel();
DefaultTableModel tableModelB = new DefaultTableModel();
DefaultTableModel tableModelR = new DefaultTableModel();
public int rowA;
public int rowB;
public int columnA;
public int columnB;
/** Creates new form MatrixApp
*/
public MatrixApp() {
initComponents();
matrixTab.setEnabledAt(1,false);
matrixTab.setEnabledAt(2,false);
// matrixTab.setEnabledAt(3,false);
}
@SuppressWarnings
//Generated code
private void firstTabNextActionPerformed(java.awt.event.ActionE vent evt) {
rowA = Integer.parseInt(matATextField1.getText());
columnA = Integer.parseInt(matATextField2.getText());
rowB = Integer.parseInt(matBTextField1.getText());
columnB = Integer.parseInt(matBTextField2.getText());
if (rowA != rowB || columnA != columnB){
JOptionPane.showMessageDialog(null, "Illegal matrix dimensions.", "Error", JOptionPane.WARNING_MESSAGE);
}
else{
enableTabs();
matrixTab.setSelectedIndex(matrixTab.getSelectedIn dex()+1);
matrixATable.setModel(tableModelA);
tableModelA.setColumnCount(columnA);
tableModelA.setRowCount(rowA);
matrixBTable.setModel(tableModelB);
tableModelB.setColumnCount(columnB);
tableModelB.setRowCount(rowB);
}
}
private void ExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void matABackActionPerformed(java.awt.event.ActionEvent evt) {
matrixTab.setSelectedIndex(matrixTab.getSelectedIn dex() -1);
}
private void matANextActionPerformed(java.awt.event.ActionEvent evt) {
matrixTab.setSelectedIndex(matrixTab.getSelectedIn dex()+1);
}
private void matBBackActionPerformed(java.awt.event.ActionEvent evt) {
matrixTab.setSelectedIndex(matrixTab.getSelectedIn dex()-1);
}
private void matBNextActionPerformed(java.awt.event.ActionEvent evt) {
matrixAddition();
}
private void startButtonActionPerformed(java.awt.event.ActionEv ent evt) {
matrixTab.setSelectedIndex(matrixTab.getSelectedIn dex() - 3);
}
private void resultNextActionPerformed(java.awt.event.ActionEve nt evt) {
matrixTab.setSelectedIndex(matrixTab.getSelectedIn dex()+1);
}
private void loadButtonActionPerformed(java.awt.event.ActionEve nt evt) {
//
}
private void saveButtonActionPerformed(java.awt.event.ActionEve nt evt) {
//
}
private void matATextField1ActionPerformed(java.awt.event.Actio nEvent evt) {
//
}
public void enableTabs(){
matrixTab.setEnabledAt(matrixTab.getSelectedIndex( )+1,true);
matrixTab.setEnabledAt(matrixTab.getSelectedIndex( )+2,true);
}
public void matrixAddition() {
matrixTab.setSelectedIndex(matrixTab.getSelectedIn dex()+1);
resultTable.setModel(tableModelR);
tableModelR.setColumnCount(columnA);
tableModelR.setRowCount(rowA);
resultTable.setModel(tableModelR);
tableModelR.setColumnCount(columnA);
tableModelR.setRowCount(rowA);
Integer sum=0;
for (int i = 0; i<Integer.parseInt(matATextField1.getText()); i++){
for (int j=0; j<Integer.parseInt(matATextField2.getText()); j++){
sum = Integer.parseInt(tableModelA.getValueAt(i,j).toStr ing()) + Integer.parseInt(tableModelB.getValueAt(i,j).toStr ing());
tableModelR.setValueAt(sum, i, j);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
//......
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MatrixApp().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton Exit;
private javax.swing.JRadioButton addition;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton firstTabNext;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JPanel jPanel5;
private javax.swing.JPanel jPanel6;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JButton loadButton;
private javax.swing.JButton matABack;
private javax.swing.JButton matANext;
private javax.swing.JTextField matATextField1;
private javax.swing.JTextField matATextField2;
private javax.swing.JButton matBBack;
private javax.swing.JButton matBNext;
private javax.swing.JTextField matBTextField1;
private javax.swing.JTextField matBTextField2;
private javax.swing.JTable matrixATable;
private javax.swing.JTable matrixBTable;
private javax.swing.JTabbedPane matrixTab;
private javax.swing.JRadioButton multiplication;
private javax.swing.JButton resultNext;
private javax.swing.JTable resultTable;
private javax.swing.JButton saveButton;
private javax.swing.JButton startButton;
private javax.swing.JRadioButton subtraction;
// End of variables declaration
}