The Artima Developer Community
Sponsored Link

Java Answers Forum
Convert type to type

1 reply on 1 page. Most recent reply: Oct 23, 2008 6:40 AM by Jesper de Jong

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Mustafa Magdy

Posts: 1
Nickname: mustafamax
Registered: Oct, 2008

Convert type to type Posted: Oct 19, 2008 1:58 AM
Reply to this message Reply
Advertisement
I was trying to write a generic function that converts a value to a generic type:

public static <T> T toType(Object value, Class<T> tClass) {
try {
T obj = tClass.newInstance();
return obj;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}

it gives me InitiationException why?


Jesper de Jong

Posts: 6
Nickname: 56437
Registered: Jun, 2008

Re: Convert type to type Posted: Oct 23, 2008 6:40 AM
Reply to this message Reply
InitiationException? Did you mean InstantiationException? The API documentation of InstantiationException explains when this exception can happen (look it up).

Is the type you are calling this with a concrete type (not an abstract class or an interface)? Does the type have a no-arguments constructor?

Flat View: This topic has 1 reply on 1 page
Topic: setCurrentDirectory from a string????? Previous Topic   Next Topic Topic: Capture http requests

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use