Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: addin two matrices.no display in final cell in the result table:java.lang.NullPointerException error

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default addin two matrices.no display in final cell in the result table:java.lang.NullPointerException error

    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
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: addin two matrices.no display in final cell in the result table:java.lang.NullPointerException error

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    There is a variable with a null value on the line where the exception happened. Look at the line, find the variable (use println() if need to see which variable is null) and backtrack in the code to see why that variable does not have a valid non-null value.

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  2. [SOLVED] Error Message: Exception in thread "main" java.lang.NullPointerException etc.
    By coffecupcake in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2011, 09:34 AM
  3. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  4. [SOLVED] Java run time error: 'java.lang.NullPointerException"
    By ChandanSharma in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 20th, 2011, 11:53 AM
  5. Getting "AWT-EventQueue-0" java.lang.NullPointerException error
    By tryingtoJava in forum AWT / Java Swing
    Replies: 9
    Last Post: September 21st, 2009, 10:46 PM

Tags for this Thread