Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8388095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:23:36+00:00 2026-06-09T18:23:36+00:00

Here’s the custom view code which gives me a null pointer exception in the

  • 0

Here’s the custom view code which gives me a null pointer exception in the layout tab of the XML which uses this view:

public class BoardView extends View {

    // Drawables for the board boxes, the playable zone
    public Drawable[][] block = new Drawable[20][10];
    // Drawables for the wall (yes, it's done with tiles)
    public Drawable[] wall = new Drawable[102];
    // Drawable for the background, and boolean for drawing it or not
    Drawable mbg;
    boolean bg = false;
    // Context and canvas to be used along the class
    Context context;
    Canvas c;

    /*************************************************/
    /* Class constructor **************************** */
    /*************************************************/
    /* Defines the context and the canvas *********** */
    /*************************************************/
    public BoardView(Context cont, AttributeSet attrs) {
        super(cont, attrs);
        context = cont;
    }

    /*************************************************/
    /* Initializes drawables for playable boxes ***** */
    /*************************************************/
    /* Must be initialized one by one from the Game * */
    /* activity, passing all the parameters ********* */
    /*************************************************/
    public void initialize(int i, int j, int left, int top, int side) {
        block[i][j] = context.getResources().getDrawable(R.drawable.alpha);
        block[i][j].setBounds(left, top, left + side, top + side);
    }

    /*************************************************/
    /* Draws the board wall ************************* */
    /*************************************************/
    /* Needs the top-left point of the board frame ** */
    /* and the width of the wall ******************** */
    /*************************************************/
    public void createWall(int left, int top, int side) {
        int i = 0, x, y;
        x = left - side / 2;
        y = top;
        // The left wall
        while (i < 40) {
            wall[i] = context.getResources().getDrawable(R.drawable.brick);
            wall[i].setBounds(x, y, x + side / 2, y + side / 2);
            y = y + side / 2;
            i = i + 1;
        }
        x = left + side * 10;
        y = top;
        // The right wall
        while (i < 80) {
            wall[i] = context.getResources().getDrawable(R.drawable.brick);
            wall[i].setBounds(x, y, x + side / 2, y + side / 2);
            y = y + side / 2;
            i = i + 1;
        }
        x = left - side / 2;
        // The floor
        while (i < 102) {
            wall[i] = context.getResources().getDrawable(R.drawable.brick);
            wall[i].setBounds(x, y, x + side / 2, y + side / 2);
            x = x + side / 2;
            i = i + 1;
        }
    }

    /*************************************************/
    /* Draws the board background ******************* */
    /*************************************************/
    /* Needs the top-left point of the board frame ** */
    /* and the width of the wall ******************** */
    /*************************************************/
    public void createBg(int left, int top, int side) {
        // Set board background (if any)
        bg = false;
        int bgn = 1 + (int) (Math.random() * 19);
        switch (bgn) {
        case 1:
            mbg = getResources().getDrawable(R.drawable.bg1);
            break;
        case 2:
            mbg = getResources().getDrawable(R.drawable.bg2);
            break;
        case 3:
            mbg = getResources().getDrawable(R.drawable.bg3);
            break;
        case 4:
            mbg = getResources().getDrawable(R.drawable.bg4);
            break;
        case 5:
            mbg = getResources().getDrawable(R.drawable.bg5);
            break;
        case 6:
            mbg = getResources().getDrawable(R.drawable.bg6);
            break;
        case 7:
            mbg = getResources().getDrawable(R.drawable.bg7);
            break;
        case 8:
            mbg = getResources().getDrawable(R.drawable.bg8);
            break;
        case 9:
            mbg = getResources().getDrawable(R.drawable.bg9);
            break;
        case 10:
            mbg = getResources().getDrawable(R.drawable.bg11);
            break;
        case 11:
            mbg = getResources().getDrawable(R.drawable.bg11);
            break;
        case 12:
            mbg = getResources().getDrawable(R.drawable.bg12);
            break;
        case 13:
            mbg = getResources().getDrawable(R.drawable.bg13);
            break;
        case 14:
            mbg = getResources().getDrawable(R.drawable.bg14);
            break;
        case 15:
            mbg = getResources().getDrawable(R.drawable.bg15);
            break;
        case 16:
            mbg = getResources().getDrawable(R.drawable.bg16);
            break;
        case 17:
            mbg = getResources().getDrawable(R.drawable.bg17);
            break;
        case 18:
            mbg = getResources().getDrawable(R.drawable.bg18);
            break;
        case 19:
            mbg = getResources().getDrawable(R.drawable.bg19);
            break;
        }
        mbg.setBounds((int) (left), (int) (top), (int) (left + side * 10),
                (int) (top + 20 * side));
    }

