I created a singleton class that is a list of Boat. I am creating an option for MainActivity's Action Bar, where it will show the information about the boat based on user input for the registration of the boat. If it finds the registration in the list, it should append all the information about that particular boat's registration on TextView. However, I am keep getting my error message instead. I think the cause is somewhere in my for loop statement or its body, but I am not quite sure. This is what my code looks like (I will also provide the list, BoatList aka my singleton class and the regular class Boat below my main code):
public void showBoat(View view)
{
//Declare references and variables
EditText et;
TextView tv;
ImageView im;
BoatList boat_list;
String boatRegist;
//Set references
et = (EditText) findViewById(R.id.edit_boat_regist);
tv = (TextView) findViewById(R.id.text_main);
im = (ImageView) findViewById(R.id.image_area);
boat_list = BoatList.getInstance();
//Get user input
boatRegist = et.getText().toString();
//Search through the list and try to find the boat by registration
int i;
for(i = 0; i < boat_list.size(); i++)
{
if(boatRegist == boat_list.get(i).getRegi())
{
tv.append(boat_list.get(i).getMake() + "\n" + boat_list.get(i).getRegi() +
"\n" + boat_list.get(i).getYear() + "\n" + boat_list.get(i).getLength()
+ "\n" + boat_list.get(i).getBeam() + "\n" + boat_list.get(i).getFuel() +
"\n" + boat_list.get(i).getPrice() + "\n" + boat_list.get(i).getAge()
+ "\n");
}
else
Toast.makeText(ShowBoatActivity.this, "Error: The registration is invalid!",
Toast.LENGTH_SHORT).show();
}
//Open the picture from the URL address
/*
ImageDownloader idl = new ImageDownloader();
idl.download(boat_list.get(i).getPicURL(), im);*/
}
Below is my Singleton class:
import java.util.LinkedList;
public final class BoatList extends LinkedList<Boat>
{
private static BoatList instance = null;
private BoatList()
{
// Exists only to defeat additional instantiations.
}
//Define method
public static BoatList getInstance()
{
if(instance == null)
instance = new BoatList();
return instance;
}
} //end BoatList
Finally, here is my regular class, Boat:
public class Boat {
//Declare data members
private String make;
private String registeration;
private int year;
private int length;
private int beam;
private String fuel;
private double price;
private String picURL;
private int age;
private double priceLT;
//Define the methods
public String getMake()
{
return make;
}
public void setMake(String m)
{
make = m;
}
public String getRegi()
{
return registeration;
}
public void setRegi(String r)
{
registeration = r;
}
public int getYear()
{
return year;
}
public void setYear(int y)
{
year = y;
}
public int getLength()
{
return length;
}
public void setLength(int L)
{
length = L;
}
public int getBeam()
{
return beam;
}
public void setBeam(int b)
{
beam = b;
}
public String getFuel()
{
return fuel;
}
public void setFuel(String f)
{
fuel = f;
}
public double getPrice()
{
return price;
}
public void setPrice(double p)
{
price = p;
}
public String getPicURL()
{
return picURL;
}
public void setPicURL(String url)
{
picURL = url;
}
public int getAge()
{
int currentYear = 2015;
//Calculate the age of the boat
age = currentYear - year;
return age;
}
public double getLuxuryTax()
{
//Declare variables
double newPrice;
double luxTax = .15;
//Calculate the price with luxury tax if the first condition
//is found to be True.
if (length >= 50 && age <= 10)
{
newPrice = price * luxTax;
priceLT = price + newPrice;
if (priceLT == 0)
{
priceLT = (Double) null;
return priceLT;
}
}
return priceLT;
}
}