-1

I am programming a tic-tac-toe android app, and I'm trying to use a for loop to make all the buttons in the array I put them in the same size.

However, when I put this line of code in to do that:

for (int count = 0; count < 9; count++){
    board[count].Height(ht);
}

The app crashes when I try to run it, without explanation.

What is going on?

Here is the entire code for reference:

package tictacstartns;

import com.google.devtools.simple.runtime.components.android.Button;
import com.google.devtools.simple.runtime.components.Component;
import com.google.devtools.simple.runtime.components.HandlesEventDispatching;
import com.google.devtools.simple.runtime.components.android.Form;
import com.google.devtools.simple.runtime.components.android.HorizontalArrangement;
import com.google.devtools.simple.runtime.components.android.Label;
import com.google.devtools.simple.runtime.events.EventDispatcher;



public class TicTacStartNS extends Form implements HandlesEventDispatching {

   private Button btnOne;
   private Button btnTwo;
   private Button btnThree;
   private Button btnFour;
   private Button btnFive;
   private Button btnSix;
   private Button btnSeven;
   private Button btnEight;
   private Button btnNine;

   private Button btnXOchanger;
   private Button btnReset;
   private Button btnClearScore;

   private Label lblTurn;
   private Label lblSpace;
   private Label lblXWins;
   private Label lblXWinsCount;
   private Label lblOWins;
   private Label lblOWinsCount;
   private Label lblCatsGame;
   private Label lblCatsGameCount;

   int ht = 75;
   int wd = 75;
   float fs = 50.0f;

   boolean xGoesNow = true;

   Button board[]=    {btnOne,btnTwo,btnThree,btnFour,btnFive,btnSix,btnSeven,btnEight,btnNine};


      void $define() {

      HorizontalArrangement hr = new HorizontalArrangement(this);
      btnOne = new Button(hr);
      btnTwo = new Button(hr);
      btnThree = new Button (hr);

      hr = new HorizontalArrangement(this);
      board[3] = new Button(hr);
      board[4] = new Button(hr);
      board[5] = new Button(hr);

      hr = new HorizontalArrangement(this);
      board[6] = new Button(hr);
      board[7] = new Button(hr);
      board[8] = new Button(hr);

      for (int count = 0; count < 9; count++){
       board[count].Height(ht);
      }

   EventDispatcher.registerEventForDelegation(this, "JavaBridge", "Click");
}


   @Override
   public boolean dispatchEvent(Component component, String id, String eventName,
                         Object[] args) {

   if (component.equals(btnOne) && eventName.equals("Click")) {
       btnOne.Text(xGoesNow?"X":"O");
       xGoesNow = (xGoesNow?false:true);
       return true;
   } else if (component.equals(btnTwo) && eventName.equals("Click")) {
       btnTwo.Text(xGoesNow?"X":"O");
       xGoesNow = (xGoesNow?false:true);
       return true;
   } else if (component.equals(btnThree) && eventName.equals("Click")) {
       btnThree.Text(xGoesNow?"X":"O");
       xGoesNow = (xGoesNow?false:true);
       return true;
   }
   return false;
   }
}
4

1 回答 1

2
Button board[]=    {btnOne,btnTwo,btnThree,btnFour,btnFive,btnSix,btnSeven,btnEight,btnNine};

它包含所有这些参考。但它们被初始化为null.

当你这样做

btnOne = new Button(hr);

board [0]没有更新,所以它保持null访问时导致 NPE。

这是因为尽管引用指向同一个对象,但当您重新分配一个引用的值时,它只特定该引用。

举个简单的例子:

String str = null;
String [] arr= {str};
str = "hello";
System.out.println ("The value of the reference is " + arr[0]);

输出是 参考值是null

因此,在实际分配它们之前,请确保要分配给数组的引用指向 Object,或者进行双重分配,一个 forbtnOne和一个 for board[0]。但是,由于您已经拥有数组,btnOne因此是多余的。

于 2013-03-07T20:32:41.287 回答