Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help please i keep getting a runtime error with code that seems correct.

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help please i keep getting a runtime error with code that seems correct.

    I am trying to read an object from a file. I have followed the examples in my book and online that i could find. but i keep getting an error saying

    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.String;
    at Manager.savemeth(Manager.java:67)
    at Manager$1.actionPerformed(Manager.java:27)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)

    it keeps going but this is the main part. 67 is the String[] temp that i posted below and 27 is the call to the method with the string in it

     
    				instream = new ObjectInputStream(new FileInputStream(filename));
    				String[] temp = (String[])(instream.readObject());

    i can't figure out why im running into this problem.
    Last edited by jorys22; December 7th, 2013 at 06:04 PM. Reason: Solved


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,193
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Help please i keep getting a runtime error with code that seems correct.

    Is the readObject() method returning a String object instead of a String array?
    Read into an Object and print out its class (call getClass)
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Help please i keep getting a runtime error with code that seems correct.

    Quote Originally Posted by jorys22 View Post
    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.String;

    i can't figure out why im running into this problem.
    The exception message is (at least for me) clear: it states that a String cannot be cast to String[] (the part "[L" is the bytecode-level indication of an array of objects).
    Why are you expecting a String[] ? What have you serialized (elsewhere in the code) into the file? If you have serialized N single String objects, you can't expect to read them as a String[].
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

Similar Threads

  1. Runtime error-- Applet program
    By amar5445 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 28th, 2013, 07:32 AM
  2. Runtime Error
    By Dennis Enya in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 24th, 2013, 12:44 PM
  3. Replies: 1
    Last Post: January 17th, 2013, 06:04 AM
  4. Runtime Error
    By SyntheticD in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 10th, 2011, 03:09 PM
  5. Runtime Error running in UVA
    By mathfxr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 17th, 2010, 02:06 PM