0

Faced a problem when working with SurfaceView and drawing elements from an array, which is updated in another thread. The values ​​in the array are always not null. Below I give the code and error messages. With what it can be connected? I will be very glad to help!

SurfaceViewThread:

@Override
        public void run() {
            Canvas canvas;
            while (runFlag) {
                canvas = null;
                try {
                    // получаем объект Canvas и выполняем отрисовку
                    canvas = surfaceHolder.lockCanvas(null);
                    synchronized (bw.multithreadLayers) {
                        if (canvas == null) return;
                        p.setShader(new LinearGradient(0, 0, 0, scrH, bw.getColor(), bw.getColor2(), Shader.TileMode.MIRROR));
                        canvas.drawPaint(p);
                        p.setShader(null);
                        p.setTextSize(textSize);
                        p.setColor(0xFFffffff);

                        for (int i = 0; i<bw.multithreadLayers.size(); i++) {
                            try {
                                for (int j = 0; j<bw.multithreadLayers.get(i).size(); j++) {
                                    GObject go = bw.multithreadLayers.get(i).get(j);
                                    canvas.drawBitmap(go.getTexture(), go.getX(), scrH-go.getY()-go.getTexture().getHeight(), p);
                                }
                            } catch(Exception e) {
                                System.out.println("JException, " + i + ", " + e.getMessage());
                            }
                        }

                        canvas.drawBitmap(player, objX, objY, p);
//some code...
                    }
                } 
                finally {
                    if (canvas != null) {
                        surfaceHolder.unlockCanvasAndPost(canvas);
                    }
                }
            }
        }

MultithreadLayers:

lowLayers.clear();
        lowLayers.add(layer0);
        lowLayers.add(layer1);
        lowLayers.add(layer2Active);
        multithreadLayers = lowLayers;

Exceptions:

06-19 08:05:41.967 I/System.out(30470): JException, 0, Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get(int)' on a null object reference

And

06-19 08:05:43.601 I/System.out(30470): JException, 1, Index: 1, Size: 3
  • Because of these exceptions, flickering appears on the screen... – Daniel Bernar Jun 19 '17 at 05:22
  • Check if `bw.multithreadLayers.get(i)` is null, if not then proceed with `bw.multithreadLayers.get(i).get(j)`...Also check null for `GObject go` – Akshay Bhat 'AB' Jun 19 '17 at 05:22
  • 1
    Look at the stack trace to find the line of your code that causes the error. – Code-Apprentice Jun 19 '17 at 05:34
  • Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Code-Apprentice Jun 19 '17 at 05:35
  • Output stack trace did not help. I got the same error, and the line number that I already knew. But why do I get NullPointerExceprion, if not under what circumstances the object can not be null? Also, here's a funny thing: [link](http://www.codesend.com/view/a720d752408eb600cdaadf8e0772e7af/) – Daniel Bernar Jun 19 '17 at 11:04

1 Answers1

0

I found the problem. I forgot to call clone() on ArrayList :D

multithreadLayers = (ArrayList<ArrayList<GObject>>) lowLayers.clone();