Results 1 to 10 of 10
- 12-15-2011, 08:23 AM #1
Member
- Join Date
- Dec 2011
- Location
- Southern Wisconsin
- Posts
- 8
- Rep Power
- 0
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:
The Code for the second file named TestScores.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 = ""; } } } }
Any help on why I'm getting this error would be much appreciated.Java Code:import java.util.*; public class TestScores { public Object[] ia; public void TestScores(Object[] x) { ia = x; } }
- 12-15-2011, 08:39 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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.
- 12-15-2011, 09:23 AM #3
Member
- Join Date
- Dec 2011
- Location
- Southern Wisconsin
- Posts
- 8
- Rep Power
- 0
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:
Is this the part that's wrong or did I not understand what you were trying to tell me?Java Code:else if(choice == 2) { userInput = ""; Object[] ia = new Object[scores.size()]; scores.toArray(ia); TestScores myScores = new TestScores(ia); }
Thanks for your help
- 12-15-2011, 09:33 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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:
Post the entire compiler message if there is one and I'll go and find a computer with a compiler.Java Code:Object[] ia = scores.toArray(); TestScores myScores = new TestScores(ia);
- 12-15-2011, 09:45 AM #5
Member
- Join Date
- Dec 2011
- Location
- Southern Wisconsin
- Posts
- 8
- Rep Power
- 0
Re: Error Passing Object through a constructor
- 12-15-2011, 09:49 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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?
- 12-15-2011, 09:57 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: Error Passing Object through a constructor
Arrgh!
That "void" shouldn't be there. It's a constructor!Java Code:import java.util.*; public class TestScores { public Object[] ia; public void TestScores(Object[] x) { ia = x; } }
- 12-15-2011, 09:58 AM #8
Member
- Join Date
- Dec 2011
- Location
- Southern Wisconsin
- Posts
- 8
- Rep Power
- 0
Re: Error Passing Object through a constructor
Didn't need this post and didn't want to clutter solution
Last edited by mpankhurst; 12-15-2011 at 10:06 AM.
- 12-15-2011, 10:03 AM #9
Member
- Join Date
- Dec 2011
- Location
- Southern Wisconsin
- Posts
- 8
- Rep Power
- 0
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.
- 12-15-2011, 10:12 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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
-
Getting the calling object in a constructor?
By Koala in forum New To JavaReplies: 8Last Post: 10-31-2011, 03:27 AM -
Create an object without calling the constructor?
By sublixt in forum New To JavaReplies: 7Last Post: 10-28-2011, 12:49 PM -
Passing SWT object to another class
By Vinaya Lal Shrestha in forum SWT / JFaceReplies: 0Last Post: 04-03-2009, 01:50 PM -
Passing a Vector object to a function
By evapisces in forum New To JavaReplies: 4Last Post: 09-27-2008, 03:18 AM -
Passing a new object to a constructor please clarifiy the concept for me please.
By stefan24 in forum New To JavaReplies: 2Last Post: 07-09-2007, 05:01 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks