Java unexpected type error

Below is a code snippet of a game in applet.I got an error: Unexpected type required variable value. Actually the error is related to my assignment in the redraw method, 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;
    }
}

      

+3


source to share


2 answers


The problem is with the following two lines:

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

      

I am assuming that getX

both getY

return a variable x

and y

. However, you cannot mutate them this way, you need to create a setter method or get them directly and change 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 can directly:

jp.x = someValue;

      

The error message "required variable, value found" refers to what is returned getX

. The left side of the job should be a variable to hold the value, but in your case it is the value (returned by the receiver) hence the error message.

+3


source


You cannot assign a value to a method call. Change repaint()

to the following:



jp.setX(jp.getWidth()-10);
jp.setY(yPos);

      

+1


source







All Articles