0

私はすぐにこれに腹を立てるので、私を助けてください:

コードを実行すると、最初にloadNewPoint()が実行され、グローバル変数(allPointsAndPlaces)からのデータが表示されます。

ただし、(子クラスの)ボタンをクリックすると、同じメソッドloadNewPoint()によってallPointsAndPlacesのnullポインターが返されます。

この問題を解決するために、コード構造を元のメソッドから大幅に変更し、このメソッド(loadNewPoint())を親クラスに移動して、問題が解決するかどうかを確認しました。

親クラス:

public class CabbieApp implements EntryPoint {

  private GetLocationsServiceAsync getAllLocationsService = GWT.create(GetLocationsService.class);
  CabbiePoint[] allPointsAndPlaces;
  PointsQuiz quiz;

  /**
   * Entry point method.
   */
  public void onModuleLoad() {

      //Get all the required data from DB
      getAllPointsAndLocations();

  }

  private void loadAppPages(){
      // Associate the Main panel with the HTML host page.
      RootPanel rootPanel = RootPanel.get("pointsList");    

      quiz = new PointsQuiz();

      rootPanel.setStyleName("GWTapp");
      rootPanel.add(quiz.getMainPanel());

      loadNewPoint();

  }


    private void getAllPointsAndLocations() {
        // Initialize the service proxy.
        if (getAllLocationsService == null) {
            getAllLocationsService = GWT.create(GetLocationsService.class);
        }

        // Set up the callback object.
        AsyncCallback<CabbiePoint[]> callback = new AsyncCallback<CabbiePoint[]>() {
          public void onFailure(Throwable caught) {
              System.out.println(caught.getMessage());
          }

          public void onSuccess(CabbiePoint[] result) {
            //allPointsAndPlaces = result;
            System.out.println(result.length);
            allPointsAndPlaces = result;
            loadAppPages();


          }
        };

        // Make the call to the service.
        getAllLocationsService.getLocations(callback);

    }


    void loadNewPoint(){
        int r = Random.nextInt(allPointsAndPlaces.length);
        quiz.CurrentPlace = allPointsAndPlaces[r].getPlaceName();
        quiz.CurrentLocation = allPointsAndPlaces[r].getPlaceLocation();

        quiz.point.setText(quiz.CurrentPlace);
        quiz.location.setText(quiz.CurrentLocation);
        quiz.location.setStyleName("invisibleText");
    }

  }

子クラス:

public class PointsQuiz extends CabbieApp{

       VerticalPanel mainPanel = new VerticalPanel();
       HorizontalPanel navigation = new HorizontalPanel();
       TextBox point = new TextBox();
       TextBox location = new TextBox();
       Button showLocation = new Button("Show Location");
       Button nextPoint = new Button("Next Point");
       String CurrentPlace, CurrentLocation;


      public PointsQuiz() {

          // Assemble Add Stock panel.
              navigation.add(showLocation);
              navigation.add(nextPoint);
              navigation.setCellHorizontalAlignment(nextPoint, HasHorizontalAlignment.ALIGN_RIGHT);
              navigation.addStyleName("addPanel");
              mainPanel.setSpacing(5);
              mainPanel.setStyleName("body");
              mainPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
              mainPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

            // Assemble Main panel.
            mainPanel.add(point);
            point.setWidth("200px");
            mainPanel.add(location);
            location.setWidth("200px");
            mainPanel.add(navigation);
            navigation.setWidth("200px");

         // Move cursor focus to the input box.
            showLocation.setFocus(true);

         // Listen for mouse events on the show location button.
            showLocation.addClickHandler(new ClickHandler() {
              public void onClick(ClickEvent event) {
                showCurrentLocation();}
              });


             // Listen for mouse events on the next point button.
            nextPoint.addClickHandler(new ClickHandler() {
              public void onClick(ClickEvent event) {
                loadNewPoint();
                  }
            });       
      }



        private void showCurrentLocation(){
            location.setStyleName("visibleText");
        }

        public VerticalPanel getMainPanel() {
            return mainPanel;
        }


}
4

2 に答える 2

2

私はブミカの助けを借りてこの問題の解決策を見つけることができました。

この作業を行うにはCabbiePoint[] allPointsAndPlaces、静的に変更する必要がありました。

これにより、参照の問題が1つの方法(子から親へ)で解決されます。

また、私はトラフのデバッグを見つけることができました、このリファレンス

quiz = new PointsQuiz(); 

の2回目の実行でもnullになりますloadNewPoint()。したがって、この子参照(PointsQuiz quiz;)およびその他の子への参照も静的に設定されました。

于 2013-01-05T16:30:28.187 に答える
1

がnullであるため、nullポインタエラーが発生しallPointsAndPlacesます。コーディングに従って、の値は allPointsAndPlacesgetAllPointsAndLocations()メソッドでのRPC呼び出しの完了後に割り当てられます。したがって、にallPointsAndPlacesはいくつかの値が割り当てられています。

loadNewPoint()ここでは、子クラスのメソッドに直接アクセスしようとします。一度に、allPointsAndPlacesは割り当てられません。

于 2013-01-05T07:00:58.520 に答える