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 15 of 15

Thread: Casting java.lang.String to a custom class

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Casting java.lang.String to a custom class

    Matrix's dialogue system to get dialogues is as follows:

    Put a dialogue like this: handledDialogues.put("Bob", (Class<Dialogue>) Bob.class.getCanonicalName());

    So I said to myself, why not add them all automatically by file location?

    I did this:

    		for (File file : new File("bin/com/rs/game/player/dialogues/impl").listFiles()) {
    				Class<Dialogue> dialogue = (Class<Dialogue>) Class.forName(file.getName().replaceAll(".class", "").getClass().getCanonicalName());
    				if (!handledDialogues.containsKey(dialogue)) {
    					handledDialogues.put(file.getName().replaceAll(".class", ""), dialogue);
    					System.out.println("put " + file.getName());
    				}
    			}

    And now the dialogues in the impl folder are not loading and this error is printed:

    java.lang.ClassCastException: java.lang.String cannot be cast to com.rs.game.player.dialogues.Dialogue
    	at com.rs.game.player.dialogues.DialogueHandler.getDialogue(DialogueHandler.java:208)
    	at com.rs.game.player.DialogueManager.startDialogue(DialogueManager.java:20)
    	at com.rs.networking.decoders.handlers.NPCHandler$1.run(NPCHandler.java:96)
    	at com.rs.game.player.CoordsEvent.processEvent(CoordsEvent.java:46)
    	at com.rs.game.player.Player.processEntity(Player.java:543)
    	at com.rs.cores.WorldThread.run(WorldThread.java:34)

    On line:
    return classD.newInstance();

    	public static final Dialogue getDialogue(Object key) {
    		if (key instanceof Dialogue)
    			return (Dialogue) key;
    		Class<Dialogue> classD = handledDialogues.get(key);
    		if (classD == null)
    			return null;
    		try {
    			return classD.newInstance();
    		} catch (Throwable e) {
    			Logger.handle(e);
    		}
    		return null;
    	}
    Last edited by Tyluur; July 29th, 2012 at 12:33 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Casting java.lang.String to a custom class

    What does the newInstance() method return?

    What variable has a String value?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Casting java.lang.String to a custom class

    I don't know what variable has a string value.


    Outprinting newInstances gives me:

    java.lang.ClassCastException: java.lang.String cannot be cast to com.rs.game.player.dialogues.Dialogue
    O

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Casting java.lang.String to a custom class

    Quote Originally Posted by Tyluur View Post
    I don't know what variable has a string value.
    The variable you are casting to com.rs.game.player.dialogues.Dialogue

  5. #5
    Member
    Join Date
    Jul 2012
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Casting java.lang.String to a custom class

    I want that to be fixed and don't know how.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Casting java.lang.String to a custom class

    Can you post the code for the newInstance() method to show what it returns?

    Or can you post a small complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jul 2012
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Casting java.lang.String to a custom class

    I posted what newInstance returns here: http://www.javaprogrammingforums.com...html#post72103

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Casting java.lang.String to a custom class

    Can you post the code? Your link does not show the code. It points to an error message.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Casting java.lang.String to a custom class

    What Norm meant by "show what it returns" is to get the return value from the signature/footprint of the method. Not to see the error message

  10. #10
    Member
    Join Date
    Jul 2012
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Casting java.lang.String to a custom class

    That's what is outprinted. I returned it by out.println(classD.newInstance());

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Casting java.lang.String to a custom class

    Can you post the code for the newInstance() method?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member
    Join Date
    Jul 2012
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Casting java.lang.String to a custom class

        public T newInstance()
            throws InstantiationException, IllegalAccessException
        {
            if (System.getSecurityManager() != null) {
                checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
            }
            return newInstance0();
        }

    It's in the JDK packaging.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Casting java.lang.String to a custom class

    Can you post a small complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Casting java.lang.String to a custom class

    The culprit line is here. (PS aren't you from rune-server?)
    Class<Dialogue> dialogue = (Class<Dialogue>) Class.forName(file.getName().replaceAll(".class", "").getClass().getCanonicalName());
    `file.getName().replaceAll(...)` is a String object, so calling `getClass()` returns String.class. `String.class.getCanonicalName()` will then be "java.lang.String.class", which (after removing the .class extension), when passed through Class.forName(), will return String.class once again... heh. Maybe you should write your code a bit cleaner so you notice simple mistakes like this with ease.

    EDIT: To clarify, what you probably want is:
    String name = file.getName();
    name = name.substring(0, name.length() - 6);
    Class<? extends Dialogue> clazz = Class.forName("com.rs.game.player.dialogues.impl." + name).asSubclass(Dialogue.class);
    if (!handledDialogues.containsKey(name)) {
      handledDialogues.put(name, clazz);
    }
    Last edited by veeer; August 5th, 2012 at 03:35 PM.

  15. #15
    Member
    Join Date
    Jul 2012
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Casting java.lang.String to a custom class

    Quote Originally Posted by veeer View Post
    The culprit line is here. (PS aren't you from rune-server?)
    Class<Dialogue> dialogue = (Class<Dialogue>) Class.forName(file.getName().replaceAll(".class", "").getClass().getCanonicalName());
    `file.getName().replaceAll(...)` is a String object, so calling `getClass()` returns String.class. `String.class.getCanonicalName()` will then be "java.lang.String.class", which, when passed through Class.forName(), will return String.class once again... heh. Maybe you should write your code a bit cleaner so you notice simple mistakes like this with ease.

    EDIT: To clarify, what you probably want is:
    String name = file.getName();
    name = name.substring(0, name.length() - 6);
    Class<? extends Dialogue> clazz = Class.forName("com.rs.game.player.dialogues.impl." + name).asSubclass(Dialogue.class);
    if (!handledDialogues.containsKey(name)) {
      handledDialogues.put(name, clazz);
    }
    Yeah, I'm from Rune-Server.

Similar Threads

  1. Class Casting
    By Sana1990 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 9th, 2012, 08:13 AM
  2. [SOLVED] why casting int to String is not possible through brackets method
    By voltaire in forum Java Theory & Questions
    Replies: 2
    Last Post: May 2nd, 2010, 04:00 PM
  3. reading string input then casting it to an int?
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: March 27th, 2010, 11:49 PM
  4. Custom Java stack class (with generics) problem
    By TBBucs in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 7th, 2010, 02:25 AM
  5. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM