Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Can't instantiate custom class

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Can't instantiate custom class

    I have a class CollegeCourse:

    public class CollegeCourse
    {
    	 private String courseId;
    	 private int creditHours;
    	 private char grade;
     
    	public CollegeCourse(String i, int h, char g)
    	{
    		this.courseId = i;
    		this.creditHours = h;
    		this.grade = g;
    	}
     
    	public String courseId(){return this.courseId;}
    	public int creditHours(){return this.creditHours;}
    	public char grade(){return this.grade;}
     
    	public void setcourseId(String i){this.courseId = i;}
    	public void setcreditHours(int h){this.creditHours = h;}
    	public void setgrade(char g){this.grade = g;}
     
    }

    Then I have the second class Student:

    public class Student 
    {
    	private int studentId;
    	private CollegeCourse[] courseArray = new CollegeCourse[5];
     
     
    	public Student(int s, CollegeCourse[] c)
    	{
    		this.studentId = s;
    		this.courseArray = c;	
    	}
     
    	public int studentId(){return this.studentId;}
    	public CollegeCourse[] courseArray(){return this.courseArray;}
     
    	public void setstudentId(int s){this.studentId = s;}
    	public void setcourseArray(CollegeCourse[] c){this.courseArray = c;}
    }

    In my tester class I am trying to instantiate the Student class like:
    Student stu = new Student();

    The compiler complains:
    InputGrades.java:8: error: constructor Student in class Student cannot be applied to given types;
    	 Student stu = new Student(12, new CollegeCourse("CSI 2010", 4, 'A'));
    	                    ^

    Also when I want to take input can I use a setter like this?:
    stu.courseId = console.next();


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Can't instantiate custom class

    You have defined a constructor for Student that takes 2 parameters as an argument, if you wish to have an empty argument constructor - which is how you are trying to instantiate the class - explicitly define this constructor in your class, or use the two argument constructor.

  3. #3
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Can't instantiate custom class

    I am obviously still wrong because of my compiler error, am I any closer. I am trying to understand this conceptually, where am I wrong and
    why?

    tester class:

    import java.util.*;
     
     
    public class InputGrades
    {
    	public static void main(String[] args)
    	{
    		Scanner console = new Scanner(System.in);
     
    		int sId, hours;
    		String cId;
    		char letterGrade;
     
    		System.out.println("Please enter the student id: ");		
    		sId = console.nextInt();
     
    		System.out.println("Enter the course id: ");
    		cId = console.nextLine();
     
    		System.out.println("Enter the credit hours: ");
    		hours = console.nextInt();
     
    		System.out.println("Enter the letter grade: ");
    		letterGrade = console.next().charAt(0);
     
    		Student stu = new Student(sId, new CollegeCourse(cId, hours, letterGrade));
     
     
     
    	}
    }

    Thanks!

  4. #4
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Can't instantiate custom class

    I forgot here is the compiler error:
    InputGrades.java:26: error: constructor Student in class Student cannot be applied to given types;
    		Student stu = new Student(sId, new CollegeCourse(cId, hours, letterGrade));
    		              ^
      required: int,CollegeCourse[]
      found: int,CollegeCourse
      reason: actual argument CollegeCourse cannot be converted to CollegeCourse[] by method invocation conversion
    1 error

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Can't instantiate custom class

    Have a look at the expected data types and the found data types, they must match:

    Expected:
    int
    CollegeCourse[]

    Found:
    int
    CollegeCourse


    Error, reason
    "reason: actual argument CollegeCourse cannot be converted to CollegeCourse[] by method invocation conversion"


    Have a look at Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

  6. #6
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't instantiate custom class

    Building on what Tsarin said above, when you instantiate your student object in your tester class, you need to give it arguments that match the types it it expecting. As Tsarin said, the student 2 argument constructor is expecting an INT for the first argument and an ARRAY of college courses as the second argument. You are attempting to pass a single college course as your second argument when it should be an array of college courses.

Similar Threads

  1. Custom Log level help
    By seanman in forum Java SE APIs
    Replies: 1
    Last Post: September 25th, 2011, 08:55 PM
  2. Is it possible to instantiate an interafce ?
    By Learner in forum Object Oriented Programming
    Replies: 7
    Last Post: September 2nd, 2011, 12:10 PM
  3. Could not instantiate bean class
    By Sheval in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 21st, 2011, 03:40 PM
  4. [SOLVED] instantiate problems
    By jamal in forum Object Oriented Programming
    Replies: 9
    Last Post: September 13th, 2010, 06:08 PM
  5. Custom Java stack class (with generics) problem
    By TBBucs in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 7th, 2010, 02:25 AM