what does int cannot be dereferenced mean here

4k Views Asked by At
//Listener for the preorder button
jbtPreOrder.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e){
        key = Integer.parseInt(jtfKey.getText());
        if (!tree.isEmpty()){
            JOptionPane.showMessageDialog(null, "Enter something in the tree");
        }
        else {
            JOptionPane.showMessageDialog(null, key + " ");
            preorder(key.left);
            preorder(key.right);
        }
    }
}); 

In the preorder it says int cannot be dereferenced even when the variable is global.

2

There are 2 best solutions below

1
On

Key is a an int and int is a primitive. You cannot call methods on a primitive, only an object. Key should really be a string because you are calling get text and hence retrieving characters and not numbers....

0
On
        preorder(key.left);
        preorder(key.right);

Exactly the error it self explaining that here key is a primitive datatype (int). you are using primitive datatype (here int) key as a reference variable.

So,you cannot dereference using key since it is not holding any object.