我正在尝试将表内的插槽与变量f进行比较,以查看是否存在重复或插槽为空。

它符合要求,但最终给了我NullPointerException

这是我的代码,问题标有//this one

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class Journal extends JPanel
    {
    public Journal()
    {
        String colN1 = "Date";
        String colN2 = "Student";
        int a = 20;
        int b = 5;
        int c = a*2/b;
        int col = c*2;
        String[] columnNames = new String[col];
        for(int colF = 0; colF < col; colF++)
        {
            if(colF%2 == 0)
            {
                columnNames[colF] = colN1;
            }
            else
            {
                columnNames[colF] = colN2;
            }
        }
        int row = b;
        int d = 1;
        Object[][] data = new Object[row][col];
        for (int row1 = 0; row1 < data.length; row1++)
        {
            for (int col1 = 0; col1<data[0].length; col1++)
            {
                if(col1%2 != 0)
                {
                    data[row1][col1]= "day" + d;
                    d++;
                }
                else
                {
                    Integer f = new Integer(1);
                    int col2 = 0;
                    int row3 = 0;
                    boolean rows;
                    for (int row2 = 0; row2 < data.length; row2++)
                    {
                        if(data[row2][col2].equals(null))//this one
                        {
                            rows = true;
                        }
                        else if(data[row2][col2].equals(f))//this one
                        {
                            rows = false;
                        }
                        else
                        {
                            rows = true;
                        }
                        col2++;
                        if(col2 >= col)
                        {
                            col2 = 0;
                        }

                        for (int col3 = 0; col3<data[0].length; col3++)
                        {
                            if(f > a)
                            {
                                f = 1;
                            }
                            else if(rows == true && data[row3][col3].equals(f))//this oen
                            {
                                data[row3][col3]= f;
                            }
                            f++;
                            row3++;
                            if(row3 >= row)
                            {
                                row3 = 0;
                            }
                        }
                    }
                }
            }
        }
        JTable table = new JTable(data, columnNames)
        {
            public Class getColumnClass(int column)
            {
                for (int row4 = 0; row4 < getRowCount(); row4++)
                {
                    Object o = getValueAt(row4, column);

                    if (o != null)
                        return o.getClass();
                }
                return Object.class;
            }
        };
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane);
    }
    private static void gui()
    {
        JFrame gui = new JFrame();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setTitle("Journal List");
        gui.setSize(700,200);
        gui.setLocationRelativeTo(null);
        gui.setVisible(true);
        gui.add(new Journal());
    }
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                gui();
            }
        });
    }
}

最佳答案

如果x.equals(y)可能是x,则不能调用null,因为在这种情况下取​​消引用x会抛出NullPointerExceptionx.equals(null)不再有效。

您想要的可能是这样的:

if((x == null && f == null) || (x != null && f != null && x.equals(f)))


最好可以将其抽象为辅助函数:

public static boolean equals(Object a, Object b) {
    if(a == null)
        return(b == null);
    else
        return((b != null) && a.equals(b));
}


然后,您可以简单地调用if(equals(data[row2][col2], f)),并在其他类似的地方重复使用它。

10-05 22:53
查看更多