Calling non-static method from within mousehandler results in error:" non static method cannot be referenced from static context

48 Views Asked by At

As the title states im having trouble with a method inside a mousehandler. My mousehandler is:

public class MouseHandler implements MouseListener{

    @Override
    public void mouseClicked(MouseEvent e){


    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON1) {
            try {
                Pieces.getPos(e, coordinateXYZV, tile,chessBoard);
            }catch (Exception a){

            }
        }
    }

now i know questions about static stuff are really not welcome, but i just cant seem to find the solution although similar problems were easily fixed. Neither the class with the mousehandler nor the method is static, so why am i getting this error in the first place? Im creating a new mousehandler like this:

MouseHandler mhandler = new MouseHandler();

and then pass it around to whereever its needed. if anything further is needed please point it out because this is my first time asking a question here and im not entirely sure whats needed to help me here

1

There are 1 best solutions below

0
speedprogrammer9000 On

For future people stumbling upon this: call your methods correclty:

 @Override
    public void mousePressed(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON1) {
            try {
                Pieces p = new Pieces();                       <--
                p.getPos(e, coordinateXYZV, tile,chessBoard);  <--
            }catch (Exception a){

            }
        }
    }