我收到了java.lang.NullPointerException

时间:2012-11-05 19:11:16

标签: java user-interface nullpointerexception

我有一个在底部实现smithWaterman算法的类,我已经决定将main放在GUI中并将其打印出矩阵。我一直得到NullPointerException。我不知道如何解决它,有没有人看到错误?谢谢。

    package main;

    import java.util.*;
    import java.util.*;
    import java.applet.Applet;
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.List;
    import java.lang.Math;
    import javax.swing.DefaultListModel;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextField;
    import javax.swing.JTextPane;
    import javax.swing.ListModel;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.table.TableColumn;
    import javax.swing.table.TableModel;
    import java.lang.Math;

    /**
     * Smith-Waterman Algorithm:
     * 
     * @author Jomana Sherif Final Project SE 165 Fall2012
     */
    public class smithWaterman2 {

        // Initializing the two array lists of the input
        StringBuffer store1 = new StringBuffer(0);
        StringBuffer store2 = new StringBuffer(0);
        String input1 = null;
        String input2 = null;
        double cell[][];
        double matchValue = 1.0;
        double misMatchValue = -0.3;
        double diagonal = 0.0;
        double gapValue = 0.3;
        double max = 0.0;
        int maxacross = 0;
        int maxdown = 0;

        /**
         * Print Matrix
         * 
         * @param args
         */
        public void printMatrix() {
            System.out.print(" | ");
            for (int i = 0; i < input2.length(); i++) {
                System.out.print(input2.charAt(i) + "  | ");

            }
            System.out.println("  ");
            for (int across = 0; across < input1.length(); across++) {

                for (int down = 0; down < input2.length(); down++) {
                    if (down == 0) {
                        System.out.print(input1.charAt(across) + "|");
                    }
                    System.out.printf("%1$.1f", cell[across][down]);

                    System.out.print(" |");
                }
                System.out.println(" ");
            }

        }

        public void generate() {
            /**
             * Creating the NxN matrix
             * 
             * @param args
             */

            cell = new double[input1.length()][input2.length()];

            /**
             * Computation
             * 
             * @param args
             */
            for (int across = 0; across < input1.length(); across++) {
                for (int down = 0; down < input2.length(); down++) {
                    // Factor 1-3 goes here

                    /**
                     * Factor 1
                     */
                    double match = 0.0;
                    double f2 = 0.0;
                    double f3 = 0.0;

                    if (input1.charAt(across) == (input2.charAt(down))) {
                        match = matchValue;

                    } else {
                        match = misMatchValue;
                    }
                    // diagonal
                    if (across < 1 || down < 1) {
                        diagonal = 0;
                    } else {
                        diagonal = cell[across - 1][down - 1];
                    }
                    double f1 = diagonal + match;

                    /**
                     * Factor 2
                     */
                    for (int k = across - 1; k >= 0; k--) {
                        if (cell[k][down] != 0) {
                            f2 = cell[k][down] - (gapValue * (across - k));
                        }
                    }
                    /**
                     * Factor 3
                     */
                    for (int l = down - 1; l >= 0; l--) {
                        if (cell[across][l] != 0) {
                            f3 = cell[across][l] - (gapValue * (down - l));
                        }

                    }
                    double cellValue = Math.max(Math.max(Math.max(f1, f2), f3), 0);
                    cell[across][down] = cellValue;
                    if (cellValue >= max) {
                        max = cellValue;
                        maxacross = across;
                        maxdown = down;
                    }

                }

            }
            System.out.println("Max equals:" + max);
            System.out.println("Max down:" + maxacross);
            System.out.println("Max across:" + maxdown);

        }

        /**
         * Optimal Solution Calculation
         */
        public void optimal() {

            int x = maxacross;
            int y = maxdown;
            // starting from the max
            store1.append(input1.charAt(x));
            store2.append(input2.charAt(y));

            while (x > 0 && y > 0) {

                double left = cell[x - 1][y];
                double diagonal = cell[x - 1][y - 1];
                double up = cell[x][y - 1];
                if (left <= diagonal && up <= diagonal) {
                    x = x - 1;
                    y = y - 1;
                    store1.append(input1.charAt(x));
                    store2.append(input2.charAt(y));
                } else if (left >= diagonal && left >= up) {
                    x = x - 1;
                    y = y;
                    store1.append(input1.charAt(x));
                    store2.append('-');
                } else if (up >= diagonal && left <= up) {
                    x = x;
                    y = y - 1;
                    store1.append('-');
                    store2.append(input2.charAt(y));
                }

            }
            store1.reverse();
            store2.reverse();
            System.out.println("store1:" + store1);
            System.out.println("store2:" + store2);

        }

        /**
         * Main Method
         */

        public static void main(String[] args) {

            smithWaterman2 sw = new smithWaterman2();
            sw.generate();
            sw.printMatrix();
            sw.optimal();


        }
    }

1 个答案:

答案 0 :(得分:6)

input2为空,但您在generate方法中的第一次调用是:

cell = new double[input1.length()][input2.length()];

当您尝试对空指针执行任何操作时,您正在调用input2.length();,结果为NullPointerExceptipon

您的代码很容易出现NullPointerException。确保在调用任何操作时,引用不是null