Evilunclebill
Evilunclebill

Reputation: 789

I want to see if event is initialized or not but get an error

I have 2 classes, 1st being Sprite and 2nd being GameView. I have a onTouchEvent in the GameView class and I want to change the sprites direction and animation upon a touchevent. However, any way I have tried to address the event in the sprite class I get a Variable might not have been initialized so therefor I want to check if the event is initialized - this gives me the same error though!

Sprite class:

    MotionEvent event;  // These three declarations is just to get them in
    int xcoords;        // the scope - not sure it's the right way.
    int ycoords;

    if (gameView.onTouchEvent(event, xcoords, ycoords) != null) {  // This
    // gives me the error, specifically on event, xcoords and ycoords.

        MotionEvent event = this.gameView.onTouchEvent(event.getPointerCount());
        int isPushedDown = event.getPointerCount();

        if (isPushedDown == 0) {

            srcX = 1;
            srcY = 3;

            Rect srcNoMove = new Rect(srcX, srcY, srcX, srcY);

            canvas.drawBitmap(bmp, srcNoMove, dst, null);

        }

    }

GameView event:

public boolean onTouchEvent(MotionEvent event, float xcoord, float ycoord) {
        if (System.currentTimeMillis() - lastClick > 500) {
            lastClick = System.currentTimeMillis();
    }

    xcoord = event.getX();
    ycoord = event.getY();

    return onTouchEvent(event, xcoord, ycoord);

}

How do I address this problem? Declaring the variables above with null, and 2 times = 0; gives the problem that a Boolean cant be null and doing if (!gameView.onTouchEvent(event)) { would that not just return the null I stated above?

I want to check if the variable is initiated and if it is, do something.

Upvotes: 0

Views: 118

Answers (4)

Dalija Prasnikar
Dalija Prasnikar

Reputation: 28512

First there is not enough code posted to tell exactly what is happening, and the rest of code is rather bad and I just cannot even imagine what you are trying to do here and what was your actual problem. Maybe your first draft code before you tried to fix it in wrong way would be more telling.

gameView.onTouchEvent(event, xcoords, ycoords) != null 

In above code you are not actually comparing whether your onTouchEvent is assigned in above code, you are calling it, and since its return value is boolean you cannot compare it with null

If gameView is instance of GameView class, then you don't have to check whether your event is initialized (actually, it is not event but method of your GameView class) and you can use it if gameView variable is initialized. So quite possible is that you actually want to test gameView != null

Also your GameView onTouchEvent method is calling itself recursively with return onTouchEvent(event, xcoord, ycoord);


While you do have issues with your code, real problem here seems to be that you want to go forward way to fast, and you are trying to skip learning the basics. Another issue is that when you bump into an issue, you are listening to compiler trying to purely satisfy its errors without considering what you are actually doing. Compilers are stupid, don't let them lead the way. You are the boss here.

So when compiler tells you that you don't have variable event in scope, you have to stop and think what is the actual meaning. Do you really need event variable there or not? If you just forgot declaring it, then go ahead and do it, but if this kind of error (or any other error) has caught you by surprise then you have to stop and rethink your steps.

Now back to some basics.

public class GameView
{
   // onTouchEvent is method of GameView class
   // it can only be called on valid (non-null) instance of GameView class
   // when you have valid instance of GameView class all its methods 
   // can be used, you don't have to check whether they are null 
   //
   // return value of onTouchEvent is declared as boolean and that 
   // is the only value it can possibly return
   // passed parameters are event, xcoord and ycoord and they can be used inside 
   // onTouchEvent the way you see fit. 
   // since event is object (depending on class design) you can change its state and 
   // those changes will be preserved after onTouchEvent finishes. 
   // xcoord and ycoord are primitive types passed as value and whatever 
   // you do to them those changes will not survive after onTouchEvent.

   public boolean onTouchEvent(MotionEvent event, float xcoord, float ycoord) 
   {         
      xcoord = event.getX(); // this value will be lost the moment onTouchEvent returns
      return true; // or return false 
   }

}

First question in your classes design is what exactly should happen when user touches the screen. Does your Sprite has to react on touch events that only happen when actual Sprite location is touched, or it has to react no matter where touch occurred. In first case the best course of action would be to implement touch detection directly inside Sprite class. In second case, you have to implement touch detection inside GameView class and then direct your sprite instance to do something.

I am assuming that both Sprite and GameView are extending View class so we can tap in into default touch event dispatching overriding onTouchEvent method.

First way - process touch inside Sprite. In this case GameView does not need to know anything about Sprite and does not need Sprite instance

public class Sprite extends View
{
        public boolean onTouchEvent(MotionEvent event)
        {
            // do your movement logic
            return true; // return true if we are handling the event, false otherwise
        }
} 

Second way - do your touch processing in GameView. In this case GameView has to hold valid Sprite instance

public class Sprite extends View
{
    public void move(float x, float y)
    {
    // do your movement logic here 
    }
} 

public class GameView extends View
{
    public Sprite sprite; // you have to initialize that sprite instance before you can use it 
    // - most likely in Activity onCreate method, but that will depend on your game logic

    public boolean onTouchEvent(MotionEvent event)
    {
        sprite.move(event.GetX(), event.GetY());
        return true; // return true if we are handling the event, false otherwise
    }
}

Upvotes: 1

Sergey Shustikov
Sergey Shustikov

Reputation: 15821

First of all.

MotionEvent event = this.gameView.onTouchEvent(event.getPointerCount());

Bad practice.


Secondary

if (gameView.onTouchEvent(event, xcoords, ycoords) != null) {

Bad practice. onTouchEvent must return boolean result. Not a object.


However.

You need initialize a variable before using. "VERY SMART" Java compiler get error to you because most probably you have NullPointerException in runtime.

MotionEvent event;

if(event.getX() != 0f) // compile error.

FIX :

MotionEvent event = null; // In good case - you need use obtain(...) method of MotionEvent class.

if(event!=null && event.getX() != 0f) // All ok.

Upvotes: 1

Mark Aldrich
Mark Aldrich

Reputation: 26

If you want to get rid of the Variable might not be initialized you can declare an object with an initial value of null and then later test for a null (like if(object != null)).

It is a different story with primitives, as they cannot be null. However, there are classes built in to Java that use these primitives as bases.

You can change int xcoords to be Integer xcoords = null. It will then have a value of null, and you can appropriately test for nulls using the syntax I described above.

Java has classes like this for every primitive. int is Integer, boolean is Boolean, etc. Most of them should be easy to figure out.

Upvotes: 1

Sam Estep
Sam Estep

Reputation: 13324

The problem here is that there's a difference between a variable being uninitialized and a variable being initialized but having a value of null. If a variable is uninitialized (like event, xcoords, and ycoords in your question), you can't use it for anything, including checking it for a null value. You must ensure that the variable is initialized to some value before you use it in any way.

Upvotes: 1

Related Questions