    /*************************************************/
    /* Draws the board ****************************** */
    /*************************************************/
    /* Draws the walls, the bg and all the boxes **** */
    /*************************************************/
    @Override
    protected void onDraw(Canvas canvas) {
        c = canvas;
        super.onDraw(canvas);
        if (bg)
            mbg.draw(canvas);
        for (int i = 0; i < 102; i++)
            wall[i].draw(c);
        for (int i = 0; i < 20; i++)
            for (int j = 0; j < 10; j++) {
                block[i][j].draw(canvas);
            }
        // Actually draw
        invalidate();
    }

    /*************************************************/
    /* Canvas getter ******************************** */
    /*************************************************/
    public Canvas getCanvas() {
        return c;
    }

    /*************************************************/
    /* Colors a box ********************************* */
    /*************************************************/
    /* Changes the drawable for the indicated box to */
    /* to 'c'. Can also be COLOR_NONE to undraw ***** */
    /*************************************************/
    public void setColor(int i, int j, byte c) {
        Rect rect;
        rect = block[i][j].getBounds();
        switch (c) {
        case Values.COLOR_NONE:
            block[i][j] = context.getResources().getDrawable(R.drawable.alpha);
            ;
            break;
        case Values.COLOR_RED:
            block[i][j] = context.getResources().getDrawable(
                    R.drawable.block_red);
            break;
        case Values.COLOR_GREEN:
            block[i][j] = context.getResources().getDrawable(
                    R.drawable.block_green);
            break;
        case Values.COLOR_BLUE:
            block[i][j] = context.getResources().getDrawable(
                    R.drawable.block_blue);
            break;
        case Values.COLOR_YELLOW:
            block[i][j] = context.getResources().getDrawable(
                    R.drawable.block_yellow);
            break;
        case Values.COLOR_PINK:
            block[i][j] = context.getResources().getDrawable(
                    R.drawable.block_pink);
            break;
        case Values.COLOR_PURPLE:
            block[i][j] = context.getResources().getDrawable(
                    R.drawable.block_purple);
            break;
        case Values.COLOR_WHITE:
            block[i][j] = context.getResources().getDrawable(
                    R.drawable.block_white);
            break;
        }
        block[i][j].setBounds(rect);
    }
}

and here’s the error log:

java.lang.NullPointerException
    at com.seavenois.tetris.BoardView.onDraw(BoardView.java:166)
    at android.view.View.draw(View.java:6880)
    at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    at android.view.View.draw(View.java:6883)
    at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    at android.view.View.draw(View.java:6883)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.render(RenderSessionImpl.java:466)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:320)
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:325)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.RenderService.createRenderSession(RenderService.java:372)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(GraphicalEditorPart.java:1640)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.recomputeLayout(GraphicalEditorPart.java:1391)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.activated(GraphicalEditorPart.java:1165)
    at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditorDelegate.partActivated(LayoutEditorDelegate.java:734)
    at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditorDelegate.partBroughtToTop(LayoutEditorDelegate.java:744)
    at org.eclipse.ui.internal.PartListenerList$2.run(PartListenerList.java:87)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
    at org.eclipse.core.runtime.Platform.run(Platform.java:888)
    at org.eclipse.ui.internal.PartListenerList.fireEvent(PartListenerList.java:57)
    at org.eclipse.ui.internal.PartListenerList.firePartBroughtToTop(PartListenerList.java:85)
    at org.eclipse.ui.internal.PartService.firePartBroughtToTop(PartService.java:208)
    at org.eclipse.ui.internal.WorkbenchPagePartList.firePartBroughtToTop(WorkbenchPagePartList.java:76)
    at org.eclipse.ui.internal.WorkbenchPagePartList.fireActiveEditorChanged(WorkbenchPagePartList.java:52)
    at org.eclipse.ui.internal.PartList.setActiveEditor(PartList.java:162)
    at org.eclipse.ui.internal.WorkbenchPage.makeActiveEditor(WorkbenchPage.java:1355)
    at org.eclipse.ui.internal.WorkbenchPage.setActivePart(WorkbenchPage.java:3629)
    at org.eclipse.ui.internal.WorkbenchPage.requestActivation(WorkbenchPage.java:3159)
    at org.eclipse.ui.internal.PartPane.requestActivation(PartPane.java:279)
    at org.eclipse.ui.internal.EditorPane.requestActivation(EditorPane.java:98)
    at org.eclipse.ui.internal.PartPane.setFocus(PartPane.java:325)
    at org.eclipse.ui.internal.EditorPane.setFocus(EditorPane.java:127)
    at org.eclipse.ui.internal.PartStack.presentationSelectionChanged(PartStack.java:837)
    at org.eclipse.ui.internal.PartStack.access$1(PartStack.java:823)
    at org.eclipse.ui.internal.PartStack$1.selectPart(PartStack.java:137)
    at org.eclipse.ui.internal.presentations.util.TabbedStackPresentation$1.handleEvent(TabbedStackPresentation.java:133)
    at org.eclipse.ui.internal.presentations.util.AbstractTabFolder.fireEvent(AbstractTabFolder.java:269)
    at org.eclipse.ui.internal.presentations.util.AbstractTabFolder.fireEvent(AbstractTabFolder.java:278)
    at org.eclipse.ui.internal.presentations.defaultpresentation.DefaultTabFolder.access$1(DefaultTabFolder.java:1)
    at org.eclipse.ui.internal.presentations.defaultpresentation.DefaultTabFolder$2.handleEvent(DefaultTabFolder.java:88)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1077)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1062)
    at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:774)
    at org.eclipse.swt.custom.CTabFolder.setSelection(CTabFolder.java:2746)
    at org.eclipse.swt.custom.CTabFolder.onMouse(CTabFolder.java:1433)
    at org.eclipse.swt.custom.CTabFolder$1.handleEvent(CTabFolder.java:257)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4165)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3754)
    at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2701)
    at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2665)
    at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2499)
    at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:679)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:668)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:123)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:344)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1410)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1386)

Here is line 166 of BoardView.java:

wall[i].draw(c);

How can I fix this? I got this code from a tetris source code and I want to study this for future reference but I can’t view the layout of the game

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:23:38+00:00Added an answer on June 9, 2026 at 6:23 pm

    You are currently not doing any NULL checking in your onDraw().

    Having the NULL checks in will help you make more stable code and help you find your problem quicker.

    I would add in some NULL checks like below:

    protected void onDraw(Canvas canvas) {
        if (canvas != null) {
            c = canvas;
            super.onDraw(canvas);
            if (bg) {
                if (mbg != null) {
                    mbg.draw(canvas);
                } else {
                    /*
                     * Handle null value
                     * print to logcat this was null
                     */
                }
            }
            for (int i = 0; i < 102; i++) {
                if (wall[i] != null) {
                    wall[i].draw(c);
                } else {
                    /*
                     * Handle null value
                     * print to logcat this was null
                     */
                }
            }
            for (int i = 0; i < 20; i++) {
                for (int j = 0; j < 10; j++) {
                    if (block[i][j] != null) {
                        block[i][j].draw(canvas);
                    } else {
                        /*
                         * Handle null value
                         * print to logcat this was null
                         */
                    }
                }
            }
            //Actually draw
            invalidate();
        } else {
            /*
             * Handle null value
             * print to logcat this was null
             */
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code in a function I'm trying to revise. This example works
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
Here a simple question : What do you think of code which use try
Here is my code, which takes two version identifiers in the form 1, 5,
Here is my code to send send direct message using scribe. But it gives
Here is the main scroller. The stream view. But how did they implement this?
Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }
Here's my code in the <head></head> : <link rel=stylesheet href=http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css /> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script>
Here is my SQL script CREATE TABLE tracks( track_id int NOT NULL AUTO_INCREMENT, account_id
Here is my persistence.xml : <?xml version=1.0 encoding=UTF-8?> <persistence xmlns=http://java.sun.com/xml/ns/persistence xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd version=1.0>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.