向ArrayList<>添加项目时的Android NullPointerException

时间:2015-04-04 12:46:44

标签: java android arraylist nullpointerexception

我陷入困境,并且不知道如何解决这个问题NullPointerException ...我没有太大改变但是在几次代码更改之后我遇到了这些问题而我现在无法确定错误来自哪里... 代码:

public class Levelhandler {
    public BoardElement[][] matrix;
    private int levelNr = 1;
    private ArrayList<Wall> allWalls;
    private ArrayList<Character> allCharacters;
    private ArrayList<Finish> allFinishes;

    // context
    private Context context;

    public Levelhandler(Context context) {
        this.context = context;
        matrix = new BoardElement[12][22];
        allWalls = new ArrayList<Wall>();
        Log.v("init", "done init");
        allCharacters = new ArrayList<Character>();
        allFinishes = new ArrayList<Finish>();

        for (int i = 0; i < 12; i++) {
            for (int j = 0; j < 22; j++) {
                matrix[i][j] = null;
            }
        }
    }

..........

public void loadWalls() {
        allWalls = null;
        Scanner s = null;

        try {
            s = new Scanner(context.getAssets().open(
                    "lev" + levelNr + "/walls.txt"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ArrayList<String> list = new ArrayList<String>();
        while (s.hasNext()) {
            list.add(s.next());
        }
        s.close();
        for (String line : list) {
            allWalls.add(new Wall(context,
                    Integer.valueOf(line.split("\\;")[0]), Integer.valueOf(line
                            .split("\\;")[1])));
            matrix[Integer.valueOf(line.split("\\;")[0])][Integer.valueOf(line
                    .split("\\;")[1])] = new Wall(context, Integer.valueOf(line
                    .split("\\;")[0]), Integer.valueOf(line.split("\\;")[1]));
        }
    }

错误:

04-04 14:33:37.970: E/AndroidRuntime(10193): FATAL EXCEPTION: main
04-04 14:33:37.970: E/AndroidRuntime(10193): Process: com.paulk.pacman, PID: 10193
04-04 14:33:37.970: E/AndroidRuntime(10193): java.lang.NullPointerException
04-04 14:33:37.970: E/AndroidRuntime(10193):    at com.paulk.pacman.Levelhandler.loadCharacters(Levelhandler.java:121)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at com.paulk.pacman.PacManView.onDraw(PacManView.java:58)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.View.draw(View.java:15397)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.View.getDisplayList(View.java:14291)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.View.getDisplayList(View.java:14333)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.View.draw(View.java:15111)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.ViewGroup.drawChild(ViewGroup.java:3318)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3155)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.View.getDisplayList(View.java:14286)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.View.getDisplayList(View.java:14333)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.View.draw(View.java:15111)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.ViewGroup.drawChild(ViewGroup.java:3318)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3155)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.View.getDisplayList(View.java:14286)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.View.getDisplayList(View.java:14333)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.View.draw(View.java:15111)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.ViewGroup.drawChild(ViewGroup.java:3318)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3155)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.View.getDisplayList(View.java:14286)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.View.getDisplayList(View.java:14333)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.View.draw(View.java:15111)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.ViewGroup.drawChild(ViewGroup.java:3318)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3155)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.View.getDisplayList(View.java:14286)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.View.getDisplayList(View.java:14333)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.View.draw(View.java:15111)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.ViewGroup.drawChild(ViewGroup.java:3318)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3155)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.View.getDisplayList(View.java:14286)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.View.getDisplayList(View.java:14333)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.View.draw(View.java:15111)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.ViewGroup.drawChild(ViewGroup.java:3318)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3155)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.View.draw(View.java:15400)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.widget.FrameLayout.draw(FrameLayout.java:472)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2583)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.View.getDisplayList(View.java:14291)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.View.getDisplayList(View.java:14333)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1585)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1457)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.ViewRootImpl.draw(ViewRootImpl.java:2779)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2645)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2213)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1256)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6635)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:813)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.Choreographer.doCallbacks(Choreographer.java:613)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.Choreographer.doFrame(Choreographer.java:583)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:799)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.os.Handler.handleCallback(Handler.java:733)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.os.Handler.dispatchMessage(Handler.java:95)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.os.Looper.loop(Looper.java:146)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at android.app.ActivityThread.main(ActivityThread.java:5593)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at java.lang.reflect.Method.invokeNative(Native Method)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at java.lang.reflect.Method.invoke(Method.java:515)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
04-04 14:33:37.970: E/AndroidRuntime(10193):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:3)

只需注释掉这一行allWalls = null;并添加allWalls.clear();

答案 1 :(得分:0)

在您的loadWalls()方法中,您明确尝试清除列表allWalls = null。然后使用allWalls.add(),它将抛出异常,因为变量是null

执行此操作的正确方法如下:

public void loadWalls() {
    //clear current list
    allWalls.clear();
    //...rest of code
}