Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java unexpected type error

Tags:

java

Below is a piece of code of a game in an applet.I got the error: unexpected type required variable found value. Actually the error is because of my assignment in method repaint but how should it be? Any help would be much appreciated.

public class subclass of JApplet{
    JApplet jp; 
    int yPos=230;
    public void check{

        if(jp.getX()>160 && jp.getY()<200)
            repaint();
    }

    public void repaint(){
        jp.getX()=jp.getWidth()-10;
        jp.getY()=yPos;
    }
}
like image 759
programmer21 Avatar asked Jun 23 '26 15:06

programmer21


2 Answers

The problem reside in these two lines :

jp.getX()=jp.getWidth()-10;
jp.getY()=yPos;

I assume that getX and getY return some x and y variable. However, you can't mutate them that way, you need to create a setter method or acces them directly and modify them.

Something like :

public void setX(int x)
{
    this.x = x;
}

Then you would do

jp.setX(someValue);

or if the field is not private you could directly do :

jp.x = someValue;

The error message "required variable, found value" refer to what is returned by getX. The left side of an assignment must be a variable to hold the value, but in your case it is a value (returned by the getter) hence the error message.

like image 91
Jean-François Savard Avatar answered Jun 26 '26 09:06

Jean-François Savard


You can't assign a value to a method call. Change repaint() to something along the lines of:

jp.setX(jp.getWidth()-10);
jp.setY(yPos);
like image 33
1337joe Avatar answered Jun 26 '26 11:06

1337joe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!