Dan Erv
Dan Erv

Reputation: 5

Error with Generics in Java constructor cannot be applied to given types

I am working on homework so I just want to fix my compile error so I can keep working on it. I need to make a PointList class that keeps a list of Point objects in an ArrayList. The PointList class should accept any object that is an instance of the Point class, or a sub-class of Point.

I keep getting a compiler error that says

> required: Point<Integer>
  found: no arguments
  reason: actual and formal argument lists differ in length
  where E is a type-variable:
  E extends Object declared in class PointList

I really don't understand what I am missing I've gone through the book and can't seem to see why I get this error. I have Point class made and test done but can't seem to get it to compile.

public class PointListTest{

public static void main(String[] args){

//create point ints
Point<Integer> pointOne = new Point<Integer>(1,2);
Point<Integer> pointTwo = new Point<Integer>(3,4);
Point<Integer> pointThree = new Point<Integer>(5,6);

//make object PointList pointlist for int
PointList<Point<Integer>> newPointList = new PointList<Point<Integer>>();

//add points to list
newPointList.add(pointOne);
newPointList.add(pointTwo);
newPointList.add(pointThree);

This is what I have so far for PointList

public class PointList<E>{

   private ArrayList<Point> myPoint;

   E data;

   public PointList(E obj){

      ArrayList<Point> myPoint = new ArrayList<Point>();

      data = obj;

   }

}

Point class

public class Point<T extends Number>{

//field values
private T xCordinate;
private T yCordinate;

//constructor
//@param x the x cordinate
//@param y the y cordinate
public Point(T x, T y){
   xCordinate = x;
   yCordinate = y;
}//end constructor

public void setX(T x){

   xCordinate = x;

}//end setX

public void setY(T y){

   yCordinate = y;

}//end setY

public T getX(){

   return xCordinate;

}//end getX

public T getY(){

   return yCordinate;

}//end getY

}//end pointlist

If I could just get it to compile so that I could keep working on it I'd be extremely grateful.

Upvotes: 0

Views: 567

Answers (2)

arjayosma
arjayosma

Reputation: 550

For PointList

// Take advantage of inheritance
public class PointList<E> extends ArrayList<E> {
   // This object will now have the methods
}

Usage

// If you're using Java 8 onwards, you may not need to specify the type upon instantiation
PointList<Point<Integer>> pointList = new PointList<>();
pointList.add(new Point<Integer>(x, y));

If you want to use your own PointList

public class PointList<E> {
   private List<E> dataList;

   public PointList() {
      dataList = new ArrayList<>();
   }

   public void add(E obj) {
      dataList.add(obj);
   }
}

As mentioned by one of the comments, there seems to be a mismatch with your constructor and with your object creation.

Upvotes: 0

ilinykhma
ilinykhma

Reputation: 990

Your PointList has the only constructor with one argument:

public PointList(E obj){

      ArrayList<Point> myPoint = new ArrayList<Point>();

      data = obj;

}

But you try to call constructor with zero arguments:

PointList<Point<Integer>> newPointList = new PointList<Point<Integer>>();

So, compiler can't find the corresponding one. See Providing Constructors for Your Classes tutorial and Constructor overloading in Java.

Upvotes: 1

Related Questions