0

I am getting an error when trying to set text in textview. I have tried updating gradle(which is suggested by one of stack overflow developer's solution), also tried many solution from this same question. Nothing helped me.

ProfileFragment.java

public class ProfileFragment extends Fragment {

    private TextView txtUsername, txtExperience;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_profile, container, false);
        txtUsername = view.findViewById(R.id.txt_username1);
        getUserInfo(view.getContext(),view,null);
        return view;
    }

    protected User getUserInfo(Context context, View view, String user) {

        Map<String, String> userDetails = new HashMap<>();
        userDetails.put("userid",user);
        final User[] userinfo = new User[1];

        Call<User> call = RetrofitFactoryWithJwt.getRetrofitInstance(context).getUserProfile(userDetails);
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {

                if(response.isSuccessful() && response.body() != null){

                    txtUsername.setText(response.body().getName());
                    userinfo[0] = response.body();
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                Log.d("onFailure",Log.getStackTraceString(t));
            }
        });

        return userinfo[0];
    }
}

fragment_profile.xml

<androidx.coordinatorlayout.widget.CoordinatorLayout
    tools:context="com.work.workapp.profile.ProfileFragment"
    android:id="@+id/profile_main">
    <LinearLayout>
        <TextView
                    android:id="@+id/txt_username1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>

                <TextView
                    android:id="@+id/txt_experience1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>

    </LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

logcat error I am getting is the following:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.work.myapp, PID: 7568
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.work.workapp.profile.ProfileFragment$6.onResponse(ProfileFragment.java:236)
        at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$1.run(DefaultCallAdapterFactory.java:83)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:7325)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
rupal3112
  • 25
  • 2
  • 9
  • Are you sure it's the `TextView` that's causing the problem? Please [edit] your question to provide the complete [stack trace](https://stackoverflow.com/a/23353174). – Mike M. Dec 05 '19 at 14:25
  • I have updated the question. – rupal3112 Dec 05 '19 at 14:53
  • Are you certain that's the correct layout? Do you have multiples of that layout; e.g., possibly one each for portrait and landscape, or for different Android versions, etc.? Is that the only place that you call `getUserInfo()`? Why is it `protected`? Are you possibly calling it from a `ProfileFragment` subclass? What happens if you try `txtUsername = getView().findViewById(R.id.txt_username1);` in `onResponse()`? Do the Exception and message change? – Mike M. Dec 05 '19 at 16:17
  • Yes, I have called same method in one of its tab layout’s file. – rupal3112 Dec 05 '19 at 16:42
  • I'm not quite sure exactly what you mean by that, but it sounds like it could be the problem. We'd have to see a [mcve], though, to be able to tell you specifically what's wrong. – Mike M. Dec 05 '19 at 16:47
  • I have created instance of profile fragment in its tab-TabInfo.java. Using this instance I have called getUserInfo() method because I want same data on both files. – rupal3112 Dec 05 '19 at 16:50
  • Unless that instance is transacted with a `FragmentManager`, it will never run through its lifecycle methods, and `onCreateView()` will never `inflate()` that layout, nor find that `TextView`. – Mike M. Dec 05 '19 at 16:53
  • Then how can I send this data to its Tab layout? Can you please suggest some guidance on this? – rupal3112 Dec 05 '19 at 17:08
  • No, I can't, sorry. I really don't know what you're trying to do, specifically. – Mike M. Dec 05 '19 at 17:10
  • 1
    okay. No problem. You solved my issue. Thank you. – rupal3112 Dec 05 '19 at 17:11

2 Answers2

0

In your setText, the method response.body().getName() get null response.

mili2501
  • 452
  • 5
  • 9
-1

Your view was not inflated and created yet. so move your getUserInfo to onViewCreated