0

I want to build a small game which will be a JTable with some values and JTextField above the table, user should type any value in the text field if it exist in the table it will remove if it not exist user will get message by that, I tried to remove value which user write it in text field and wrote this code:

String value = txt_num.getText();
for (int i = 0; i < table.getRowCount(); i++) {                       
    for (int j = 0; j < table.getColumnCount(); j++) {              
        if (tbl.getModel().getValueAt(i, j).toString().equals(value)) {
            tbl.setValueAt(null, i, j);
        } else {
                JOptionPane.showMessageDialog(null, "Value isn't exist");
         }
    }
}

This code remove correctly but it remove the first value which entered but when I enter another value I got this error:

enter image description here

How can I resolve that NullPointerException?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    For better help sooner, [edit](https://stackoverflow.com/posts/62652804/edit) to add a [mre] or [Short, Self Contained, Correct Example](http://www.sscce.org). Also, problems regarding `NullPointerException` are pretty hard to fix if you don't provide the code snippet where it actually occurs alias the corresponding code snippets. I will also link [this](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) thread as chances are high your thread will simply be flagged and closed as a duplicate of that one. – yur Jun 30 '20 at 08:03
  • If "I got it" means you solved the problem yourself, I'd still invite you to submit your own answer describing how you solved the problem and accepting that, so that others who may find this thread may benefit from it aswell! – yur Jun 30 '20 at 08:20
  • Don't post images of text! In future, post the text itself. – Andrew Thompson Jun 30 '20 at 08:47

2 Answers2

1

First you need to understand, what exactly a NullPointerException is and why it happens. @yur's comment will help you with that.

Regarding your problem, the issue is in the if clause here:

if (tbl.getModel().getValueAt(i, j).toString().equals(value)) {

The first time this works, because getValueAt returns a valid object, where you can perform toString. But after you set the value of the cell in your table to null and try the same again, you get null returned from the getValueAt and attempt to perform the toString method on the null object. This will of course not work and cause the error you showed.

The solution:

Implement some logic to check if the returned value is null before trying to convert it to String. For example:

if ((tbl.getModel().getValueAt(i,j) != null) && (tbl.getModel().getValueAt(i, j).toString().equals(value))) {

Or don't set the value to null if you want to remove it, but you could instead set it to "".

maloomeister
  • 2,461
  • 1
  • 12
  • 21
0

when I converted null here

tbl.setValueAt(null, i, j);

into " " Error was resolved and code works correctly for me