user3286337
user3286337

Reputation: 1

NullPointerException in if statement

I'm trying to compare the slot inside the table with the variable f to see if there is a repeat or if the slot is empty.

It complies but ends up giving me a NullPointerException.

Here is my code, issues are marked with //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();
            }
        });
    }
}

Upvotes: 0

Views: 120

Answers (3)

Carlos Bribiescas
Carlos Bribiescas

Reputation: 4397

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

So you do Object[][] data = new Object[row][col]; that creates an array of null objects. (It doesn't instantiate the objects) Then you try to use it here data[row2][col2].equals(null) but you never checked if data[row2][col2] is not null. Since it is null, when you try to .equals on it, you get a null pointer exception. you have to instantiate 'data[row2][col2]' before you try to use functions it has.

Upvotes: 0

SpringLearner
SpringLearner

Reputation: 13844

replace if(data[row2][col2].equals(null))/ with if(data[row2][col2]==null)/

Upvotes: 0

Dolda2000
Dolda2000

Reputation: 25855

You can't call x.equals(y) if x may be null, since dereferencing x would throw a NullPointerException in that case. x.equals(null) is no more valid.

What you want is probably something more like this:

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

This can be preferably be abstracted away as a helper function:

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

Then, you can simply call if(equals(data[row2][col2], f)), and reuse it in other, similar, places.

Upvotes: 1

Related Questions