Null pointer exception with custom view in xml layout

Here's the custom view code that gives me a null pointer exception on the XML layout tab that 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 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 is 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 the Tetris source code and I want to learn this for a future reference, but I can’t view the game’s layout

+6
source share
2 answers

You are not currently performing a NULL check on onDraw() .

Having NULL checks will help you make more stable code and help you find your problem faster.

I would add some NULL checks as shown 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 */ } } 
+5
source

I do not see the actual call of the initialization methods.

For instance:

 initialize(i, j, left, top, side); 

So, I think you are doing this outside and skipping something so that either the wall, block, or mbg is still empty.

Perhaps add code for how you use and initialize your view if this hint is not enough.

(Or, but this, I hope, is not so, I'm wrong!)

0
source

Source: https://habr.com/ru/post/922592/


All Articles