• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Decoding errors

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Is there a book that can be referred to find the meaning of errors such as:

                                      java.lang.IllegalArgumentException: Can not set java.lang.String field EmailMessage.receiver to java.lang.String

Also can anyone please suggest how can one improve on programming?

Many thanks.
 
Bartender
Posts: 5907
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest using printStackTrace() to get, not only the error message, but the file names and line numbers as well.

What is the data type of EmailMessage.receiver? The error message indicates that you are trying to set it to a String and that failed because "receiver" is not a String type.
 
Bartender
Posts: 20842
125
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't shout!

What do you not understand? Java Exceptions? They're covered in any decent textbook on Java. IllegalArgumentException? It's explained in the JavaDocs (https://docs.oracle.com/javase/7/docs/api/java/lang/IllegalArgumentException.html). And Carey's advice is worth noting. If you catch the exception and do a printStackTrace on it, it will provide a lot more information. For that matter, if you don't catch it at all, the app will exit and print a stack trace.
 
Nivesh Chandola
Greenhorn
Posts: 10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Tim,

                                                                                          I won't shout.


There are certain rules that I don't understand, which are defined in Java programming language. Also, my core field is not programming so sometimes it gets difficult to comprehend what the error means.

I understand the concept of Java Exceptions, however, I don't always understand when and where to use them (like in this case).

@Carey

Data type of EmailMessage.receiver is String.

But it is ok, I now got the idea of using printStackTrace(), so let me try it first, if I can't solve it then I will post it here.

Thank you Carey and Tim

 
Nivesh Chandola
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again,

Now I really need help to understand certain concepts.

Basically, I have 4 classes and have saved each of them in a separate file as .java (I use notepad++ for programming):
1. SMSMessage
2. MessageProcessor -----------> this is a Generic Class that shows the output
3. EmailMessage
4. MainClass

Have also done error handling, using printStackTrace() in class MessageProcessor

Imported: import java.lang.reflect.Field; ----> This is being used by class MessageProcessor

Objective of Program:
Ask the user to choose from
                                          1. SMS
                                          2. Email
                                          3. Exit
SMS
      User enters 1. phone number, 2. message

Output to be displayed: message sent to phone_number was submitted for processing at current_time

Error that I received:
       java.lang.IllegalArgumentException: Can not set java.lang.String field SMSMessage.receiver to java.lang.String
       at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
       at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
       at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
       at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
       at java.lang.reflect.Field.get(Field.java:393)
       at MessageProcessor.printResult(MessageProcessor.java:26)   --------------------------> Object receiverValue = receiverField.get("message");  // this is what I wrote in class MessageProcessor inside method printResult()
       at MainClass.getSMSDetails(MainClass.java:25 )-----------------------------------------> message1.printResult(); //in the MainClass, method printResult() of MessagePocessor is called from method getSMSDetails()
       at MainClass.showMenu(MainClass.java:69) ---------------------------------------------> getSMSDetails(); // this method prompts the user to enter contact number and message
       at MainClass.main(MainClass.java:94)----------------------------------------------------> calls the method showMenu() which calls the the method getSMSDetails()

Similar errors are observed when user selects Email.


However, the same piece of code worked on my friend's system in Netbeans.
 
Marshal
Posts: 6851
470
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you share the source code? (unless it is very huge in terms of lines; "huge" in terms of common sense). That way everything would be much quicker.
 
Saloon Keeper
Posts: 2619
329
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems like you are try to set the field to that of a different type - for example trying to set a long value to a field of Long type.

In your case, the error message says you are trying to set a String to a String, which you would think would be fine.  As Liutauras suggested, seeing the source code would make things easier to understand.
 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:In your case, the error message says you are trying to set a String to a String, which you would think would be fine.



Except that it's being done via reflection. It's possible that the code is running under a security manager which does not allow that.

I scanned through some tutorials and noticed that there's a setAccessible(boolean) method in the Field class -- maybe that would be useful?
 
Nivesh Chandola
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please find the attached source code.

The code in method getEmailDetails is similar to the method getSMSDetails().




---------------------------------------------------------------------------------------Main--------------------------------------------------------------------------------------------

 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your MessageProcessor class, lines 19 and 21 have a significant difference. Line 21 is right, but line 19 is wrong.
 
Nivesh Chandola
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Paul, it worked. Can't believe I didn't realise that.

Thanks everyone for helping me out as well.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!