I'm working on a project in android studio that has a list with an array adapter. If i click on a list item you have to be send to the workshop page that show all the info for the workshop. I did this using a setOnItemClickListener but somehow it is not working. its throwing this error
Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference at com.example.jmuts.tikcitv2.scedule$1.onItemClick(scedule.java:68)*
I have no idea how to fix this, so im stuck now. i hope someone can help me.
Schedule.java (This is the code that has the Click event)
public class scedule extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
ListView lv;
ArrayAdapter<String> adapter;
String[] name, beginning, end;
String eventName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scedule);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
lv = findViewById(R.id.listView);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
eventName = bundle.getString("eventname");
}
listner(eventName);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(scedule.this, workshop.class);
intent.putExtra("eventname", lv.getItemAtPosition(position).toString());
startActivity(intent);
}
});
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_Change) {
Intent c = new Intent(scedule.this, MainActivity.class);
c.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(c);
finish();
} else if (id == R.id.nav_home) {
finish();
} else if (id == R.id.nav_scedule) {
//Intent intent = new Intent(scedule.this, scedule.class);
//intent.putExtra("eventname", eventName);
//startActivity(intent);
} else if (id == R.id.nav_map) {
Intent intent = new Intent(scedule.this, map.class);
intent.putExtra("eventname", eventName);
startActivity(intent);
finish();
} else if (id == R.id.nav_faq) {
Intent intent = new Intent(scedule.this, faq.class);
intent.putExtra("eventname", eventName);
startActivity(intent);
finish();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void listner(String item){
String getData = null;
String dbResult = "empty";
dbConnect database = new dbConnect(this);
try {
Date c = Calendar.getInstance().getTime();
System.out.println("Current time => " + c);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = df.format(c);
String query = "SELECT c.workshopname, b.starttime, b.endtime FROM events as a inner join scedules as b on a.eventid = b.eventid inner join workshops as c on b.workshopid = c.workshopid where b.workshopid = c.workshopid and b.eventid = (select eventid from events where eventname = '" + item + "') order by b.starttime;";
getData = "?query=" + URLEncoder.encode(query, "UTF-8");
//data += "&username=" + URLEncoder.encode(userName, "UTF-8");
String link = "https://jackstest.000webhostapp.com/androiddbconnect.php" + getData;
dbResult = database.execute(link).get();
}
catch (Exception e){
e.printStackTrace();
}
try{
JSONArray ja = new JSONArray(dbResult);
JSONObject jo = null;
name = new String[ja.length()];
beginning = new String[ja.length()];
end = new String[ja.length()];
for(int i = 0; i < ja.length(); i++){
jo = ja.getJSONObject(i);
name[i] = jo.getString("workshopname");
beginning[i] = "Start: " + jo.getString("starttime");
end[i] = "end: " + jo.getString("endtime");
}
lv.setAdapter(new dataListAdapter(name, beginning, end));
}
catch (Exception e){
e.printStackTrace();
}
}
class dataListAdapter extends BaseAdapter {
String[] name, beginning, end;
dataListAdapter() {
name = null;
beginning = null;
end = null;
}
public dataListAdapter(String[] text, String[] text1,String[] text3) {
name = text;
beginning = text1;
end = text3;
}
public int getCount() {
// TODO Auto-generated method stub
return name.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.list_view_scedule_layout, parent, false);
TextView t1, t2, t3;
t1 = (TextView) row.findViewById(R.id.list_item1);
t2 = (TextView) row.findViewById(R.id.list_item2);
t3 = (TextView) row.findViewById(R.id.list_item3);
t1.setText(name[position]);
t2.setText(beginning[position]);
t3.setText(end[position]);
return (row);
}
}
}
Workshop.java (This is the activity that has to show the data)
public class workshop extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
String eventName;
TextView Header;
TextView Content;
TextView Time;
TextView Date;
TextView Location;
ImageView View;
ArrayAdapter<String> adapter;
String[] data;
String [] image;
private Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_workshop);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Header = (TextView) findViewById(R.id.header);
Content = (TextView) findViewById(R.id.content);
Time = (TextView) findViewById(R.id.start_end_time);
Date = (TextView) findViewById(R.id.date);
Location = (TextView) findViewById(R.id.location);
View = (ImageView) findViewById(R.id.imageView3);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
eventName = bundle.getString("eventname");
}
GetInfo(eventName);
}
private void GetInfo(String eventName) {
String GetInfo = null;
String dbResult = "empty";
dbConnect database = new dbConnect(this);
try{
String query = "";
GetInfo = "?query=" + URLEncoder.encode(query, "UTF-8");
String link = "https://jackstest.000webhostapp.com/androiddbconnect.php" + GetInfo;
dbResult = database.execute(link).get();
}
catch(Exception e){
Header.setText("Workshop");
}
try{
JSONArray ja = new JSONArray(dbResult);
JSONObject jo = null;
data = new String[ja.length()];
image = new String[ja.length()];
for (int i = 0;i < 1 ; i++){
jo = ja.getJSONObject(i);
data[i] = jo.getString("workshopname");
image[i] = jo.getString("imagelink");
}
bitmap = getBitmapFromURL(image[0]);
View.setImageBitmap(bitmap);
Header.setText(data[0]);
}
catch(Exception e){
e.printStackTrace();
}
}
private Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_Change) {
Intent c = new Intent(workshop.this, MainActivity.class);
c.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(c);
finish();
} else if (id == R.id.nav_home) {
finish();
} else if (id == R.id.nav_scedule) {
Intent intent = new Intent(workshop.this, scedule.class);
intent.putExtra("eventname", eventName);
startActivity(intent);
finish();
} else if (id == R.id.nav_map) {
Intent intent = new Intent(workshop.this, map.class);
intent.putExtra("eventname", eventName);
startActivity(intent);
finish();
} else if (id == R.id.nav_faq) {
Intent intent = new Intent(workshop.this, faq.class);
intent.putExtra("eventname", eventName);
startActivity(intent);
finish();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}