Firebase数据库“ onDataChange”方法NullPointerException

时间:2019-09-14 21:19:54

标签: java android firebase firebase-realtime-database

如果这个问题很明显,您好,但是自从两天后,我得到了空指针异常


E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplicationteeeeeeeeeest, PID: 13439
    java.lang.NullPointerException: Attempt to invoke virtual method 'void java.util.ArrayList.add(int, java.lang.Object)' on a null object reference
        at com.example.myapplicationteeeeeeeeeest.ui.FSettings.SettingsFragment$4.onDataChange(SettingsFragment.java:336)
        at com.google.firebase.database.Query$1.onDataChange(com.google.firebase:firebase-database@@19.0.0:179)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.0.0:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.0.0:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.0.0:55)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
I/Process: Sending signal. PID: 13439 SIG: 9
Disconnected from the target VM, address: 'localhost:8617', transport: 'socket'

我知道Firebase是异步的,返回值是null,但是它随机出现。 这是它所使用的类。

package com.example.myapplicationteeeeeeeeeest.ui.FSettings;

import ...



public class SettingsFragment extends Fragment  {

    private SettingsViewModel sendViewModel;

    private Recipe recipe_1,recipe_2,recipe_3,recipe_4,recipe_5,recipe_6,recipe_7,recipe_8,recipe_9,recipe_10,recipe_11,recipe_12,recipe_13,recipe_14,recipe_15,recipe_16,recipe_17,recipe_18,recipe_19,recipe_20;
    private ArrayList<String> ingridients1,ingridients2,ingridients3,ingridients4,ingridients5,ingridients6,ingridients7,ingridients8,ingridients9,ingridients10,ingridients11,ingridients12,ingridients13,ingridients14,ingridients15,ingridients16,ingridients17,ingridients18,ingridients19,ingridients20;
    private View root;
    private String[] resultAmount,resultIngredients;
    ExpandableListView expandableListView;
    ExpandableListAdapter expandableListAdapter;
    List<String> expandableListTitle;
    HashMap<String, List<String>> expandableListDetail;



    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        sendViewModel =
                ViewModelProviders.of( this ).get( SettingsViewModel.class );
        View root = inflater.inflate( R.layout.fragment_settings, container, false );

