25

I was try to write a code by singleton pattern.

this is my MainActivity.

public class MainActivity extends AppCompatActivity {

public static MainActivity activity;
public TouchSensor touchSensor;
public SurfaceViewThread surfaceViewThread;
public TriggerChecker triggerChecker;

public Physics physics;

//public static Handler mHandler = new Handler();

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    activity = this;
    surfaceViewThread = SurfaceViewThread.getInstance();
    touchSensor = TouchSensor.getInstance();
    triggerChecker = TriggerChecker.getInstance();
    physics = Physics.getInstance();

    touchSensor.start();
    triggerChecker.start();
    physics.start();
    //surfaceViewThread is already started.

    activity.setContentView(surfaceViewThread);
}

and this is a code what I try to write by singleton pattern.

public class SurfaceViewThread extends SurfaceView implements Runnable, SurfaceHolder.Callback {

private static SurfaceViewThread surfaceViewThread = new SurfaceViewThread();

private static Camera camera;
private SurfaceHolder holder;
private Thread thread;
private boolean run;

private Vector<ObjectGraphic> objects;
private Vector<Graphic> UIs;
private ObjectGraphic field;

private Vector<ObjectGraphic> draws;
private ComponentMessage componentMessage;

public static SurfaceViewThread getInstance()
{
    if(surfaceViewThread == null)
        surfaceViewThread = new SurfaceViewThread();
    return surfaceViewThread;
}
private SurfaceViewThread()
{
    super(activity.getApplicationContext());
    camera = new Camera();
    getHolder().addCallback(this);
    holder = getHolder();
    objects = new Vector<>();
    UIs = new Vector<>();

    draws = new Vector<>();

    run = false;

    componentMessage = new ComponentMessage();
}

and when I build this code, Camera class crashed by NullPointException.

public class Camera extends GameObject {

private Rect area;
private int sight;
private int maxSight = 50;
private int minSight = 10;
private int[] fieldOfView;//{vertical, horizontal}
private int[] maxFieldOfView = new int[] { 60, 45 };
private int[] minFieldOfView = new int[] { 10, 10 };

public Camera()
{
    super();
    area = new Rect();
    this.fieldOfView = new int[] { 60, 45 };
    objectMatrix.Rotate(objectMatrix.xVector(), Math.toRadians(-45));

    setDisplayVector();
}
private void setSight()
{
    double angle = Math.PI/2 - Math.acos(new Common_Math.Matrix1X4(0, 1, 0, 0).Inner(objectMatrix.zVector().Invert()));
    if(angle < 0 && angle > - Math.PI/2) {
        double t = -objectMatrix.qVector().getY() / objectMatrix.zVector().getY();
        int x = (int) (objectMatrix.zVector().getX() * t + objectMatrix.qVector().getX());
        int z = (int) (objectMatrix.zVector().getZ() * t + objectMatrix.qVector().getZ());

        sight = (int)Math.sqrt(Math.pow((x - objectMatrix.qVector().getX()), 2) + Math.pow((objectMatrix.qVector().getY()), 2) + Math.pow((z - objectMatrix.qVector().getZ()), 2));
        if(sight < minSight)
            sight = minSight;
    }
    else
    {
        sight = maxSight;
    }
}
private void setDisplayVector()
{
    setSight();
    Common_Math.Matrix1X4 upVector, downVector, leftVector, rightVector;
    Common_Math.Matrix1X4 zVector = objectMatrix.zVector();

    upVector = zVector.Rotate(objectMatrix.xVector(), fieldOfView[0]);
    downVector = zVector.Rotate(objectMatrix.xVector(), -fieldOfView[0]);
    leftVector = zVector.Rotate(objectMatrix.yVector(), fieldOfView[1]);
    rightVector = zVector.Rotate(objectMatrix.yVector(), -fieldOfView[1]);

    double angle = Math.PI/2 - Math.acos(new Common_Math.Matrix1X4(0, 1, 0, 0).Inner(objectMatrix.zVector().Invert()));
    upVector = upVector.Scaled((int)(Math.abs(objectMatrix.qVector().getY() * Math.tan(angle + fieldOfView[0]))));
    downVector = downVector.Scaled((int)(Math.abs(objectMatrix.qVector().getY() * Math.tan(angle - fieldOfView[0]))));
    leftVector = leftVector.Scaled((int)(sight * Math.asin(fieldOfView[1])));
    rightVector = rightVector.Scaled((int)(sight * Math.asin(fieldOfView[1])));

    area.left = (int) objectMatrix.qVector().Add(leftVector).getX();
    area.top = (int) objectMatrix.qVector().Add(upVector).getY();
    area.right = (int) objectMatrix.qVector().Add(rightVector).getX();
    area.bottom = (int) objectMatrix.qVector().Add(downVector).getY();
}

and this is GameObject class

public class GameObject {

protected GameObject upperGameObject;//if upperGameObject is null, this GameObject is parent GameObject.

protected Common_Math.Matrix4X4 objectMatrix;
private Vector<SeaWeedComponent> componentVector;

public GameObject()
{
    objectMatrix = new Common_Math.Matrix4X4();
    AllocatePhysicsSpace();
    componentVector = new Vector<>();
}

and Matrix4x4 class

public static class Matrix4X4
{
    private Matrix1X4 xVector, yVector, zVector, qVector;

