0

My issue is that i am trying to get the String Array to Shuffle and display the Random List back into the List View. my shuffle is inside my Custom Adapter.

My SongAdapter

public class SongAdapter extends ArrayAdapter<Model> {
 public static String[] listSongs = new String[]{}; //String Array of ListSongs#
public void loadFile() {
        try {
            Resources ResFiles = myContext.getResources(); //ResFile is set to getResources,
            InputStream ReadDbFile = ResFiles.openRawResource(R.raw.song); //InputStream is Called to get Text file Songs
            BufferedReader reader = new BufferedReader(new InputStreamReader(ReadDbFile));
            String DbLines;
            while ((DbLines = reader.readLine()) != null)//While DBlines is not Null keep Reading.
            {
                listSongs = DbLines.split("#");//Dblines is Splitting the Text where # is..
                Model SongModel1 = new Model(); //Call the Model Class
                SongModel1.setName(listSongs[0]);//Set Name from String Array
                SongModel1.setSigner(listSongs[1]);//Set Signer from String Array
                SongModel1.setUrl(listSongs[2]); //Set URL from String Array
                this.add(SongModel1);//Add SongModel String Array to the Model Class
            }
            //Catch any Exception..
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void shuffle(){
        Collections.shuffle(Arrays.asList(listSongs));
        notifyDataSetChanged();
    }

List Fragment

public class MenuFragment extends ListFragment {
    static protected SongAdapter adapter;



    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.list_fragment, container, false);
        adapter = new SongAdapter(getActivity(),
                android.R.layout.simple_list_item_1);
        setListAdapter(adapter);
        setHasOptionsMenu(true);
        return view;
    }

And Finally i am calling in the ActionBar On Click inside Main class

public class MainActivity extends ActionBarActivity {
    static protected SongAdapter adapter;
@Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId()) {
            case R.id.shuffle:
                adapter.shuffle();
               return true;

The Error i am getting at Run time is

E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.dfoley.project2, PID: 1304
    java.lang.NullPointerException
            at com.example.dfoley.project2.MainActivity.onOptionsItemSelected(MainActivity.java:135)
            at android.app.Activity.onMenuItemSelected(Activity.java:2600)
            at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:350)
            at android.support.v7.app.ActionBarActivity.onMenuItemSelected(ActionBarActivity.java:155)
            at android.support.v7.app.ActionBarActivityDelegate$1.onMenuItemSelected(ActionBarActivityDelegate.java:74)
            at android.support.v7.app.ActionBarActivityDelegateBase.onMenuItemSelected(ActionBarActivityDelegateBase.java:551)
            at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:802)
            at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
            at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:949)
            at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:939)
            at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:596)
            at android.support.v7.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:145)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
Dave
  • 71
  • 10
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Simon Dec 03 '14 at 20:36

2 Answers2

1

You should use MenuFragment.adapter.shuffle() because your adapter on your MainActivity is different from the other one (which is inside your MenuFragment)

As an advice, you should create a public method on your MenuFragment that calls adapter.shuffle() and call it from your MainActivity.

DavidGSola
  • 697
  • 5
  • 17
  • So i should take that Method out of the Adpater Class and place it inside my MenuFragment then Call that then to the MainActivity – Dave Dec 03 '14 at 20:38
  • Not necessarily, you can create that method (called methodA) on your `MenuFragment` and inside that method you call `adapter.shuffle()` from your Adapter. Finally, call from your `MainActivity` your `fragment.methodA` – DavidGSola Dec 03 '14 at 20:42
  • Hi I added the `adapter.shuffle` to the `MenuFragment` and then called MethodA inside the `MainActivity` but still show null Pointer but it stop the Application from Crashing. – Dave Dec 03 '14 at 21:55
1

the SongAdapter in your Fragment is different from the one in your Activity, and only the one in the Fragment is initialized. What you could do is to define a method in your Fragment that calls array.shuffle() and call this method when you press on the menu's button

Blackbelt
  • 156,034
  • 29
  • 297
  • 305