Search code examples
javaandroidandroid-layoutfor-loopandroid-custom-view

How to add multiple custom views programmatically in Android


I have a slight issue here. I've made a custom view class that carries some data and I am trying to add it dynamically in my main activity, now when I try to add a single view it works fine. But when I put it in a for loop and let's say I want to add more than one the code breaks with this error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.digiart.xapp/com.digiart.xapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View)' on a null object reference

custom view:

public class TableView extends View {

    private static final String TAG = "TableView";
    private int numberOfSeats;
    private int tableId;
    private int positionX;
    private int positionY;
    private int objectWidth;
    private int objectHeight;
    private boolean isTaken;
   private  String tableKind;


     private Rect rectangle;
    private Paint paint;


    public TableView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }
    public TableView(Context context,int numberOfSeats,int tableId,int positionX,int positionY,int width,int height
    ,boolean isTaken, String tableKind) {
        super(context);

        this.numberOfSeats = numberOfSeats;
        this.tableId = tableId;
        this.positionX = positionX;
        this.positionY = positionY;
        this.objectWidth = width;
        this.objectHeight = height;
        this.isTaken = isTaken;
        this.tableId = tableId;
        this.tableKind = tableKind;

        //defining shape
        rectangle = new Rect(positionX,positionY,width,height);
        //defining shape color
        paint = new Paint();
        paint.setColor(Color.GRAY);
        Log.i(TAG, "TableView: tableId: "+tableId+" isTaken: "+isTaken);


    }

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

        //canvas.drawColor(Color.BLUE);
        canvas.drawRect(rectangle, paint);

    }

    public int getNumberOfSeats() {
        return numberOfSeats;
    }

    public int getTableId() {
        return tableId;
    }

    public int getPositionX() {
        return positionX;
    }

    public int getPositionY() {
        return positionY;
    }

    public int getObjectWidth() {
        return objectWidth;
    }

    public int getObjectHeight() {
        return objectHeight;
    }

    public boolean isTaken() {
        return isTaken;
    }

    public String getTableKind() {
        return tableKind;
    }
}

code snippet from main activity with some static values for testing:

for (int i = 0;i<15;i++) {
           tv = new TableView(MainActivity.this, numberOfSeats, tableId, positionX, positionY, objectWidth, objectHeight
                    , isTaken, tableKind);
            positionX +=20;
            floorPlan.addView(tv);
        }

Now this is quite confusing for me because I can not seem to grasp what could be null here, I am just trying to make new instance of view every time where the x position is the only thing that changes.


Solution

  • The problem is exactly what the error says:

    Attempt to invoke method addView(android.view.View)' on a null object reference
    

    You are trying to do null.addView, which is causing the error!

    The only time you do "addView" in your code is here:

    floorPlan.addView(tv);
    

    So this means that floorPlan == null.

    Check where you initialise floorPlan and ensure that it is being set correctly :)