I have two classes;
- ProgramAdapter.java
- buy_ticket.java
I want to pass data from the ProgramAdapter class to the Buy_Ticket class. I am getting the error below:
"Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference at com.smartsoft.travelmate.buy_ticket.onCreate(buy_ticket.java:35)"
Why am I getting this error and how I can get past this problem? My issue could be from the putExtra method statement or the getString method statement but I can't see the issue.
Error Source: Buy_Ticket.class line 35: String operatorName=getIntent().getExtras().getString("operator");//Get operator name
Error Message Log:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.smartsoft.travelmate, PID: 9643
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smartsoft.travelmate/com.smartsoft.travelmate.buy_ticket}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
ProgramAdapter
public class ProgramAdapter extends ArrayAdapter<String> {
Context context;
int[] images;
String[] operatorName;
String[] busRegistration;
String[] trip;
String[] ticketPrice;
String[] departureDates;
String[] departureTimes;
String fetchedOperator;
public ProgramAdapter(available_buses context, int[] images,String[] operatorName, String[]
busRegistration,String[] trip,String[] ticketPrice,String[] departureDates, String[]
departureTimes) {
super(context, R.layout.bus_operater,R.id.txtOperator,operatorName);
this.context = context;
this.images = images;
this.operatorName = operatorName;
this.busRegistration = busRegistration;
this.trip=trip;
this.ticketPrice = ticketPrice;
this.departureDates = departureDates;
this.departureTimes = departureTimes;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View singleItem=convertView;
ProgramViewHolder holder = null;
if (singleItem==null){
LayoutInflater layoutinflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
singleItem=layoutinflator.inflate(R.layout.bus_operater,parent,false);
holder = new ProgramViewHolder(singleItem);
singleItem.setTag(holder);
}else {
holder = (ProgramViewHolder) singleItem.getTag();
}
holder.itemImage.setImageResource(images [position]);
holder.operatorName.setText(operatorName [position]);
holder.busRegistration.setText("(" + busRegistration [position] + ")");
holder.trip.setText("Trip: " + trip [position]);
holder.departureDates.setText("Departure Date: " + departureDates [position]);
holder.departureTimes.setText("Departure Time: " + departureTimes [position]);
holder.ticketPrice.setText("Ticket Price: " + ticketPrice [position]);
singleItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Toast.makeText(getContext(),"You tapped: " + operatorName [position],Toast.LENGTH_SHORT).show();
Intent i = new Intent(context, buy_ticket.class);
context.startActivity(i);
fetchedOperator=operatorName[position].toString();
//Toast.makeText(getContext(), fetchedOperator, Toast.LENGTH_SHORT).show();//Test code
i.putExtra("operator",fetchedOperator);
}
});
return singleItem;
}
}
buy_ticket
public class buy_ticket extends AppCompatActivity {
Button bBuyTicket;//Button Buy Ticket
TextView tvSTDTicketPrice,tvGrandTicketCost;//Declare UI views/controls
int seatCapacity=60;
double ticketQuantity;//Declare integer variables
double grandTicketsCost,ticketPrice;//Declare float variables
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buy_ticket);
String operatorName=getIntent().getExtras().getString("operator");//Get operator name
TextView tvOperatorName=(TextView)findViewById(R.id.txtOperator);
//tvOperatorName.setText(operatorName);
Spinner sTQuantity = findViewById(R.id.spinTicketQuantity);//Declare ticket quantity object/spinner
List<String> totalPassengers=new ArrayList<>();//Define arraylist object
for (int i=1;i<=seatCapacity;i++)
{
totalPassengers.add(String.valueOf(i));//Populate spinner with integer numbers according to max bus seating capacity
}
ArrayAdapter<String> tickets_adapter;
tickets_adapter= new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, totalPassengers);
tickets_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sTQuantity.setAdapter(tickets_adapter);
bBuyTicket=findViewById(R.id.btnBuyTicket); //Cast button to variable buyTicket
tvSTDTicketPrice=findViewById(R.id.txtSTDTicketPrice);//Cast textview to variable tvSTDTicketPrice
tvGrandTicketCost=findViewById(R.id.txtTotalTicketCost);
sTQuantity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
DecimalFormat numberFormatter=new DecimalFormat("#,###.##");
//Get ticket price from string
String fetchedPrice=tvSTDTicketPrice.getText().toString().substring(4);//Get the standard ticket price
ticketPrice=Double.parseDouble(fetchedPrice);//Convert ticket price to float
ticketQuantity=Double.parseDouble(sTQuantity.getSelectedItem().toString());//Get the desired number of tickets to be purchased
grandTicketsCost=Double.valueOf(ticketPrice*ticketQuantity);//Calculate the total cost of the tickets
tvGrandTicketCost.setText("ZMW " + String.valueOf(numberFormatter.format(grandTicketsCost)));//Display the total cost if the tickets
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
//Get number of tickets required
//Buy Ticket Code
bBuyTicket.setOnClickListener(view -> {
});
}
}