0

I have the following code that blows the app. Methods like drawLine work fine if they are directly inside "onDraw" but not if placed outside in a separate method as shown below. Does anyone have any idea what's wrong? The version with the method compiles ok but the app crashes when started.

Many thanks Roberto

public class Painter extends View {

private static Canvas canvas;
private static Paint paintG = null;
public static int screenW; // screen width
public static int screenH; // screen height

private String tag = "Painter";

public Painter(Context context) {
    super(context);
    readDrawingData(); 
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    screenW = canvas.getWidth(); // screen width
    screenH = canvas.getHeight(); // screen height
    Rect allScreen = new Rect(0, 0, screenW, screenH);

    // works OK
    paintG = new Paint();
    paintG.setAntiAlias(true);
    paintG.setStrokeWidth(2);
    paintG.setStyle(Paint.Style.FILL);
    paintG.setColor(0xFFf7f9d2);
    canvas.drawRect(allScreen, paintG);

    // paint circle/oval - works OK
    double R = ((screenW > screenH) ? screenH : screenW) / 2 - 100;
    double Z = R + 20;
    double X = screenW /2;
    double Y = screenH /2;
    RectF oval1 = new RectF(Math.round(X-R), Math.round(Y-R), 
                            Math.round(X+R), Math.round(Y+R));
    paintG.setStyle(Paint.Style.STROKE);
    paintG.setColor(0xffff0000);
    canvas.drawOval(oval1, paintG);

    // this code works OK here
    paintG.setColor(0xffff0000);
    if (i > 0) {
        for (int j=0; j<i; j++) {
            canvas.drawLine(x1[j], y1[j], x2[j], y2[j], paintG);
        }
    }

// calling this will crash app
paintDrawing();

}

// ************************************************************
void paintDrawing() {
    // PROBLEM: same code as above - causes app to crash...
    // when outside onDraw
    paintG.setColor(0xffff0000);
    if (i > 0) {
        for (int j=0; j<i; j++) {
        canvas.drawLine(x1[j], y1[j], x2[j], y2[j], paintG);
    }
}
// ************************************************************ 

}

  • 1
    lack of basic java knowledge ( variable scopes) and -1 for posting NPE related question – Selvin Jan 15 '15 at 16:41
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Selvin Jan 15 '15 at 16:43
  • Please note that variables such as **i** etc are declared in the original file. This code is just a fragment! – Roberto Markovic Jan 15 '15 at 16:43
  • what is a field? what is a local variable? do you know the differences? ... also phrase "app crash" is not used by real programmers (becuase it is too broad) – Selvin Jan 15 '15 at 16:45
  • 1
    `i` is not the problem, `canvas` is – tyczj Jan 15 '15 at 16:46
  • comment of Selvin is not correct. If I comment out drawLine and paintG the app works. drawLine and paintG are declared as static and global! – Roberto Markovic Jan 15 '15 at 16:47
  • remove your global `canvas` and you will see the problem – tyczj Jan 15 '15 at 16:50
  • 1
    seems like it is even worst case ... it is not duplicate of "What is a Null Pointer Exception, and how do I fix it?" but rather : [Unfortunately MyApp has stopped. How can I solve this?](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Selvin Jan 15 '15 at 16:51
  • paintG and canvas are not null. in the method I added **if(canvas == null) Log.i(tag, 'canvas is null');** same for paintG and both do not produce a log! – Roberto Markovic Jan 15 '15 at 16:53
  • oh? then where do you set canvas? – tyczj Jan 15 '15 at 16:54
  • then what exception are you getting and where(which line - not number please just cite whole line)? – Selvin Jan 15 '15 at 16:54
  • 1
    *paintG and canvas are not null* yeah, sure .... http://ideone.com/qemrE3 – Selvin Jan 15 '15 at 17:00
  • sorry - I really missed to assign the global values. Many thanks. – Roberto Markovic Jan 15 '15 at 17:06
  • If you debugged your app like @Selvin suggested you wouldn't have – tyczj Jan 15 '15 at 17:12

0 Answers0