aspose file tools*
The moose likes Beginning Java and the fly likes How can I solve IllegalArgumentException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How can I solve IllegalArgumentException " Watch "How can I solve IllegalArgumentException " New topic
Author

How can I solve IllegalArgumentException

Elahe Shafie
Ranch Hand

Joined: Dec 12, 2001
Posts: 291
I am facing this error
"String: java.lang.IllegalArgumentException"
and don't know how can I solve it?any advice or hint would be appriciated.
Many thanks,
Elahe


Thanks,
Elahe
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
On what line number is the exception being thrown? Can you please lable that in your source code?


Rob
SCJP 1.4
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
...and then, assuming it comes from one of the stmt.setInt(), setString(), setDate(), etc. methods - look up the declared type of that column in the database, and compare it to the type you're asking for. Use this table to see if the method you're using makes sense, and let us know what you come up with.


"I'm not back." - Bill Harding, Twister
Elahe Shafie
Ranch Hand

Joined: Dec 12, 2001
Posts: 291
DearRob and Jim,
Thanks for your input the problem was just format of date and I have to enter fixedDate like this format 2002/2/2 instead of 2002-2-2 (in table fixedDate is date type)
any way to solve this problem I like to show user fixedDate with this format 2002/2/2 and here is my part of code but it gives me this error:
Any advice?
Many thanks,
Elahe
Incompatible type for =. Can't convert String to Date.
dDate = formatter.format(dDate);
^
1 error
----Code-----
java.sql.Date dDate= null;
while (rs.next()) {
C_BugList xobj=new C_BugList();
xobj.setName(rs.getString("name")); SimpleDateFormat formatter = new SimpleDateFormat("yy/M/d");
dDate = rs.getDate("fixedDate");
dDate = formatter.format(dDate);
xobj.setFixedDate(dDate);
v.addElement(xobj);
}
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Elahe: just a reminder that java.util.Date and java.sql.Dates do not have formatting. They are actually a long value that counts the milliseconds from a point in time. This being said, there are a couple of problems with this line of code:
since dDate is a java.sql.Date, your argument for SimpleDateFormat.format( java.util.Date ) of the wrong type. To fix this, convert dDate to a java.util.Date before you format it to a String. The second part that is wrong is that SimpleDateFormat.format( java.util.Date ) method returns a String object. You have a java.sql.Date accepting the String as a return value. To correct this part, create a String to hold the formatted date on the return of the method:

but I think the problem is in your concept of a date. A date object has no format, it is only a number. When you need to display a date, you create a visual representation ( String representation ) of the date to display it. You determine the way the date will be visually represented in this string by using a SimpleDateFormat object to parse the date value into the representation you want. This also works in the reverse way. If you have a visual representation of a date ( such as user input ), and you want to convert it to a date object (to write to the database or something), you define how the date is represented in the string using a SimpleDateFormat object, then you use this SimpleDateFormat object to convert your visual representation of the date into an actual date object. Again, I must stress that the date object you just created has no formatting, it is only a number.
Jamie
Elahe Shafie
Ranch Hand

Joined: Dec 12, 2001
Posts: 291
Dear Jamie THANK YOU SO MUCH for you useful information and your absolutly right I hope I can get ride of this task finaly so soon...
Jamie I want to forget use all those and make it simple and read from table and put slash between and show user that's it because I know if I put slash(/) it will work(I hope...)
Just could you please correct me if I am wrong is sintax?
Many thanks,
Elahe
----------
S2=2002-04-09
s2 = (s1.substring(1,4)).concat("/").concat(s1.substring(6,2)).concat("/").concat(s1.substring(9,2));
and then I will update you about the result...
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Jamie I want to forget use all those and make it simple and read from table and put slash between and show user that's it because I know if I put slash(/) ...
----------
S2=2002-04-09
s2 = (s1.substring(1,4)).concat("/").concat(s1.substring(6,2)).concat("/").concat(s1.substring(9,2));
and then I will update you about the result...

OK here's the code set --> if table column is of type Date:

OK here's the code set --> if table column is of type String in the format: "yyyy-MM-dd" ( 2002-04-09 )


good luck, Jamie
Elahe Shafie
Ranch Hand

Joined: Dec 12, 2001
Posts: 291
Jamie I guess I am almost close with your BIG HELP
just I don't know how can I convert "formatted_date_string" to date type since "FixedDate" is date type...and I guess I am done...
Again thanks for your big help,
Elahe
Elahe Shafie
Ranch Hand

Joined: Dec 12, 2001
Posts: 291
Jamie for my answer I used:
java.util.Date myDate=sdf.parse(formatted_date_string);
xobj.setFixedDate(myDate);
in my code to convert but it gives me this error:
Exception java.text.ParseException must be caught, or it must be declared in the throws clause of this method
java.util.Date myDate=sdf.parse
(formatted_date_string);
^
1 error
how can I solve this?
Thanks,
Elahe
Elahe Shafie
Ranch Hand

Joined: Dec 12, 2001
Posts: 291
Sorry for too much forwarded okay I added this catch as well no I don't have any error but it didn't the template to show data...
Any advice?
Thanks,
Elahe
catch( Exception x ) {
formatted_date_string = "-";
}
Elahe Shafie
Ranch Hand

Joined: Dec 12, 2001
Posts: 291
Let me update you when I debug the program it stopped right before this line and catch thrown...
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/M/d");
Any advice?!!!
Elahe
Elahe Shafie
Ranch Hand

Joined: Dec 12, 2001
Posts: 291
Okay Jamie I guess I fixed those part and now "formatted_date_string" has a value like 2002/3/15 but I really don't know how can I convert it to date and put it back since "setSubmittedDate" is date type I need to convet would you please help me on this part?(what a day...whole day I was on this issue..)
Mant thanks,
Elahe
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Originally posted by elahe shafie:
Okay Jamie I guess I fixed those part and now "formatted_date_string" has a value like 2002/3/15 but I really don't know how can I convert it to date and put it back since "setSubmittedDate" is date type I need to convet would you please help me on this part?(what a day...whole day I was on this issue..)

From the above information it seems to me that you don't need to convert this to a string at all. if setSubmittedDate takes a java.util.Date as an input parameter, just skip the conversion of the date to a string:

if you do not use the formatted_date_string for anything, you can just remove that code until you need to display the date in some sort of format

if you need to display the submittedDate in a certain format, then you use the SimpleDateFormat when you need to display it. But as of right now in your code, it looks like you don't need it.
Jamie
[ April 24, 2002: Message edited by: Jamie Robertson ]
Elahe Shafie
Ranch Hand

Joined: Dec 12, 2001
Posts: 291
Dear Jamie I am speechless and odn't know how you figured out my confusion and solved it!!! BIG BIG THANKS.
I am done with the date issue now it is working what wa that!!! my God.
Many many Thanks,
Elahe
Elahe Shafie
Ranch Hand

Joined: Dec 12, 2001
Posts: 291
And thanks every body.
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

bet you won't get stuck on that one again! I'm glad you solved it, and learned how to avoid it in the future! You'll have to help the next person with this problem
Elahe Shafie
Ranch Hand

Joined: Dec 12, 2001
Posts: 291
Sure Jamie I will.We have to help each other...
Have great evening,
Elahe
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I solve IllegalArgumentException