Search code examples
javanetbeansnetbeans-8

how to check jTable nullpointer in row


how to check my jtable if null pointer? my code is always null pointer and then can't export to xls. this my code

if (tbGudangSales.getValueAt(i, 5).toString().trim().length() == 0) {
                        status = "tidak ada penjualan";
                    } else{
                        status = tbGudangSales.getValueAt(i, 5).toString();
                    }

                    label = new Label(4, baris, status, bordertabel);
                    excelSheet.addCell(label);

this my error Error null pointer

this my value table Table


Solution

  • All you need to do is to check if the value is null. For example:

    Object value = tbGudangSales.getValueAt(i, 5);
    
    if (value != null)
    {
        if(value.toString().trim().isEmpty()) // instead checking the length "manually", you can use the isEmpty() method
        {
            status = "tidak ada penjualan";
        } 
        else
        {
            status = value.toString();
        }
    }