Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Callback working but variable not initialized??

    Im using smartGWT 2.2 with Firefox 3.6 and eclipse IDE

    My EntryPoint class shows as following

    Code:
    /**
     * 
     */
    package org.gwt.venus.client;
    
    import com.google.gwt.core.client.EntryPoint;
    import com.google.gwt.user.client.rpc.AsyncCallback;
    import com.google.gwt.user.client.ui.RootPanel;
    
    /**
     * @author npradeeptha
     *
     */ 
    import com.smartgwt.client.data.DataSource;
    import com.smartgwt.client.data.DateRange;  
    import com.smartgwt.client.data.RelativeDate;       
    import com.smartgwt.client.widgets.form.DynamicForm;
    import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
    import com.smartgwt.client.widgets.form.fields.DateRangeItem;  
    import com.smartgwt.client.widgets.grid.ListGrid;  
    import com.smartgwt.client.widgets.grid.ListGridField;
    import com.smartgwt.client.widgets.layout.HLayout;
    import com.smartgwt.client.widgets.layout.VLayout;  
      
      
    public class VenusData implements EntryPoint {
    
    	/* (non-Javadoc)
    	 * @see com.google.gwt.core.client.EntryPoint#onModuleLoad()
    	 */
    	VenusDataSource  vds = new VenusDataSource();
    	private RpcCallServiceAsync rpc = RpcInit.initRpc();
    	String[] designstrings = null;
        
    	@Override
    	public void onModuleLoad() {
    		// TODO Auto-generated method stub
    	initializeDesignsArray();
    	DataSource ds = vds.getDs();
    
    	
    	VLayout layout = new VLayout(8);
    	
        HLayout searchFields = new HLayout();
        searchFields.setBackgroundColor("Cyan");
        
        
        DynamicForm searchForm = new DynamicForm();
    	ComboBoxItem cmbDesigns = new ComboBoxItem("cmbDesigns", "Design");
    	ComboBoxItem cmbCriteria = new ComboBoxItem("cmbCriteria", "Criteria");
    	cmbDesigns.setWidth("*");
    	cmbCriteria.setWidth("*");
    	cmbDesigns.setType("comboBox");
    	
    	
    	//cmbDesigns.setValueMap(designstrings);
    	cmbCriteria.setValueMap("FAST","HARSHA_FAST","NIJ_1");
    	
    	
    	
    	
    	// Inline FilterEditor Example (MiniDateRangeItem) 
    	
    	final DateRangeItem rangeItem = new DateRangeItem("Date");  
        rangeItem.setWidth("*");  
        rangeItem.setShowTitle(false);  
        rangeItem.setAllowRelativeDates(true);  
       
       
        
        DateRange dateRange = new DateRange();  
        dateRange.setRelativeStartDate(new RelativeDate("m"));  
        dateRange.setRelativeEndDate(new RelativeDate("m"));  
        rangeItem.setValue(dateRange);  
    
        searchForm.setNumCols(6);
        searchForm.setFields(cmbDesigns,cmbCriteria, rangeItem);
        searchFields.addMember(searchForm);
    
        // Create a ListGrid displaying data from the worldDS and also displaying a FilterEditor  
        final ListGrid dataGrid = new ListGrid();  
        dataGrid.setWidth(655);  
        dataGrid.setHeight(240); 
        searchFields.setWidth(650);
        
        
        ListGridField builds = new ListGridField("build", "Build");
        builds.setWidth(130);
        ListGridField criteria = new ListGridField("criteria", "Criteria");
        ListGridField design = new ListGridField("design", "Design");
        ListGridField lut = new ListGridField("lut", "LUT");    
        ListGridField date = new ListGridField("date", "Date");  
        ListGridField lut4 = new ListGridField("lut4", "Eq LUT4");  
        ListGridField ble = new ListGridField("ble", "BLE FFs");
        ListGridField slack = new ListGridField("slack", "Slack");  
        
      dataGrid.setFields(builds,criteria,design,date,slack,lut,lut4,ble);
       
       dataGrid.setDataSource(ds);
       dataGrid.setAutoFetchData(true);
       System.out.println(designstrings.length); //*** Gives a null pointer error.
       cmbDesigns.setValueMap(designstrings); //does not set any values since designstrings is null.
       
       layout.addMember(searchFields);
       layout.addMember(dataGrid);  
    
        layout.draw(); 
        RootPanel.get("content").add(layout); 
    } 
    	
    	public void initializeDesignsArray(){
    		rpc.getDesign(new AsyncCallback<Design[]>() {
    
    			@Override
    			public void onFailure(Throwable caught) {
    				System.out.println("Design callback failed");
    				
    			}
    
    			@Override
    			public void onSuccess(Design[] result) {
    				//System.out.println(result.length);
    				String[] designs = new String[result.length];
    				
    				for(int i=0; i<designs.length; i++){
    					
    					 designs[i] = result[i].design;
    					 System.out.println(designs[i]); // Prints the items so the callback works and gets data..
    					 
    				}
    				loadDesigns(designs);
    				
    			}
    		});
    	}
    	
    	public void loadDesigns(String[] result) {
    		designstrings = result;
                    System.out.println(designstrings.length); //works too
    
    	}
    }
    I have commented on the code where it works and goes wrong.. The CallBack works perfectly but the variable is always null. Please explain whats going on. Thanks!!

    #2
    You need to read a tutorial on Asyncronous(sp?) calls. Your issue is that the code to print the array is called before the Async call completes. Google for GWT and read through their tutorials first.

    Comment


      #3
      Thank you for the reply! I have gone through all sorts of examples and tutorials. What am I doing wrong here? I don't understand since the call for the DataSource works in a similar way and that has no problems with assignment. Am I doing something wrong in passing several objects with the same server side impl and client side classes? Do I have to make new classes? But the data still gets passed but not initialized. Thats where I'm confused!

      Thanks!

      Comment


        #4
        Until you reach this point in your code
        Code:
        @Override
        public void onSuccess(Design[] result) {
        nothing can assumed about the designstrings variable. Your setValueMap() call should be inside the onSuccess(..) function, otherwise there is no way to guarantee the server has already returned the data back. Again start by looking up the definition of Asynchronous that should be best the starting point. You can't make an asynchronous call and assume it will return at a given point unless you in the onSuccess() callback period.

        Comment


          #5
          I see! I get what you mean. Thank you very much! I will read on Asynchronous calls.
          Last edited by npradeeptha; 8 Jul 2010, 23:37.

          Comment


            #6
            Hi,

            Can you please post the solution.
            Even i'm stuck with the same problem.
            Please help

            Thanks

            Comment

            Working...
            X