2

I'm trying to create a android custom view and i'm facing a rather strange problem.

Every data members I set in the constructor (String, Bitmap, ...) become null in the overriden onDraw method.

Here's the code:

public class SimulationView extends View
{
private static final String TAG = "SIMULATION_VIEW";
private String str;
private Bitmap mField;

public SimulationView(Context context) {
    super(context);
    str = new String("hello"); 
    mField = BitmapFactory.decodeResource(getResources(), R.drawable.field);
    Log.i(TAG_DEBUG, mField.getHeight() + " " + mField.getWidth()); // displays "1518 900" properly
    Log.i(TAG, str); // displays "hello" properly
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Log.i(TAG, str); // throws java.lang.NullPointerException
    Log.i(TAG, mField.getHeight() + " " + mField.getWidth()); // throws java.lang.NullPointerException (when previous line is commented of course)
    invalidate();
}

public SimulationView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

public SimulationView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context);
}

private void init(Context context) {
}

Here's the stacktrace when the String causes the NPE:

   Process: com.example.simulation_view, PID: 5207
java.lang.NullPointerException: println needs a message
        at android.util.Log.println_native(Native Method)
        at android.util.Log.i(Log.java:160)
        at com.example.simulation_view.SimulationView.onDraw(SimulationView.java:84)
        at android.view.View.draw(View.java:15114)

And here's the stacktrace when the Bitmap causes the NPE:

  java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getHeight()' on a null object reference
        at com.example.simulation_view.SimulationView.onDraw(SimulationView.java:83)
        at android.view.View.draw(View.java:15114)

What am I doing wrong ? Any help would be much appreciated!

Have a good day

1
  • How do you create SimulationView on code by constructor with Context or in xml? Or one time on xml and on time in code? Mar 15, 2015 at 21:06

2 Answers 2

3

your init method is empty. Your code works only if you instantiate programmatically your custom view. If you declare in an xml, the Bitmap and the String are not initialized. Use init to initialized, and call it also in the constructor that takes only the Context as parameter

private void init(Context context) {
    str = "hello"; 
    mField = BitmapFactory.decodeResource(getResources(), R.drawable.field);
}
9
  • Well, it this seems to have solved the problem! Thank you very much @Blackbelt !But i'm curious, why does the instanciation made in the constructor are not applied ? Mar 15, 2015 at 21:21
  • when you declare the custom view in xml the constructor called is the one with two parameters, SimulationView(Context context, AttributeSet attrs), and in this one you were not initializing the objects you were using in the onDraw
    – Blackbelt
    Mar 15, 2015 at 21:25
  • Ok, but in the design of my app, I instanciate my custom view by calling the one argument constructor. (In the MainActivity) : mSimulationView = new SimulationView(this) Mar 15, 2015 at 21:28
  • did you declare you view in one your layout ?
    – Blackbelt
    Mar 15, 2015 at 21:29
  • Yes, i created a xml layout file for this custom view. Mar 15, 2015 at 21:35
0
mField = BitmapFactory.decodeResource(getResources(), R.drawable.field);

This line is returning a null object and thus when you attempt to access the getHeight() method it will throw a null pointer exception. Try verifying that the parameters are what you expect.

In the case of the onDraw() method you should consider the scope of your global variable String str. It is likely that it is not assigned a value by the time it reaches Log.i(TAG, str); in your onDraw() method. Consider using local variables instead and passing it as a parameter to the necessary methods.

3
  • Like i commented in the constructor, the Log function displays the height and the width of the bitmap properly, prooving therefore that the decodeResource does not return a null object. Mar 15, 2015 at 21:15
  • See Blackbelt's answer, I believe he is correct in saying that the problem lies in your init function.
    – James
    Mar 15, 2015 at 21:17
  • Yes, that did solve the problem! Thank you for your help though! I'm really intrigued about why the instanciations made in my constructor are not applied in the rest of the code during runtime... Mar 15, 2015 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.