Jump to content

Libgdx and android app crashing during basic assignment operation

Please excuse my weird variable names

 

Can anyone figure out what the problem is here? 

 

This program keeps crashing when I run it on my current test device (google pixel 3 xl). I have tried debugging it and I am certain that the part of the code causing the crashes is the assignment operation from the taco variables into the cVec Vector3 class, this much I know. I have moved and tested it a few ways by moving this portion of code around and removing it completely. At this point I am sure it is atleast part of the problem because when I remove it the program runs fine. Does anyone have any ideas?

 

package craigapps.com;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector3;

public class MyGdxGame extends ApplicationAdapter {
    SpriteBatch batch;
    Texture img;
    private OrthographicCamera cam;
    private ShapeRenderer sr;
    private Vector3 pos;
    public int tacox = 0;
    int tacoy = 0;
    public Vector3 cVec;

    @Override
    public void create() {
        batch = new SpriteBatch();
        img = new Texture("badlogic.jpg");
        sr = new ShapeRenderer();
        cam = new OrthographicCamera();
        cam.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        pos = new Vector3(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2, 0);


    }
    public void addcamvalues(){
        cVec.x = (int) tacox;
        cVec.y = (int) tacoy;

    }

    @Override
    public void render() {
        addcamvalues();
        tacox++;
        tacoy++;


        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        batch.draw(img, tacox, tacoy);
        batch.end();
        //render
        cam.update();
        if (Gdx.input.isTouched()) {
            pos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
            cam.unproject(pos);
        }
        //draw
        sr.begin(ShapeRenderer.ShapeType.Filled);
        sr.setColor(Color.GREEN);
        sr.circle(pos.x, pos.y, 64);
        sr.end();



    }

    @Override
    public void dispose() {
        batch.dispose();
        img.dispose();
        sr.dispose();
    }
}

 

Link to post
Share on other sites

How about including the stack trace in your post? When there's a crash, surely there's a stack trace that is telling you why/where it is crashing (look at Android Studio's LogCat).

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

public class MyGdxGame extends ApplicationAdapter {

  public Vector3 cVec;

  <...>

  public void addcamvalues(){
    cVec.x = (int) tacox;
    cVec.y = (int) tacoy;
  }
}

Are you initializing cVec anywhere? Otherwise I would expect a NullPointerException... Why are your fields public ? Also, why are you casting int fields to int? That is completely redundant.

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

8 hours ago, Eigenvektor said:

How about including the stack trace in your post? When there's a crash, surely there's a stack trace that is telling you why/where it is crashing (look at Android Studio's LogCat).

Let me get that

Link to post
Share on other sites

8 hours ago, Eigenvektor said:

public class MyGdxGame extends ApplicationAdapter {

  public Vector3 cVec;

  <...>

  public void addcamvalues(){
    cVec.x = (int) tacox;
    cVec.y = (int) tacoy;
  }
}

Are you initializing cVec anywhere? Otherwise I would expect a NullPointerException... Why are your fields public ? Also, why are you casting int fields to int? That is completely redundant.

I though declaring it initialized it... I only have errors when I try to reassign values to it. I am not sure how to search source code in Java yet so perhaps I should look into that but with out looking I would assume it's a safe be that the is a default constructor already initializing it wouldn't you say? But also, x and y are properties of the Vector3 class so they are already declared null when they are defined in the class so how could reassigning value to them be a problem?

 

Also, yes. Recasting to an int is redundant but I was trying every possible solution as I wasn't sure the exact data types involved in the Vector3 class or how Java handles such assignments I though it was worth a try

 

Anyway, let me try to find something in the log cat I guess

Link to post
Share on other sites

8 hours ago, Eigenvektor said:

How about including the stack trace in your post? When there's a crash, surely there's a stack trace that is telling you why/where it is crashing (look at Android Studio's LogCat).

So logcat was useless, but wow I am sorry I wasn't declaring it. Lol my bad, you were right. No problem now

Link to post
Share on other sites

3 hours ago, stack0s said:

I though declaring it initialized it... I only have errors when I try to reassign values to it. I am not sure how to search source code in Java yet so perhaps I should look into that but with out looking I would assume it's a safe be that the is a default constructor already initializing it wouldn't you say?

Yes and no. Declaring a field in a class initializes it to the default value for that type. For primitive types like int, long, etc. the default value is 0. For a boolean the default would be false. For anything else, the default value is null. This means you should see something like "java.lang.NullPointerException" in logcat when you try to access it before assigning a value to it.

 

You need to restrict LogCat to the output of your app, otherwise you probably get way too much output. Of course if your app crashes directly during startup its possible you get a semi-useless error that tells you your Activity can't be started. Otherwise you should see a stack trace that tells you what went wrong and which line of code.

 

I guess you figured it out by now, but you need to assign a value to your field before you attempt to access its fields, properties and methods in turn:

public Vector3 cVec = new Vector3();

This will create a new instance of the Vector3 class and will initialize its fields. This means x and y should be 0, assuming they're ints.

 

3 hours ago, stack0s said:

Also, yes. Recasting to an int is redundant but I was trying every possible solution as I wasn't sure the exact data types involved in the Vector3 class or how Java handles such assignments I though it was worth a try.

Casting is only needed, if your field is of a different type than the target and cannot be converted without loss. For example if you assign an int to a long, no cast is needed, because a long can accept all possible values of an int. When you assign a long to an int on the other hand, you would need to cast. You're essentially telling the compiler "don't worry, I know what I'm doing".

 

You can only cast if the types in question are compatible, e.g. you can't cast from int to a String or vice versa. In this case you would need to do a conversion:

private String s = String.valueOf(5); // "5"

// This will throw an exception if your String can't be parsed to an int
private int x = Integer.parseInt("5") // 5

 

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×