The moose likes Beginning Java and the fly likes Passing values to constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing values to constructor" Watch "Passing values to constructor" New topic

Passing values to constructor

Al Wells
Ranch Hand

Joined: Apr 18, 2005
Posts: 87
I will post all code in a follow on. In the interest of space, I have only
posted portions here. I am trying to pass values from one class if the
button is pressed to another class' constructor. I am clearly not doing
this correctly.

Thanks for your help.


I'll post both class files below but hopefully this text is enough to
figure out what is wrong.

[ May 24, 2005: Message edited by: Gregg Bolinger ]
Al Wells
Ranch Hand

Joined: Apr 18, 2005
Posts: 87
Here is all of the code in both classes.

Here is the second class

[Gregg Bolinger - Edited to fix page width problems.]
[ May 24, 2005: Message edited by: Gregg Bolinger ]
Fahd Shariff
Ranch Hand

Joined: Nov 22, 2002
Posts: 38
The method you are using to pass values to the constructor is fine.

What problems are you experiencing? Are you using the passed in values correctly? Why is your userID and password null?

Maybe you need to do:
String userID = uNameString ;
String password = pWordString ;

Fahd Shariff<br />"Let the code do the talking"
Al Wells
Ranch Hand

Joined: Apr 18, 2005
Posts: 87

The userID and password are "null" because they are not required to
log into the DB. The reason for passing the values is for use in the
query "SELECT FirstName, LastName FROM CUS_USER (the table)
WHERE EmailAddress = 'uNameString' (first string) AND Pasword =
'pWordString'(the second string)";

But, when I compile, I get the following error:
" cannot find symbol
symbol: constructor TestLogin(java.lang.String,java.lang.String)
location: class TestLogin
TestLogin tl = new TestLogin(uNameString, pWordString);
1 error
Al Wells
Ranch Hand

Joined: Apr 18, 2005
Posts: 87
The carrot in the preceding should be under new BTW.
Al Wells
Ranch Hand

Joined: Apr 18, 2005
Posts: 87
Fixed it by playing around... here is how:

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

Some questins and comments.

Why is there a main method in your TestLogin class and why does it extend JFrame?

String uNameString = "null";
String pWordString = "null";

That does not set those variables to null. That sets those variables to a string value that equals a literal string null. You probably meant to do:

String uNameString = null;
String pWordString = null;

Let's look at what you are trying to do but much simpler.

GenRocket - Experts at Building Test Data
Al Wells
Ranch Hand

Joined: Apr 18, 2005
Posts: 87

The simple answer is: I don't know... I just kept trying things until
it worked. Not very scientific to be sure.

I extended JFrame in that class only to create a new screen. I am sure
that is not the right way to go about it which is why I have found
this forum to be so very valuable. I have gotten more to work in the
last four days due to you and Ernest's help than I have ever understood
in the past. Please keep the comments coming. What I generally do is
get the thing to just work. That is all I care about. After I do that,
I try some other things (play with the code and see what breaks it and
what makes it work with less code). For instance, I did not even try
to pass these values and create a new object until I got both classes
working all by themselves. Once I tried that, it broke. Then I tried a
whole bunch of things until it worked again. Once you give me feedback,
I make leeps and bounds. It is funny that so many of the things that I am
told after I try the dumb way just seems so obvious later on. This forum
has been outstanding for helping me learn.

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I gotta ask ... do you get a lot of "Al wells that end wells" ??

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Al Wells
Ranch Hand

Joined: Apr 18, 2005
Posts: 87
All through my childhood and still to this day. What is worse is that my
name is short for Alvin. My parents were high when I was born. I wonder if
it is too late to switch to using my middle name...

Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Al Wells:
The carrot in the preceding should be under new BTW.

That typo made my day, I'm still grinning - thanks!

If you want to preserve the formatting of your text, for example for positioning carets, you need to use code tags.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: Passing values to constructor
It's not a secret anymore!