0

请帮助我,因为我很快就会为此发疯:

当我运行代码时,第一次执行 loadNewPoint() 并显示来自全局变量的一些数据 - allPointsAndPlaces

但是,当我单击一个按钮(来自子类)时,相同的方法 loadNewPoint() 为我提供了 allPointsAndPlaces 的空指针。

为了解决这个问题,我对原来的代码结构进行了很多更改,并将这个方法(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

在 Bhumika 的帮助下,我设法找到了解决这个问题的方法。

为了完成这项工作,我必须更改CabbiePoint[] allPointsAndPlaces为静态。

这将以一种方式解决参考问题 - 从孩子到父母。

我还设法通过调试找出了这个参考

quiz = new PointsQuiz(); 

在第二次运行时也为空loadNewPoint()。所以这个子引用 ( PointsQuiz quiz;) 和任何其他对子引用的引用也设置为静态。

于 2013-01-05T16:30:28.187 回答
1

由于allPointsAndPlaces为空,您会收到空指针错误。根据您的编码 的值 allPointsAndPlaces是在 getAllPointsAndLocations() 方法中完成 RPC 调用后分配的。所以allPointsAndPlaces有一些赋值。

在这里,您尝试直接访问loadNewPoint()子类中的方法。一次,allPointsAndPlaces不分配。

于 2013-01-05T07:00:58.520 回答