I have to connect neo4j to android studio. I have a recyclerview that must be populated by neo4j. I made the connection and it works for me but the recyclerview is not populated because in debug I saw that the connection driver variable remains null.
public static class AsyncTask_Drink extends AsyncTask<String, String, String> {
static AsyncTask_Drink instance = null;
Connection conn = null;
Driver driver;
@Override
protected String doInBackground(String... strings) {
try {
String uri = "bolt://localhost:7687";
String username = "neo4j";
String password = "neo4j";
driver = GraphDatabase.driver(uri, AuthTokens.basic(username, password));
Session session = driver.session();
Log.e("Client", "Connessione effettuata");
} catch (Exception e) {
e.printStackTrace();
Log.e("Client", "Connessione non effettuata");
}
return null;
}
public Driver getConn() {
return this.driver;
}
public static AsyncTask_Drink getInstance() throws Neo4jException {
if (instance == null) {
instance = new AsyncTask_Drink();
}
return instance;
}
}`
public class DaoDrinkDatabase implements DaoDrink {
public ArrayList<DrinkDomain> getDrinkCocktail() throws Neo4jException, ClassNotFoundException {
ArrayList<DrinkDomain> drinkDomains = new ArrayList<DrinkDomain>();
Driver driver = DrinksFragment.AsyncTask_Drink.getInstance().getConn();
try(Session session = driver.session()) {
Result result = session.run("MATCH (d:drink) -[rel:DI_TIPO]->(t:Tipologia {tipo:\"Cocktail\"}) RETURN d,rel,t");
while (result.hasNext()) {
Record record = result.next();
String name = (record.get("name").asString());
String descriptor = (record.get("descriptor").asString());
Double fee = (record.get("fee").asDouble());
String pic = (record.get("pic").asString());
drinkDomains.add(new DrinkDomain(name, descriptor, fee, pic));
}
}
return drinkDomains;
}
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
DrinksViewModel drinksViewModel =
new ViewModelProvider(this).get(DrinksViewModel.class);
binding = FragmentDrinksBinding.inflate(inflater, container, false);
View root = binding.getRoot();
text_head = root.findViewById(R.id.text_head);
category = root.findViewById(R.id.fragment_drinks_category);
cocktail = root.findViewById(R.id.fragment_drinks_Cocktail);
shake = root.findViewById(R.id.fragment_drinks_Shake);
recyclerViewcategory = root.findViewById(R.id.fragment_drinks_recyclerView_category);
recyclerViewcocktail = root.findViewById(R.id.fragment_drinks_recyclerView_cocktail);
recyclerViewshake = root.findViewById(R.id.fragment_drinks_recyclerView_shake);
FunctionThread f = new FunctionThread("drinks");
Thread t = new Thread(f);
t.start();
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
if(UserSingleton.getLogged()){
text_head.setVisibility(View.INVISIBLE);
category.setVisibility(View.VISIBLE);
cocktail.setVisibility(View.VISIBLE);
shake.setVisibility(View.VISIBLE);
recyclerViewcategory.setVisibility(View.VISIBLE);
recyclerViewcocktail.setVisibility(View.VISIBLE);
recyclerViewshake.setVisibility(View.VISIBLE);
}else{
text_head.setVisibility(View.VISIBLE);
category.setVisibility(View.VISIBLE);
shake.setVisibility(View.INVISIBLE);
cocktail.setVisibility(View.INVISIBLE);
recyclerViewcategory.setVisibility(View.VISIBLE);
recyclerViewcocktail.setVisibility(View.INVISIBLE);
recyclerViewshake.setVisibility(View.INVISIBLE);
}
recyclerViewCategoryList = root.findViewById(R.id.fragment_drinks_recyclerView_category);
recyclerViewCocktailList = root.findViewById(R.id.fragment_drinks_recyclerView_cocktail);
recyclerViewShakeList = root.findViewById(R.id.fragment_drinks_recyclerView_shake);
recyclerViewCategory();
AsyncTask_Drink asyncTask_drink = new AsyncTask_Drink();
asyncTask_drink.execute();
recyclerViewCocktail();
recyclerViewShake();
return root;
}`
Connection made to pass data from neo4j to android with java using recyclerview.
Error:
java.lang.NullPointerException: Attempt to invoke interface method 'org.neo4j.driver.Session org.neo4j.driver.Driver.session()' on a null object reference