3

I am gettin null pointer exception on this line wsresult = restClientInterface.skuska(); for restClientInterface. Here is my sample:

import org.springframework.http.converter.json.GsonHttpMessageConverter;

import com.googlecode.androidannotations.annotations.rest.Get;
import com.googlecode.androidannotations.annotations.rest.Rest;

@Rest(rootUrl = "http://my_ip_address/webresources", converters = {GsonHttpMessageConverter.class})
public interface RestClient {

    @Get("/persons")
    String skuska();    
}

And i am using in it fragment

    @EFragment
public class HomeFragment extends Fragment {

private Button ws;
private TextView wsstring;
private String wsresult;        

        @RestService
        RestClient restClientInterface;

        wsstring = (TextView) view.findViewById(R.id.wsstring);

                ws = (Button) view.findViewById(R.id.ws);
                ws.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        getWSResult();
                        wsstring.setText(wsresult);         
                    }
                });

                return view;
            }

            @Background
            public void getWSResult() {
                 wsresult = restClientInterface.skuska();       
            }
Szalai Ladislav
  • 161
  • 1
  • 1
  • 7

1 Answers1

2

Your RestClient should be injected correctly by AA just after your fragment is ready. Could you copy/paste the generated class to see what's happening ?

Moreover, you're calling getWSResult() (which is a background method) and just after you're setting the result of the Rest call in your TextView. But you can't say if the result arrived before you're trying to setting it in your object.

You should try with a code like this one :

@EFragment(R.layout.homeFragment)
public class homeFragment extends Fragment {

    @ViewById
    Button ws;

    @ViewById
    TextView wsstring;

    @RestService
    RestClient restClientInterface;

    private String wsresult;

    @Click
    void wsstringClicked() {
        getWSResult();
    }

    @Background
    void getWSResult() {
        wsresult = restClientInterface.skuska();
    }

    @UiThread
    void updateUI() {
        wsstring.setText(wsresult);
    }
}

EDIT: Just removed private on @ViewById annotated fields

EDIT2: Just thinking about it. how are you using this fragment in your activities ? Are you using either @FragmentById or @FragmentByTag ?

DayS
  • 1,561
  • 11
  • 15
  • Can you put a breakpoint or a log on line 17 of the generated fragment and tell me if you have something ? If this method isn't called you may have spotted a bug in AA and I invite you to report the issue on the [github](https://github.com/excilys/androidannotations/issues?state=open) – DayS Feb 21 '13 at 19:11
  • Before posting an issue, please try the syntaxe I suggested to you yesterday (ie: By using either `@FragmentById` or `@ FragmentByTag` to inject your fragment in your activity – DayS Feb 21 '13 at 19:13
  • Its already solved. Problem was in initialization of the components in onCreate method. I ve put it to @AfterViews annotated method and its working. Thanks DayS anyway – Szalai Ladislav Feb 23 '13 at 19:07