    public Matrix4X4() {
        xVector = new Matrix1X4(1, 0, 0, 0);
        yVector = new Matrix1X4(0, 1, 0, 0);
        zVector = new Matrix1X4(0, 0, 1, 0);
        qVector = new Matrix1X4(0, 0, 0, 1);
    }

so I was check a break point to see why this happened but when I debug this, every variables are stuck at "Collecting data.." and nothing happen. and even debuger respond is slow down.

I can't guess why this happen. is there any problem in my code? how can I see the variables?

신승빈
  • 347
  • 3
  • 10

5 Answers5

38

I came across this issue. To solve, for a single object, what helped me was to right-click the stuck object, then go to view as and select object instead of Kotlin class.

enter image description here

enter image description here

as a definitive measure, this answer could be a possible solution, but it does not work for me on

Android Studio Electric Eel | 2022.1.1
Build #AI-221.6008.13.2211.9477386, built on January 11, 2023
Runtime version: 11.0.15+0-b2043.56-8887301 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 12.6.2
Victor Oliveira
  • 3,293
  • 7
  • 47
  • 77
  • 2
    Do you think it has something to do with the latest Android Studio update? I have recently updated to Electric Eel, and the breakpoint did not work. Before, with the same breakpoint, it was still working. – Noel Victorino Jan 27 '23 at 06:48
  • Update (which might help others). Try to "Invalidate cache." My shortcut (using Sublime keymap) to do that was Double Shift > "Invalidate cache." – Noel Victorino Jan 27 '23 at 10:16
  • 3
    I think it's related to an update; this is a five years old question and a few people noticed my answer too fast. What's odd is that I had the issue on both canary and stable AS.. Invalidate cache did not solve to me ; not sure when it started; a few days ago I also updated my MacOs ; maybe it's a Jetbrains/MacOS thing, probably to be fixed soon – Victor Oliveira Jan 28 '23 at 09:23
  • Thanks! I initially thought invalidating the cache would work. But it did not. So, another possible solution (which worked for me) was to try selecting Thread when Suspending from the breakpoint. – Noel Victorino Jan 30 '23 at 05:31
  • 2
    Definitely looks like an Electric Eel bug. I just came here to troubleshoot the same thing. – MattMatt Feb 11 '23 at 17:19
  • When I right-click on the object, it's just showing loading indicator. – Matej Hlatky Jun 13 '23 at 15:17
  • I don't know how, but for me helped switch from Kotiln class to Object (sources wrote on Kotlin). Thank you! – kirkadev Jul 05 '23 at 18:09
7

Not sure if this should be a comment or an answer...

I've seen the same problem in a completely different context and it "somehow" has to do with using threads. It seems when the debugger tries to get a value and for that needs to run something that is associated with another thread, it deadlocks.

In my case I have a javascript engine which is invoked on a non-ui thread when I need to execute JS. If I have variables anywhere that are e.g. subclasses of JSObject, the debugger locks up.

Only way I found working around this is to do logging and setting my breakpoints wisely.

Check if your code runs into any locks or synchronized statements when accessing variables, that might be the cause in your case.

Till this is fixed, you can see your object while debugging as json string using "Gson().toJson(yourObject)".

M. Usman Khan
  • 3,689
  • 1
  • 59
  • 69
mir
  • 371
  • 2
  • 5
2

Having the same problem in Android Studio Flamingo:

enter image description here

Right-click the stuck object, then go to View as and select Object:

enter image description here

then you will be able to see the content of your object:

enter image description here

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
1

I was having the same problem after creating a new virtual device to replicate my phone. It had been working for a while then every time I tried to debug it stopped working at the first breakpoint it came to and was "collecting data..."

I just switched back to the original virtual device (5.1 WVGA API 25) and the debugger started doing as it was told again.

I switched to my newer virtual device and the problem had gone away.

Might have been something to do with an infinite loop I had created which threw the activity snapshot out but not too sure. Will update if it happens again.

Rob
  • 189
  • 1
  • 13
-1

What does the GameObject.class do? Your Camera class is extending GameObject.class and calls a super() in the constructor. Skip the debugger and use classic logging instead to print out before vars are set.

  • [Update]

Singleton doesn't do anything special except keeping itself as single instance, have no clue how the Android Studio Debugger handles them but it shouldn't be problematic. There's a NULL when you try to instantiate the Camera.class.

Try catching the NullPointer when instantiating the Camera()

try {
    Camera cam = new Camera();
} catch (NullPointerException e) {
    e.printStackTrace();
}

The stacktrace should tell you where the NullPointer is in the code

gekn76
  • 46
  • 7
  • 1
    Ok, I was Update GameObject Class and I will try your advise. but, I have a question if debugger cause an error when write an code with singleton pattern? – 신승빈 Aug 14 '17 at 01:41
  • I think this has something to do with singletons. – IAbstract May 29 '18 at 21:29
  • After further debugging, I think it has something to do with an initialization error. In my case, a class was being initialized that required something from my Singleton before the Singleton itself was fully initialized. In .Net this throws an initialization exception. I don't know that Java does. – IAbstract Jun 01 '18 at 20:08