问题
I had the Attempt to invoke virtual method 'java.util.List com.example.demo.Database.Database.getName()' on a null object reference
error in my application and I solved that error using Database database = new Database(this);
code.
Now there is a new error
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.demo, PID: 19678 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.demo/com.example.demo.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3132) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) at android.app.ActivityThread.-wrap17(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:7325) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:162) at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.(SQLiteAssetHelper.java:109) at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.(SQLiteAssetHelper.java:129) at com.example.demo.Database.Database.(Database.java:20) at com.example.demo.MainActivity.(MainActivity.java:28) at java.lang.Class.newInstance(Native Method) at android.app.Instrumentation.newActivity(Instrumentation.java:1096) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3122) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) at android.app.ActivityThread.-wrap17(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:7325) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Here my MainActivity.java code
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
SearchAdapter adapter;
private Context context;
MaterialSearchBar materialSearchBar;
List<String> suggestList = new ArrayList<>();
Database database = new Database(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView)findViewById(R.id.recycler_search);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
materialSearchBar =(MaterialSearchBar)findViewById(R.id.search_bar);
materialSearchBar.setHint("Search");
materialSearchBar.setCardViewElevation(10);
LoadSuggestList();
materialSearchBar.addTextChangeListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
List<String> suggest = new ArrayList<>();
for(String search:suggestList)
{
if(search.toLowerCase().contains(materialSearchBar.getText().toLowerCase()))
suggest.add(search);
}
materialSearchBar.setLastSuggestions(suggest);
}
@Override
public void afterTextChanged(Editable s) {
}
});
materialSearchBar.setOnSearchActionListener(new MaterialSearchBar.OnSearchActionListener(){
@Override
public void onSearchStateChanged(boolean enabled) {
if(!enabled)
{
adapter = new SearchAdapter(getBaseContext(),database.getPharmacy());
recyclerView.setAdapter(adapter);
}
//recyclerView.setAdapter(adapter);
}
@Override
public void onSearchConfirmed(CharSequence text) {
startSearch(text.toString());
}
@Override
public void onButtonClicked(int buttonCode) {
}
});
adapter = new SearchAdapter(this,database.getPharmacy());
recyclerView.setAdapter(adapter);
}
private void startSearch(String text){
adapter = new SearchAdapter(this, database.getPharmacyByCity(text));
recyclerView.setAdapter(adapter);
}
private void LoadSuggestList() {
suggestList = database.getName();
materialSearchBar.setLastSuggestions(suggestList);
}
Here my Database.java code
public class Database extends SQLiteAssetHelper {
private static final String DB_NAME = "pharmacyDemo.db";
private static final int DB_VER = 1;
public Database(Context context) {
super(context, DB_NAME, null, DB_VER);
}
public List<Pharmacy> getPharmacy(){
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qd = new SQLiteQueryBuilder();
String[] sqlSelect ={"ID","Name","City","Phone"};
String tableName = "Pharmacy_List";
qd.setTables(tableName);
Cursor cursor = qd.query(db,sqlSelect,null, null ,null , null , null);
List<Pharmacy> result = new ArrayList<>();
if(cursor.moveToFirst())
{
do{
Pharmacy pharmacy = new Pharmacy();
pharmacy.setID(cursor.getInt(cursor.getColumnIndex("ID")));
pharmacy.setName(cursor.getString(cursor.getColumnIndex("Name")));
pharmacy.setCity(cursor.getString(cursor.getColumnIndex("City")));
pharmacy.setPhone(cursor.getString(cursor.getColumnIndex("Phone")));
result.add(pharmacy);
}
while(cursor.moveToNext());
}
return result;
}
public List<String> getName(){
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qd = new SQLiteQueryBuilder();
String[] sqlSelect ={"Name"};
String tableName = "Pharmacy_List";
qd.setTables(tableName);
Cursor cursor = qd.query(db,sqlSelect,null, null ,null , null , null);
List<String> result = new ArrayList<>();
if(cursor.moveToFirst())
{
do{
result.add(cursor.getString(cursor.getColumnIndex("Name")));
}
while(cursor.moveToNext());
}
return result;
}
public List<Pharmacy> getPharmacyByCity(String city){
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qd = new SQLiteQueryBuilder();
String[] sqlSelect ={"ID","Name","City","Phone"};
String tableName = "Pharmacy_List";
qd.setTables(tableName);
Cursor cursor = qd.query(db,sqlSelect,"City = ?", new String[]{city},null , null , null);
List<Pharmacy> result = new ArrayList<>();
if(cursor.moveToFirst())
{
do{
Pharmacy pharmacy = new Pharmacy();
pharmacy.setID(cursor.getInt(cursor.getColumnIndex("ID")));
pharmacy.setName(cursor.getString(cursor.getColumnIndex("Name")));
pharmacy.setCity(cursor.getString(cursor.getColumnIndex("City")));
pharmacy.setPhone(cursor.getString(cursor.getColumnIndex("Phone")));
result.add(pharmacy);
}
while(cursor.moveToNext());
}
return result;
}
回答1:
Try justify "context" in your Database.java file using context = this;
like this:
private static final String DB_NAME = "pharmacyDemo.db";
private static final int DB_VER = 1;
public Database(Context context) {
super(context, DB_NAME, null, DB_VER);
context = this;
}
来源:https://stackoverflow.com/questions/61300284/attempt-to-invoke-virtual-method-android-content-context-getapplicationinfo