        expandableListView = (ExpandableListView) root.findViewById(R.id.expandableListView);
        expandableListDetail = ExpandableListDataPump.getData();
        expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
        expandableListAdapter = new CustomExpandableListAdapter(getActivity(), expandableListTitle, expandableListDetail);
        expandableListView.setAdapter(expandableListAdapter);
        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                Toast.makeText(getActivity().getApplicationContext(),
                        expandableListTitle.get(groupPosition) + " List Expanded.",
                        Toast.LENGTH_SHORT).show();
            }
        });

        expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {
                Toast.makeText(getActivity().getApplicationContext(),
                        expandableListTitle.get(groupPosition) + " List Collapsed.",
                        Toast.LENGTH_SHORT).show();
            }
        });

        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                Toast.makeText(
                        getActivity().getApplicationContext(),
                        expandableListTitle.get(groupPosition)
                                + " -> "
                                + expandableListDetail.get(
                                expandableListTitle.get(groupPosition)).get(
                                childPosition), Toast.LENGTH_SHORT
                ).show();
                return false;
            }
        });
        obtainFirebase();
        return root;
    }

    private void initIngredients() {
        ingridients1 = new ArrayList<>(  );
        ingridients2 = new ArrayList<>(  );
        ingridients3 = new ArrayList<>(  );
        ingridients4 = new ArrayList<>(  );
        ingridients5 = new ArrayList<>(  );
        ingridients6 = new ArrayList<>(  );
        ingridients7 = new ArrayList<>(  );
        ingridients8 = new ArrayList<>(  );
        ingridients9 = new ArrayList<>(  );
        ingridients10 = new ArrayList<>(  );
        ingridients11 = new ArrayList<>(  );
        ingridients12 = new ArrayList<>(  );
        ingridients13 = new ArrayList<>(  );
        ingridients14 = new ArrayList<>(  );
        ingridients15 = new ArrayList<>(  );
        ingridients16 = new ArrayList<>(  );
        ingridients17 = new ArrayList<>(  );
        ingridients18 = new ArrayList<>(  );
        ingridients19 = new ArrayList<>(  );
        ingridients20 = new ArrayList<>(  );
        resultAmount = new String[15];
        resultIngredients = new String[15];

    }

    public void obtainFirebase() {

        FirebaseDatabase.getInstance().getReference().child( "recipes" )
                .addListenerForSingleValueEvent( new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                            if( snapshot.getKey().equals("recipe_2")) {
                                //do your stuff
                                recipe_2 = snapshot.getValue( Recipe.class );
                                ArrayList<String> amountIngridients = new ArrayList<>(  );
                                ArrayList<String> ingridients = new ArrayList<>(  );

                                String[] resultAmount = new String[15];
                                resultAmount = Arrays.copyOf(recipe_2.getAmountOfIngredients().values().toArray(),recipe_2.getAmountOfIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( amountIngridients,resultAmount );

                                String[] resultIngredients = new String[15];
                                resultIngredients = Arrays.copyOf(recipe_2.getIngredients().values().toArray(),recipe_2.getIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( ingridients,resultAmount );

                                for (int i = 0; i < resultAmount.length;i++) {
                                    ingridients2.add( i,resultAmount[i]+resultIngredients[i]);
                                }
                                Arrays.fill( resultAmount,null );
                                Arrays.fill( resultIngredients,null );



                            }

                            if( snapshot.getKey().equals("recipe_3")) {
                                //do your stuff
                                recipe_3 = snapshot.getValue( Recipe.class );
                                ArrayList<String> amountIngridients = new ArrayList<>(  );
                                ArrayList<String> ingridients = new ArrayList<>(  );

                                String[] resultAmount = new String[15];
                                resultAmount = Arrays.copyOf(recipe_3.getAmountOfIngredients().values().toArray(),recipe_3.getAmountOfIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( amountIngridients,resultAmount );

                                String[] resultIngredients = new String[15];
                                resultIngredients = Arrays.copyOf(recipe_3.getIngredients().values().toArray(),recipe_3.getIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( ingridients,resultAmount );

                                for (int i = 0; i < resultAmount.length;i++) {
                                    ingridients3.add( i,resultAmount[i]+resultIngredients[i]);
                                }
                                Arrays.fill( resultAmount,null );
                                Arrays.fill( resultIngredients,null );
                            }

                            if( snapshot.getKey().equals("recipe_4")) {
                                //do your stuff
                                recipe_4 = snapshot.getValue( Recipe.class );
                                ArrayList<String> amountIngridients = new ArrayList<>(  );
                                ArrayList<String> ingridients = new ArrayList<>(  );

                                String[] resultAmount = new String[15];
                                resultAmount = Arrays.copyOf(recipe_4.getAmountOfIngredients().values().toArray(),recipe_4.getAmountOfIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( amountIngridients,resultAmount );

                                String[] resultIngredients = new String[15];
                                resultIngredients = Arrays.copyOf(recipe_4.getIngredients().values().toArray(),recipe_4.getIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( ingridients,resultAmount );

                                for (int i = 0; i < resultAmount.length;i++) {
                                    ingridients4.add( i,resultAmount[i]+resultIngredients[i]);
                                }
                                Arrays.fill( resultAmount,null );
                                Arrays.fill( resultIngredients,null );

                            }

                            if( snapshot.getKey().equals("recipe_5")) {
                                //do your stuff
                                recipe_5 = snapshot.getValue( Recipe.class );
                                ArrayList<String> amountIngridients = new ArrayList<>(  );
                                ArrayList<String> ingridients = new ArrayList<>(  );

                                String[] resultAmount = new String[15];
                                resultAmount = Arrays.copyOf(recipe_5.getAmountOfIngredients().values().toArray(),recipe_5.getAmountOfIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( amountIngridients,resultAmount );

                                String[] resultIngredients = new String[15];
                                resultIngredients = Arrays.copyOf(recipe_5.getIngredients().values().toArray(),recipe_5.getIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( ingridients,resultAmount );

                                for (int i = 0; i < resultAmount.length;i++) {
                                    ingridients5.add( i,resultAmount[i]+resultIngredients[i]);
                                }
                                Arrays.fill( resultAmount,null );
                                Arrays.fill( resultIngredients,null );

                            }

                            if( snapshot.getKey().equals("recipe_6")) {
                                //do your stuff
                                recipe_6 = snapshot.getValue( Recipe.class );
                                ArrayList<String> amountIngridients = new ArrayList<>(  );
                                ArrayList<String> ingridients = new ArrayList<>(  );

                                String[] resultAmount = new String[15];
                                resultAmount = Arrays.copyOf(recipe_6.getAmountOfIngredients().values().toArray(),recipe_6.getAmountOfIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( amountIngridients,resultAmount );

                                String[] resultIngredients = new String[15];
                                resultIngredients = Arrays.copyOf(recipe_6.getIngredients().values().toArray(),recipe_6.getIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( ingridients,resultAmount );

                                for (int i = 0; i < resultAmount.length;i++) {
                                    ingridients6.add( i,resultAmount[i]+resultIngredients[i]);
                                }
                                Arrays.fill( resultAmount,null );
                                Arrays.fill( resultIngredients,null );

                            }

                            if( snapshot.getKey().equals("recipe_7")) {
                                //do your stuff
                                recipe_7 = snapshot.getValue( Recipe.class );
                                ArrayList<String> amountIngridients = new ArrayList<>(  );
                                ArrayList<String> ingridients = new ArrayList<>(  );

                                String[] resultAmount = new String[15];
                                resultAmount = Arrays.copyOf(recipe_7.getAmountOfIngredients().values().toArray(),recipe_7.getAmountOfIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( amountIngridients,resultAmount );

                                String[] resultIngredients = new String[15];
                                resultIngredients = Arrays.copyOf(recipe_7.getIngredients().values().toArray(),recipe_7.getIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( ingridients,resultAmount );

                                for (int i = 0; i < resultAmount.length;i++) {
                                    ingridients7.add( i,resultAmount[i]+resultIngredients[i]);
                                }
                                Arrays.fill( resultAmount,null );
                                Arrays.fill( resultIngredients,null );

                            }

                            if( snapshot.getKey().equals("recipe_8")) {
                                //do your stuff
                                recipe_8 = snapshot.getValue( Recipe.class );
                                ArrayList<String> amountIngridients = new ArrayList<>(  );
                                ArrayList<String> ingridients = new ArrayList<>(  );

                                String[] resultAmount = new String[15];
                                resultAmount = Arrays.copyOf(recipe_8.getAmountOfIngredients().values().toArray(),recipe_8.getAmountOfIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( amountIngridients,resultAmount );

                                String[] resultIngredients = new String[15];
                                resultIngredients = Arrays.copyOf(recipe_8.getIngredients().values().toArray(),recipe_8.getIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( ingridients,resultAmount );

                                for (int i = 0; i < resultAmount.length;i++) {
                                    ingridients8.add( i,resultAmount[i]+resultIngredients[i]);
                                }
                                Arrays.fill( resultAmount,null );
                                Arrays.fill( resultIngredients,null );

                            }

                           //Same for recipes 9-14 just too many chars for so

                            }
                            if( snapshot.getKey().equals("recipe_15")) {
                                //do your stuff
                                recipe_15 = snapshot.getValue( Recipe.class );
                                ArrayList<String> amountIngridients = new ArrayList<>(  );
                                ArrayList<String> ingridients = new ArrayList<>(  );

                                String[] resultAmount = new String[15];
                                resultAmount = Arrays.copyOf(recipe_15.getAmountOfIngredients().values().toArray(),recipe_15.getAmountOfIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( amountIngridients,resultAmount );

                                String[] resultIngredients = new String[15];
                                resultIngredients = Arrays.copyOf(recipe_15.getIngredients().values().toArray(),recipe_15.getIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( ingridients,resultAmount );

                                for (int i = 0; i < resultAmount.length;i++) {
                                    ingridients15.add( i,resultAmount[i]+resultIngredients[i]);
                                }
                                Arrays.fill( resultAmount,null );
                                Arrays.fill( resultIngredients,null );

                            }
                            if( snapshot.getKey().equals("recipe_16")) {
                                //do your stuff
                                recipe_16 = snapshot.getValue( Recipe.class );
                                ArrayList<String> amountIngridients = new ArrayList<>(  );
                                ArrayList<String> ingridients = new ArrayList<>(  );

                                String[] resultAmount = new String[15];
                                resultAmount = Arrays.copyOf(recipe_16.getAmountOfIngredients().values().toArray(),recipe_16.getAmountOfIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( amountIngridients,resultAmount );

                                String[] resultIngredients = new String[15];
                                resultIngredients = Arrays.copyOf(recipe_16.getIngredients().values().toArray(),recipe_16.getIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( ingridients,resultAmount );

                                for (int i = 0; i < resultAmount.length;i++) {
                                    ingridients16.add( i,resultAmount[i]+resultIngredients[i]);
                                }
                                Arrays.fill( resultAmount,null );
                                Arrays.fill( resultIngredients,null );

                            }
                            if( snapshot.getKey().equals("recipe_17")) {
                                //do your stuff
                                recipe_17 = snapshot.getValue( Recipe.class );
                                ArrayList<String> amountIngridients = new ArrayList<>(  );
                                ArrayList<String> ingridients = new ArrayList<>(  );

                                String[] resultAmount = new String[15];
                                resultAmount = Arrays.copyOf(recipe_17.getAmountOfIngredients().values().toArray(),recipe_17.getAmountOfIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( amountIngridients,resultAmount );

                                String[] resultIngredients = new String[15];
                                resultIngredients = Arrays.copyOf(recipe_17.getIngredients().values().toArray(),recipe_17.getIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( ingridients,resultAmount );

                                for (int i = 0; i < resultAmount.length;i++) {
                                    ingridients17.add( i,resultAmount[i]+resultIngredients[i]);
                                }
                                Arrays.fill( resultAmount,null );
                                Arrays.fill( resultIngredients,null );

                            }
                            if( snapshot.getKey().equals("recipe_18")) {
                                //do your stuff
                                recipe_18 = snapshot.getValue( Recipe.class );
                                ArrayList<String> amountIngridients = new ArrayList<>(  );
                                ArrayList<String> ingridients = new ArrayList<>(  );

                                String[] resultAmount = new String[15];
                                resultAmount = Arrays.copyOf(recipe_18.getAmountOfIngredients().values().toArray(),recipe_18.getAmountOfIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( amountIngridients,resultAmount );

                                String[] resultIngredients = new String[15];
                                resultIngredients = Arrays.copyOf(recipe_18.getIngredients().values().toArray(),recipe_18.getIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( ingridients,resultAmount );

                                for (int i = 0; i < resultAmount.length;i++) {
                                    ingridients18.add( i,resultAmount[i]+resultIngredients[i]);
                                }
                                Arrays.fill( resultAmount,null );
                                Arrays.fill( resultIngredients,null );

                            }
                            if( snapshot.getKey().equals("recipe_19")) {
                                //do your stuff
                                recipe_19 = snapshot.getValue( Recipe.class );
                                ArrayList<String> amountIngridients = new ArrayList<>(  );
                                ArrayList<String> ingridients = new ArrayList<>(  );

                                String[] resultAmount = new String[15];
                                resultAmount = Arrays.copyOf(recipe_19.getAmountOfIngredients().values().toArray(),recipe_19.getAmountOfIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( amountIngridients,resultAmount );

                                String[] resultIngredients = new String[15];
                                resultIngredients = Arrays.copyOf(recipe_19.getIngredients().values().toArray(),recipe_19.getIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( ingridients,resultAmount );

                                for (int i = 0; i < resultAmount.length;i++) {
                                    ingridients19.add( i,resultAmount[i]+resultIngredients[i]);
                                }
                                Arrays.fill( resultAmount,null );
                                Arrays.fill( resultIngredients,null );

                            }
                            if( snapshot.getKey().equals("recipe_20")) {
                                //do your stuff
                                recipe_20 = snapshot.getValue( Recipe.class );
                                ArrayList<String> amountIngridients = new ArrayList<>(  );
                                ArrayList<String> ingridients = new ArrayList<>(  );

                                String[] resultAmount = new String[15];
                                resultAmount = Arrays.copyOf(recipe_20.getAmountOfIngredients().values().toArray(),recipe_20.getAmountOfIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( amountIngridients,resultAmount );

                                String[] resultIngredients = new String[15];
                                resultIngredients = Arrays.copyOf(recipe_20.getIngredients().values().toArray(),recipe_20.getIngredients().values().toArray().length,String[].class  );
                                Collections.addAll( ingridients,resultAmount );

                                for (int i = 0; i < resultAmount.length;i++) {
                                    ingridients20.add( i,resultAmount[i]+resultIngredients[i]);
                                }
                                Arrays.fill( resultAmount,null );
                                Arrays.fill( resultIngredients,null );

                            }

                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                } );

    }
}

如果有人有什么主意,为什么会发生这种情况应该很棒,我们多年来一直在研究这个问题,而且似乎无法解决。预先感谢!

1 个答案:

答案 0 :(得分:1)

看起来您的代码从不调用initIngredients()来创建数据库回调所需的所有ArrayList对象。它们最初都是空的,尝试向其中之一添加内容会导致该错误。