家計簿アプリをAndroid Studioで作成しています。
目的は、ShopFragmentで作成したShop画面から店名で、SELECT文を発行する事です。
budgetTrackerViewModel以下の下位階層(SQL文含め)は出来ており、まずはShopFragmentからbudgetTrackerViewModelへユーザ入力値を渡すところまで行いたいです。
DBはAndroid Room(SQLite)を使用しており、下記の通りShopFragment.javaでユーザ入力を受け取り、
受け取った入力文字列を下位階層のBudgetTrackerViewModel.javaへ渡すところまでをまずは行いたいのですが、
下記NullPointerエラーが発生してしまいます。
エラーを読み込むと、ShopFragmentの81行目のbudgetTrackerViewModel.getStoreNameLists(storeName);でViewModelクラスのgetStoreNameListsメソッドの呼び出しでNullPointerExceptionが発生していました。
ShopFragmentでユーザ入力値を受け取り、受け取った値をString storeName = enterStoreNameForQuery.getText().toString();でstoreName に代入し、budgetTrackerViewModel.getStoreNameLists(storeName);でbudgetTrackerViewModelのgetStoreNameListsメソッドを呼び出しているのですが、ここでエラーが発生しており、何故NullPointerExceptionエラーが発生するのか、原因が掴めておりません。
ご助力頂けますと幸いです。
エラー内容
2021-12-16 16:39:45.420 30171-30171/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.myproject.offlinebudgettrackerappproject, PID: 30171 java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.myproject.offlinebudgettrackerappproject.model.BudgetTrackerViewModel.getStoreNameLists(java.lang.String)' on a null object reference at com.myproject.offlinebudgettrackerappproject.ShopFragment$1.onClick(ShopFragment.java:86) at android.view.View.performClick(View.java:7259) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) at android.view.View.performClickInternal(View.java:7236) at android.view.View.access$3600(View.java:801) at android.view.View$PerformClick.run(View.java:27892) 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)
ShopFragment.java
package com.myproject.offlinebudgettrackerappproject; import android.app.Application; import android.os.Bundle; import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import com.myproject.offlinebudgettrackerappproject.model.BudgetTracker; import com.myproject.offlinebudgettrackerappproject.model.BudgetTrackerViewModel; import java.util.List; /** * A simple {@link Fragment} subclass. * Use the {@link ShopFragment#newInstance} factory method to * create an instance of this fragment. */ public class ShopFragment extends Fragment { BudgetTrackerViewModel budgetTrackerViewModel; // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; public ShopFragment() { // Required empty public constructor } /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @param param1 Parameter 1. * @param param2 Parameter 2. * @return A new instance of fragment ShopFragment. */ // TODO: Rename and change types and number of parameters public static ShopFragment newInstance(String param1, String param2) { ShopFragment fragment = new ShopFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_shop, container, false); EditText enterStoreNameForQuery = (EditText) view.findViewById(R.id.shop_search); Button storeSearchQueryBtn = (Button) view.findViewById(R.id.btn_shop_search); storeSearchQueryBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { BudgetTracker budgetTracker; String storeName = enterStoreNameForQuery.getText().toString(); budgetTrackerViewModel = new ViewModelProvider.AndroidViewModelFactory(ShopFragment.this .getApplication()) .create(BudgetTrackerViewModel.class); budgetTracker = new BudgetTracker(); budgetTracker.setStoreName(storeName); budgetTrackerViewModel.getStoreNameLists(storeName); Log.d("TAG", "onClick: " + enterStoreNameForQuery.getText().toString()); // Log.d("TAG", "onClick: " + list); } }); return view; } }
BudgetTrackerViewModel.java
package com.myproject.offlinebudgettrackerappproject.model; import android.app.Application; import androidx.annotation.NonNull; import androidx.lifecycle.AndroidViewModel; import androidx.lifecycle.LiveData; import com.myproject.offlinebudgettrackerappproject.data.BudgetTrackerRepository; import java.util.List; public class BudgetTrackerViewModel extends AndroidViewModel { public static BudgetTrackerRepository repository; public final LiveData<List<BudgetTracker>> allBudgetTrackerLists; public List<BudgetTracker> storeNameLists; public BudgetTrackerViewModel(@NonNull Application application) { super(application); repository = new BudgetTrackerRepository(application); allBudgetTrackerLists = repository.getAllBudgetTrackerData(); } public LiveData<List<BudgetTracker>> getAllBudgetTrackerLists() { return allBudgetTrackerLists; } public List<BudgetTracker> getStoreNameLists(String storeName) { storeNameLists = repository.queryStoreName(storeName); return storeNameLists; } public static void insert(BudgetTracker budgetTracker) { repository.insert(budgetTracker); } }
あなたの回答
tips
プレビュー