Results 1 to 10 of 10
  1. #1
    mpankhurst is offline Member
    Join Date
    Dec 2011
    Location
    Southern Wisconsin
    Posts
    8
    Rep Power
    0

    Default Error Passing Object through a constructor

    I am doing a small program that passes an object to a class for doing a basic score average. I am getting an error when passing the object through the construction. The error is:

    JavaAssignment4.java:40: cannot find symbol
    symbol : constructor TestScores(java.lang.Object)
    location: class TestScores
    TestScores myScores = new TestScores(ia);

    The Code for the first file named JavaAssignment4.java is:

    Java Code:
    import java.util.*;
    import javax.swing.JOptionPane;
    import java.util.ArrayList;
    
    public class JavaAssignment4
    {
    	public static void main(String[] args)
    	{
    		ArrayList scores = new ArrayList();
    		String userInput = "";
    		int lcv = 0;
    		userInput = "";
    		int choice = 0;
    			while (userInput.length() == 0)
    			{
    				userInput = JOptionPane.showInputDialog("Please Choose one:\n1. Add New Score\n2. Get Average\n3. Exit");			
    				try // validation try/catch
    				{
    					if (userInput.length() == 0)
    					{
    						
    						JOptionPane.showMessageDialog(null, "Don't leave it blank.");
    						userInput = "";
    					}
    				
    					else
    					{
    						choice = Integer.parseInt(userInput);
    						if(choice == 1)
    						{
    							scores.add(JOptionPane.showInputDialog("Enter the Score"));
    							lcv +=1;
    							userInput = "";
    
    						}
    						else if(choice == 2)
    						{
    							userInput = "";
    							Object ia = scores.toArray();
    							TestScores myScores = new TestScores(ia);
    							
    						}
    						else if(choice == 3)
    						{
    							
    						}
    					}
    				}
    				catch(NumberFormatException e)
    				{
    					JOptionPane.showMessageDialog(null, "Please type in either 1 2 3 depending on your choice.");
    					userInput = "";
    				}
    			}
    	}
    }
    The Code for the second file named TestScores.java is:

    Java Code:
    import java.util.*;
    public class TestScores
    {
    	
    	public Object[] ia;
    	public void TestScores(Object[] x)
    	{
    		ia = x;
    		
    	}
    }
    Any help on why I'm getting this error would be much appreciated.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Error Passing Object through a constructor

    Java is very fussy about types.

    You declared ia as Object, but the constructor only accepts Object[] arrays. So the compiler complains when you call the constructor.

    Notice that you actually assigned an array to ia. That's quite legal because you can assign *any* reference type to an Object variable. But when it comes time to call the constructor the compiler won't be happy until you change ia and declare it to be an array.

  3. #3
    mpankhurst is offline Member
    Join Date
    Dec 2011
    Location
    Southern Wisconsin
    Posts
    8
    Rep Power
    0

    Default Re: Error Passing Object through a constructor

    Ok I read your response and I thought I understood it. I did some google work to find the best way to convert an ArrayList to and Object[]. What follows is the code I'm using for choice 2. Unfortunately I'm doing something wrong as this still gets me the same error. Here is my my code for choice 2:

    Java Code:
    else if(choice == 2)
    						{
    							userInput = "";
    							Object[] ia = new Object[scores.size()];
    							scores.toArray(ia);
    							TestScores myScores = new TestScores(ia);
    							
    						}
    Is this the part that's wrong or did I not understand what you were trying to tell me?
    Thanks for your help

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Error Passing Object through a constructor

    The same error? "cannot find symbol
    symbol : constructor TestScores(java.lang.Object)"

    That's weird given how you've declared ai. I was thinking more simply:

    Java Code:
    Object[] ia = scores.toArray();
    TestScores myScores = new TestScores(ia);
    Post the entire compiler message if there is one and I'll go and find a computer with a compiler.

  5. #5
    mpankhurst is offline Member
    Join Date
    Dec 2011
    Location
    Southern Wisconsin
    Posts
    8
    Rep Power
    0

    Default Re: Error Passing Object through a constructor

    I put in your code (think I tried that before but it's what I'm using now) and I got the same error again:

    Error Passing Object through a constructor-errormessage.png

    I've been getting the warnings before I ever made the constructor. I know there from not putting the parsing into a try catch block. That wouldn't mess anything else up would it?

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Error Passing Object through a constructor

    That error message is different... Are you sure you didn"t make any changes to the other class?

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Error Passing Object through a constructor

    Arrgh!

    Java Code:
    import java.util.*;
    public class TestScores
    {
         
        public Object[] ia;
        public void TestScores(Object[] x)
        {
            ia = x;
             
        }
    }
    That "void" shouldn't be there. It's a constructor!

  8. #8
    mpankhurst is offline Member
    Join Date
    Dec 2011
    Location
    Southern Wisconsin
    Posts
    8
    Rep Power
    0

    Default Re: Error Passing Object through a constructor

    Didn't need this post and didn't want to clutter solution
    Attached Thumbnails Attached Thumbnails Error Passing Object through a constructor-errormessage.png  
    Last edited by mpankhurst; 12-15-2011 at 11:06 AM.

  9. #9
    mpankhurst is offline Member
    Join Date
    Dec 2011
    Location
    Southern Wisconsin
    Posts
    8
    Rep Power
    0

    Default Re: Error Passing Object through a constructor

    omg... how did I miss that... hours of googling... dude thank you for being my second set of eyes.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Error Passing Object through a constructor

    You're welcome but I didn't see it - thank Eclipse for being a third set!

    Don't worry (unless you want to) about the warning. Your teacher should have told you to expect that. The code will run fine despite that. (google: java generics or the warning message)

Similar Threads

  1. Getting the calling object in a constructor?
    By Koala in forum New To Java
    Replies: 8
    Last Post: 10-31-2011, 04:27 AM
  2. Create an object without calling the constructor?
    By sublixt in forum New To Java
    Replies: 7
    Last Post: 10-28-2011, 01:49 PM
  3. Passing SWT object to another class
    By Vinaya Lal Shrestha in forum SWT / JFace
    Replies: 0
    Last Post: 04-03-2009, 02:50 PM
  4. Passing a Vector object to a function
    By evapisces in forum New To Java
    Replies: 4
    Last Post: 09-27-2008, 04:18 AM
  5. Replies: 2
    Last Post: 07-09-2007, 06:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •