Reputation: 333
I'm getting a cast error (cannot cast from string to station), how could I overcome this, as I will need you use startStation as a parameter in other methods:
Station startStation;
startStation = (Station)(view.getStartStation());
Here is the Station
class:
public class Station {
// The name of the station.
private String name;
public Station(String name) {
if (name == null) {
throw new NullPointerException(
"The name of a station may not be null");
}
this.name = name;
}
Here is my getStartStation()
method:
public String getStartStation() {
return startStation.getText();
}
Upvotes: 0
Views: 83
Reputation: 11117
Just create a Station
object by using the existing constructor:
Station startStation = new Station(view.getStartStation());
Upvotes: 5
Reputation: 69339
Your latest edit shows that your class already has a suitable constructor. Change your code to:
Station startStation;
startStation = new Station(view.getStartStation());
or, more simply,
Station startStation = new Station(view.getStartStation());
Upvotes: 0
Reputation: 15860
cannot cast from string to station
The result of that method is a string
data type. Try creating a new method to convert the string to Station data type. Station is not accepting the String data type values.
Otherwise it won't cast it.
Upvotes: 0
Reputation: 47290
Or make station take a constructor argument of string, and create a new instance based on the string.
You could also use a factory, for station object creation.
Upvotes: 1