• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Unable to create an instance of a class

I'm following( or trying to) a tutorial showing how to work with a listview.
1. Created a new class with getters/setters and contructor with the elements in the database table.

When it came to the view function in the database helper class, the example initialized a list(arraylist) and a new instance of the class created in number 1. For whatever reason, the compiler doesn't like my instance name, it seems to want the elements within the class.

Java:
public List<Car> ViewCar()    //Cursor was the return type
    {
        try
        {
            List<Car> carlist=new ArrayList<Car>(); 
            SQLiteDatabase db=this.getWritableDatabase();
            Cursor result=db.rawQuery("select * from "+CAR_TABLE, null);
            if(result.moveToFirst())
            {
                do{
                    Car car=new Car();      (Car in Car() cannot be applied to: shows all the strings)
                    car.setMake(result.getString(0));
                    car.setModel((result.getString(1)));
                    car.setYear(result.getString(2));
                    car.setMileage(result.getString(3));
                }
                while (result.moveToNext());
            }
            db.close();
            return  carlist;
        }
        catch(Exception e)
        {
            return null;
        }
       
    }


Just for fun, here is the Car class
Java:
public class Car
{
    private String Make, Model, Year, Mileage;

    public Car(String make, String model, String year, String mileage)
    {
        Make = make;
        Model = model;
        Year = year;
        Mileage = mileage;
    }

    public String getMake()
    {
        return Make;
    }

    public void setMake(String make)
    {
        Make = make;
    }

    public String getModel()
    {
        return Model;
    }

    public void setModel(String model)
    {
        Model = model;
    }

    public String getYear()
    {
        return Year;
    }

    public void setYear(String year)
    {
        Year = year;
    }

    public String getMileage()
    {
        return Mileage;
    }

    public void setMileage(String mileage)
    {
        Mileage = mileage;
    }
}

I used the generate for this section. What am I missing? Thanks!
 
I imagine the compiler won't like this line

Code:
Car car=new Car();

You're trying to use a default constructor, which doesn't exist. That's because you've already defined a constructor on that class, taking 4 String parameters.
If you want to make this work, you should explicitly define a no-arg (default) constructor

Code:
class Car {
  public Car() {
  }
}
 
I imagine the compiler won't like this line

Code:
Car car=new Car();

You're trying to use a default constructor, which doesn't exist. That's because you've already defined a constructor on that class, taking 4 String parameters.
If you want to make this work, you should explicitly define a no-arg (default) constructor

Code:
class Car {
  public Car() {
  }
}
Silly question, this needs to go in the car class? I wonder why it worked in the tutorial. It was made last year. So it will grab the setter from here rather than the constructor?
 
Last edited:
Add the default constructor to your class, and the above line will work.
 
Back
Top Bottom