2015-04-04 103 views
-3

我被困在這一點,不知道如何解決這個NullPointerException ...我沒有太多改變,但在幾個代碼更改後,我有這些問題,我現在不能當錯誤來自... 代碼:Android NullPointerException當添加項目ArrayList <>

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) 
+0

顯示'loadCharacters'方法 – nikis 2015-04-04 12:49:47

+0

的NPE在loadCharacters發生的事情,你沒有發佈。 – m0skit0 2015-04-04 12:49:53

+2

錯誤消息告訴你發生了NullPointerException的位置。您是否嘗試調試代碼? – CKing 2015-04-04 12:49:56

回答

3

只是註釋掉該行allWalls = null;並添加allWalls.clear();

+0

謝謝你soooooo這很好的作品;) – Paulomato 2015-04-04 12:55:52

+0

@Paulomato學會閱讀stacktraces隊友 – m0skit0 2015-04-04 13:05:47

+0

:P我將在下一次 – Paulomato 2015-04-04 14:22:53

0

在您的loadWalls()方法中,您使得allWalls = null明顯嘗試清除列表。然後,使用allWalls.add(),這將拋出異常的變量是null

做,這是如下正確的方法:

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