我在做游戏。基本上我需要通过将输入数组xs []中的元素添加到ArrayList<中来记录片段的信息。 INT []取代。 具有单个数字的xs []中的任何元素将被加载为{0,数字},而任何两位数将保持不变。 (例如xs {1} - >偏移{0,1},xs {1,1} - >偏移{1,1}) 在编写代码之后,当我创建一个新片段时,会出现“noPointerException”和“Stackoverflow”错误。我不确定我做错了什么,但显然补偿是空的。 http://i.imgur.com/TWOkONv.png(参见此图。它代表xs []的含义,每个部分由xs []表示,从左上角(0,0)开始以顺时针方向或逆时针方向。) 任何帮助,将不胜感激 。 感谢
private ArrayList<int[]> offsets; // the offsets for the actual squares in this piece
private Color colour; // the colour used for this piece
private int xSize, ySize; // the extent of this piece on each axis
// initialise this piece by setting up the instance variables
// each entry on xs will be either
// - a two-digit number MN representing a square with x-offset = M and y-offset = N
// - a one-digit number N representing a square with x-offset = 0 and y-offset = N
public Piece(int[] xs)
{
ArrayList<int[]> offsets = new ArrayList<int[]>();
if ( xs != null){
for (int i=0;i<xs.length;i++){
if(xs[i] == 0)
offsets.add(new int[] {0,0});
if(xs[i] == 1)
offsets.add(new int[] {0,1});
if(xs[i] == 2)
offsets.add(new int[] {0,2});
if(xs[i] == 3)
offsets.add(new int[] {0,3});
if(xs[i] == 4)
offsets.add(new int[] {0,4});
if(xs[i] == 10)
offsets.add(new int[] {1,0});
if(xs[i] == 11)
offsets.add(new int[]{1,1});
if(xs[i] == 12)
offsets.add(new int[]{1,2});
if(xs[i] == 20)
offsets.add(new int[]{2,0});
if(xs[i] == 21)
offsets.add(new int[]{2,1});
if(xs[i] == 22)
offsets.add(new int[]{2,2});
if(xs[i] == 30)
offsets.add(new int[]{3,0});
else if(xs[i] == 40)
offsets.add(new int[]{4,0});
}
}
public ArrayList<int[]> getOffsets()
{
return offsets;
}
}
测试代码在这里
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class PieceTest
{
private Piece onesq, bigL, hypotheticalT, bighat;
/**
* Sets up the test fixture.
*
* Called before every test case method.
*/
@Before
public void setUp()
{
int[] z1 = {0};
onesq = new Piece(z1);
int[] zL = {0, 1, 2, 12, 22};
bigL = new Piece(zL);
int[] zT = {0, 10, 20, 11, 12, 13, 14};
hypotheticalT = new Piece(zT);
int[] zH = {4, 13, 22, 31, 40, 51, 62, 73, 84};
bighat = new Piece(zH);
}
@Test
public void testonesq()
{
assertEquals("onesq has 1 square", 1, onesq.getOffsets().size());
int[] onesq0 = {0,0};
assertTrue("onesq[0] = {0,0}", java.util.Arrays.equals(onesq.getOffsets().get(0), onesq0));
assertEquals("onesq x extent = 1", 1, onesq.getxSize());
assertEquals("onesq y extent = 1", 1, onesq.getySize());
}
@Test
public void testbigL()
{
assertEquals("bigL has 5 squares", 5, bigL.getOffsets().size());
int[] bigL0 = {0,0};
assertTrue("bigL[0] = {0,0}", java.util.Arrays.equals(bigL.getOffsets().get(0), bigL0));
int[] bigL1 = {0,1};
assertTrue("bigL[1] = {0,1}", java.util.Arrays.equals(bigL.getOffsets().get(1), bigL1));
int[] bigL2 = {0,2};
assertTrue("bigL[2] = {0,2}", java.util.Arrays.equals(bigL.getOffsets().get(2), bigL2));
int[] bigL3 = {1,2};
assertTrue("bigL[3] = {1,2}", java.util.Arrays.equals(bigL.getOffsets().get(3), bigL3));
int[] bigL4 = {2,2};
assertTrue("bigL[4] = {2,2}", java.util.Arrays.equals(bigL.getOffsets().get(4), bigL4));
assertEquals("bigL x extent = 3", 3, bigL.getxSize());
assertEquals("bigL y extent = 3", 3, bigL.getySize());
}
@Test
public void testhypotheticalT()
{
assertEquals("hypotheticalT has 6 squares", 7, hypotheticalT.getOffsets().size());
int[] hypotheticalT0 = {0,0};
assertTrue("hypotheticalT[0] = {0,0}", java.util.Arrays.equals(hypotheticalT.getOffsets().get(0), hypotheticalT0));
int[] hypotheticalT1 = {1,0};
assertTrue("hypotheticalT[1] = {1,0}", java.util.Arrays.equals(hypotheticalT.getOffsets().get(1), hypotheticalT1));
int[] hypotheticalT2 = {2,0};
assertTrue("hypotheticalT[2] = {2,0}", java.util.Arrays.equals(hypotheticalT.getOffsets().get(2), hypotheticalT2));
int[] hypotheticalT3 = {1,1};
assertTrue("hypotheticalT[3] = {1,1}", java.util.Arrays.equals(hypotheticalT.getOffsets().get(3), hypotheticalT3));
int[] hypotheticalT4 = {1,2};
assertTrue("hypotheticalT[4] = {1,2}", java.util.Arrays.equals(hypotheticalT.getOffsets().get(4), hypotheticalT4));
int[] hypotheticalT5 = {1,3};
assertTrue("hypotheticalT[5] = {1,3}", java.util.Arrays.equals(hypotheticalT.getOffsets().get(5), hypotheticalT5));
int[] hypotheticalT6 = {1,4};
assertTrue("hypotheticalT[6] = {1,4}", java.util.Arrays.equals(hypotheticalT.getOffsets().get(6), hypotheticalT6));
assertEquals("hypotheticalT x extent = 3", 3, hypotheticalT.getxSize());
assertEquals("hypotheticalT y extent = 4", 5, hypotheticalT.getySize());
}
@Test
public void testbighat()
{
assertEquals("bighat has 9 squares", 9, bighat.getOffsets().size());
int[] bighatx;
for (int i = 0; i < 9; i++)
{
bighatx = new int[] {i, Math.abs(i - 4)};
assertTrue("bighat[" + i + "] = {" + i + "," + Math.abs(i - 4) + "}", java.util.Arrays.equals(bighat.getOffsets().get(i), bighatx));
}
assertEquals("bighat x extent = 9", 9, bighat.getxSize());
assertEquals("bighat y extent = 5", 5, bighat.getySize());
}
}
http://imgur.com/PqMkDT4,7Qn1xsu#1 是错误发生的地方
答案 0 :(得分:2)
这是因为字段offsets
尚未初始化。在构造函数中,您需要执行此操作
public Piece(int[] xs)
{
this.offsets = new ArrayList<int[]>();
...
}