(java.lang.String)不能应用于(java.lang.Object)

ska*_*kal 6 java string user-interface java.lang.class

我有一个名为TopicS的Listner类, 我试图从名为readMessages的gui中调用它

当我尝试使用以下方法运行类TopicS时,

   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());
}                 
Run Code Online (Sandbox Code Playgroud)

它给我错误的说法

主题中的addTopicListner(java.lang.String)无法应用于(java.lang.Object)

当我将字符串更改为对象时,会出现其他错误。主要方法包括在下面,在没有GUI的情况下可以正常工作,但是我需要将其添加到GUI。我想做的就是将值带到组合框(即String数组)中,并将该字符串放入主题中(其中(t)现在

 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();
  }
 }
}
Run Code Online (Sandbox Code Playgroud)

Osc*_*Ryz 1

那是因为JComboBox.html.getSelectedItem()返回 Object

public Object getSelectedItem()
Run Code Online (Sandbox Code Playgroud)

你的方法需要一个字符串

public void addTopicToListner(String t)
Run Code Online (Sandbox Code Playgroud)

如果您 100% 确定组合框的内容是字符串,则只需将其强制转换:

a.addTopicToListner( (String) topicCombobox.getSelectedItem());
Run Code Online (Sandbox Code Playgroud)

就是这样。

此代码示例准确地重现了您的编译错误:

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 );

    }

}
Run Code Online (Sandbox Code Playgroud)

输出:

StringAndObject.java:19: workWithString(java.lang.String) in StringAndObject cannot be applied to (java.lang.Object)
        workWithString( o );
        ^
1 error   
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,即使最后一个调用 ( )一个 String 对象,workWithString(o)也不会编译。事实证明,编译器只知道它被声明为,但它没有办法知道该对象是字符串还是其他东西(例如 a )。oObjectDate

我希望这有帮助。