(java.lang.string) non può essere applicato a (java.lang.object)
-
29-10-2019 - |
Domanda
Ive una classe di elenco chiamata Temi Sto cercando di chiamarlo da una GUI chiamata ReadMessages
Quando sto cercando di eseguire gli argomenti di classe usando il seguente metodo,
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("test test test");
System.out.print("you pressed" +topicCombobox.getSelectedItem());
TopicS a = new TopicS();
a.addTopicToListner(topicCombobox.getSelectedItem());
}
Mi dà un errore dicendo
addTopicListner (java.lang.string) In argomenti non può essere applicato a (java.lang.object)
Quando cambio la stringa in oggetto ricevo altri errori. Il metodo principale è incluso di seguito, funziona bene senza GUI, ma devo aggiungerlo alla GUI. Quello che sto cercando di fare è prendere valore per combobox che è un array di stringhe e metti quella stringa nell'argomento (dove il (t) è ora
import java.util.Hashtable;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class TopicS implements MessageListener
{
private TopicConnection topicConnection;
private TopicSession topicSession;
public Topic topic;
private TopicSubscriber topicSubscriber;
public TopicS()
{}
public void addTopicToListner(String t){
try
{
// create a JNDI context
Hashtable properties = new Hashtable();
properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.exolab.jms.jndi.InitialContextFactory");
properties.put(Context.PROVIDER_URL,"rmi://localhost:1099/");
Context context = new InitialContext(properties);
// retrieve topic connection factory
TopicConnectionFactory topicConnectionFactory =
(TopicConnectionFactory)context.lookup("JmsTopicConnectionFactory");
// create a topic connection
topicConnection = topicConnectionFactory.createTopicConnection();
// create a topic session
// set transactions to false and set auto acknowledgement of receipt of messages
topicSession = topicConnection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
// retrieve topic
topic = (Topic) context.lookup(t);
// create a topic subscriber and associate to the retrieved topic
topicSubscriber = topicSession.createSubscriber(topic);
// associate message listener
topicSubscriber.setMessageListener(this);
// start delivery of incoming messages
topicConnection.start();
}
catch (NamingException e)
{
e.printStackTrace();
}
catch (JMSException e)
{
e.printStackTrace();
}
}
/* public static void main(String[] args)
//{
try
{
TopicS listener = new TopicS();
Thread.currentThread().sleep(2000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
*/
// process incoming topic messages
public void onMessage(Message message)
{
try
{
String messageText = null;
if (message instanceof TextMessage)
messageText = ((TextMessage)message).getText();
System.out.println(messageText);
}
catch (JMSException e)
{
e.printStackTrace();
}
}
}
Soluzione
È perché Jccombobox.html.getSelectedem () Restituisce l'oggetto
public Object getSelectedItem()
E il tuo metodo si aspetta una stringa
public void addTopicToListner(String t)
Se sei sicuro al 100% che i contenuti del tuo combobox sono stringhe devi solo lanciarlo:
a.addTopicToListner( (String) topicCombobox.getSelectedItem());
E questo è tutto.
Questo campione di codice riproduce esattamente l'errore di compilazione:
class StringAndObject {
public void workWithString( String s ) {} // We just care about
public void workWithObject( Object o ) {} // the signature.
public void run() {
String s = ""; // s declared as String
Object o = s; // o declared as Object
// works because a String is also an Object
workWithObject( s );
// naturally a s is and String
workWithString( s );
// works because o is an Object
workWithObject( o );
// compiler error....
workWithString( o );
}
}
Produzione:
StringAndObject.java:19: workWithString(java.lang.String) in StringAndObject cannot be applied to (java.lang.Object)
workWithString( o );
^
1 error
Come vedi, l'ultima chiamata (workWithString(o)
) non si compila anche se ciò è un oggetto stringa. Si scopre che il compilatore lo sa o
è stato dichiarato come Object
Ma non ha un modo per sapere se quell'oggetto è una stringa o è qualcos'altro (a Date
per esempio ).
Spero che questo aiuti.
Altri suggerimenti
JComboBox.getSelectedItem()
Tipo di restituzione Object
, non String
. Puoi chiamare toString()
Sul suo risultato per restituire la rappresentazione della stringa del tuo oggetto. Sembra che tu stia cercando di restituire un tipo di Topic
, il che significa che dovrai sovrascrivere il toString()
metodo su Topic
Per restituire il valore che desideri.
Prova il seguente codice
topicCombobox.getSelectedItem() instanceof String ? (String)topicCombobox.getSelectedItem() : "Socks";
Questa è una soluzione temporanea, perché non so se la entrata getSelectedItem()
è un String
.
Se lo sai, lo verrà sempre scelto
(String)topicCombobox.getSelectedItem()