-3

Below is the Logcat for the error that I am getting, json_string = getActivity().getIntent().getExtras().getString( "json_data" );

 09-10 21:56:07.507 22129-22129/com.example.aids.a09application E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                     Process: com.example.aids.a09application, PID: 22129
                                                                                     java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
                                                                                         at com.example.aids.a09application.DisplayListView.onCreateView(DisplayListView.java:31)
                                                                                         at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
                                                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
                                                                                         at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                                                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
                                                                                         at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
                                                                                         at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
                                                                                         at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
                                                                                         at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
                                                                                         at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
                                                                                         at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
                                                                                         at android.os.Handler.handleCallback(Handler.java:751)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                         at android.os.Looper.loop(Looper.java:154)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:6692)
                                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

This is the Fragment below that will lead into another Fragment DisplayinListView, Using a fragment transaction found at the bottom of this code. Originally this was an intent as the DisplayinListView was an activity, however I have now changed it to a fragment, due to the Navigation drawer.

public class StandingsList extends Fragment implements View.OnClickListener {
    //make member variable is Views
    Button mButton;
    Button mButton1;
    TextView mResult;
    String JSON_RESPONSE;
    String json_string;
    FragmentTransaction fragmentTransaction;

    ProgressDialog mProgressDialog;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate( R.layout.fragment_standings, container, false );


        //get reference of the views
        mButton = (Button) view.findViewById( R.id.button );
        mButton1 = (Button) view.findViewById( R.id.buttontwo );
        mResult = (TextView) view.findViewById( R.id.result );
        mButton1.setOnClickListener(this);

        //when button is clicked
        mButton.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //call the getJsonResponse method and fetch the response from the server
                new getJsonResponse().execute();
            }
        } );

        return view;
    }


        public class getJsonResponse extends AsyncTask<Void, Void, String> {
            String serverUrl;

            public getJsonResponse() {
                mProgressDialog = new ProgressDialog( getActivity() );
                mProgressDialog.setMessage( "Please Wait" );
                mProgressDialog.setTitle( "Processing" );
                mProgressDialog.setCancelable( false );
            }

            @Override
            protected void onPreExecute() {

                //set the url from we have to fetch the json response
                // DO NOT ADD REAL IP.
                serverUrl = "http://xx.xx.xx.xx/get_info.php";
                mProgressDialog.show();
            }

            @Override
            protected String doInBackground(Void... params) {
                try {
                    URL url = new URL( serverUrl );
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    InputStream inputStream = httpURLConnection.getInputStream();
                    BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( inputStream ) );
                    StringBuilder stringBuilder = new StringBuilder();

                    while ((JSON_RESPONSE = bufferedReader.readLine()) != null) {

                        stringBuilder.append( JSON_RESPONSE + "\n" );

                    }

                    inputStream.close();
                    bufferedReader.close();
                    httpURLConnection.disconnect();

                    return stringBuilder.toString().trim();

                } catch (MalformedURLException e) {
                    Log.e( TAG, "MalformedURLException: " + e ); //print exception message to log
                } catch (IOException e) {
                    Log.e( TAG, "IOException: " + e ); //print exception message to log
                }
                return null;
            }

            @Override
            protected void onProgressUpdate(Void... values) {
                super.onProgressUpdate( values );
            }

            @Override
            protected void onPostExecute(String result) {
                //set the result which is returned by doInBackground() method to result textView
                mResult.setText( result );
                mProgressDialog.dismiss();
                json_string = result;
            }

        }

            public void onClick(View view){

                switch (view.getId()) {
                    case R.id.buttontwo:
                        fragmentTransaction = getFragmentManager().beginTransaction();
                        fragmentTransaction.replace( main_container, new DisplayListView());
                        fragmentTransaction.commit();
                        break;

                }


            }
        }

This is the class where the error is, on line 31 - json_string = getActivity().getIntent().getExtras().getString( "json_data" );

This error has been only created since I converted this class to a Fragment from an activity.

public class DisplayListView extends Fragment {

    String json_string;
    JSONObject jsonObject;
    JSONArray jsonArray;
    StandingsAdapter standingsAdapter;
    ListView listView;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate( R.layout.display_list_view_layout, container, false );
        listView= (ListView) view.findViewById( R.id.ListViewParse );
        standingsAdapter = new StandingsAdapter(getActivity(),R.layout.row_layout);
        listView.setAdapter( standingsAdapter );
        json_string = getActivity().getIntent().getExtras().getString( "json_data" );
        try {
            jsonArray  = new JSONObject(json_string).getJSONArray("server_response");


            int count = 0;

            int driver_id, team_id, position, points;
            String firstName, lastName;



            while(count<jsonArray.length())
            {
                JSONObject JO = jsonArray.getJSONObject(count);
                driver_id = JO.getInt( "Driver_id" );
                team_id =JO.getInt( "Team_id" );
                firstName = JO.getString( "First_name" );
                lastName= JO.getString( "Last_name" );
                position= JO.getInt( "Position" );
                points = JO.getInt( "Points" );

                Standings standings = new Standings(driver_id, team_id, firstName, lastName, position, points );
                standingsAdapter.add( standings );
                count++;
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return view;
    }

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
user8534232
  • 57
  • 2
  • 8

2 Answers2

1

replace

json_string = getActivity().getIntent().getExtras().getString( "json_data" );

by

json_string = getActivity().getIntent().getStringExtra( "json_data" );
phpdroid
  • 1,642
  • 1
  • 18
  • 36
1

Now, You have replaced your activity with new Fragment.So You have to pass the data as an argument.You can do as I mention in below code.

    Bundle bundle = new Bundle();
    bundle.putString(Constants.STRING_JSON, json_string);
    replaceFragment(DisplayListView.newInstance(bundle));

Now,You have to get this json_string in your actual fragnent ad below.

  public static DisplayListView newInstance(Bundle bungle) {
    DisplayListView fragment = new DisplayListView();
    fragment.setArguments(bungle);
    return fragment;}

You can retrieve the value from arguments in Oncreate method of fragment as below

String json_string = getArguments().getString(Constants.STRING_JSON);
VP4Android
  • 576
  • 4
  • 17