-2

I want to show string (parsed json file) on textview at fragment.

I started project with navigation drawer activity.

parsing is succeeded but

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)'

error occurred

How can I fix it?

MainAcitvity.java

TextView tv=(TextView)findViewById(R.id.text_slideshow);
StringBuilder sb=new StringBuilder();
String json = null;

        try {

            InputStream is = getAssets().open("tour.json");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            json = new String(buffer, "UTF-8");


            Log.i("TEST", "afterfileio");

            JSONParser parser=new JSONParser();
            JSONObject jsonObject= (JSONObject) parser.parse(json);
            JSONObject jsonObject1=(JSONObject)jsonObject.get("facilityConveInfo");
            JSONArray jsonArray = (JSONArray) jsonObject1.get("row");




            Log.i("TEST", ""+jsonArray.size() + "");
            for (int i = 0; i < jsonArray.size(); i++) {


                JSONObject subJsonObject = (JSONObject) jsonArray.get(i);
                String FACILITY_NM =subJsonObject.get("FACILITY_NM").toString(); 


                sb.append(FACILITY_NM);

            }
            tv.setText(sb.toString());

fragment_slideshow.xml

<TextView
    android:id="@+id/text_slideshow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:textAlignment="center"
    android:textSize="20sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

SlideFragment.java

public class SlideshowFragment extends Fragment {

    private SlideshowViewModel slideshowViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        slideshowViewModel =
                ViewModelProviders.of(this).get(SlideshowViewModel.class);
        View root = inflater.inflate(R.layout.fragment_slideshow, container, false);
        final TextView textView = root.findViewById(R.id.text_slideshow);
        slideshowViewModel.getText().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });
    return root;
Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
yu0321
  • 5
  • 1
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Manoj Perumarath Sep 08 '19 at 12:10

1 Answers1

0

It look to me like your never created the fragment, so sb referencing to null.

  1. create the fragment
  2. init the fragment (new);
  3. start a fragment transaction, something like this

    FragmentTransaction fragmentTransaction = 
    getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, yourFragment);
    fragmentTransaction.commit();
    

good luck

Dor
  • 617
  • 1
  • 5
  • 11