androidnullpointerexceptionandroid-paint

Getting null pointer exception virtual method 'long android.graphics.Paint.getNativeInstance()' on a null object reference


I am trying to create a todo list app. I am adding entered text to the listitems on the enter, it's working fine without the style. After adding style with paint and drawing its giving null pointer exception. I am initializing all the drawing and paint objects in the init method of "ToDoListItemView". I tried with "log.i" but it's not entering into ToDoListItemView's init method.

Here is my code.

MainActivity:

package todo.prac.com.todolist;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView mylistView=(ListView)findViewById(R.id.myListView);
        final EditText editText=(EditText)findViewById(R.id.myEditText);
        final ArrayList<String>todoItems=new ArrayList<String>();
        final ArrayAdapter<String>adapter;
        adapter=new ArrayAdapter<String>(this,R.layout.todolist_view,todoItems);
        mylistView.setAdapter(adapter);
        editText.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                if(keyEvent.getAction()==KeyEvent.ACTION_DOWN){
                    if(i==KeyEvent.KEYCODE_ENTER){
                        todoItems.add(0,editText.getText().toString());
                        adapter.notifyDataSetChanged();
                        editText.setText("");
                        return true;
                    }
                }
                return false;
            }
        });
    }
}

and my ToDoListItemView

package todo.prac.com.todolist;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;


    public class ToDoListItemView extends TextView {
        private Paint marginPaint;
        private Paint linePaint;
        private  int paperColor;
        private float margin;
        public ToDoListItemView(Context context) {
            super(context);
        }

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

        public ToDoListItemView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }

        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        public ToDoListItemView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }
        private void init(){
            Log.i("init","inside");
            Resources myResources=getResources();
            marginPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
            linePaint=new Paint(Paint.ANTI_ALIAS_FLAG);
            marginPaint.setColor(myResources.getColor(R.color.notepad_margin));

            linePaint.setColor(myResources.getColor(R.color.notepad_line));
            paperColor=myResources.getColor(R.color.notepad_paper);
            margin=myResources.getDimension(R.dimen.notepad_margin);

        }
        @Override
        public void onDraw(Canvas canvas){
            canvas.drawColor(paperColor);
            if(linePaint==null){
                Log.i("linepaint","is null");
            }
            canvas.drawLine(0,0,getMeasuredHeight(),0,linePaint);
            canvas.drawLine(0,getMeasuredHeight(),getMeasuredWidth(),getMeasuredHeight(),linePaint);
            canvas.drawLine(margin,0,margin,getMeasuredHeight(),marginPaint);
            canvas.save();
            canvas.translate(margin,0);
            super.onDraw(canvas);
            canvas.restore();
        }
    }

and my TodoList layout xml:

<?xml version="1.0" encoding="utf-8"?>
<todo.prac.com.todolist.ToDoListItemView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:padding="10dp"
    android:scrollbars="vertical"
    android:textColor="@color/notepad_text"
    android:fadingEdge="vertical"
    />

Solution

  • You are not calling the init() method inside the constructors of your class.

    public ToDoListItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    
    public ToDoListItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ToDoListItemView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }