【问题标题】:(java.lang.String) cannot be applied to (java.lang.Object)(java.lang.String) 不能应用于 (java.lang.Object)
【发布时间】:2011-03-23 19:46:18
【问题描述】:

我有一个名为 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());
}                 

它给了我错误提示

主题中的addTopicListner(java.lang.String) 不能应用于(java.lang.Object)

当我将字符串更改为对象时,我得到了其他错误。主要方法包含在下面,没有 GUI 也可以正常工作,但我需要将其添加到 GUI。我正在尝试做的是将值作为字符串数组的组合框,并将该字符串放入主题中(现在 (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();
  }
 }
}

【问题讨论】:

  • getSelectedItem 的结果是字符串吗?如果可以,你能投吗?

标签: java string user-interface java.lang.class


【解决方案1】:

JComboBox.getSelectedItem() 返回类型 Object,而不是 String。您可以在其结果上调用 toString() 以返回对象的字符串表示形式。看起来您正在尝试返回 Topic 的类型,这意味着您需要覆盖 Topic 上的 toString() 方法才能返回所需的值。

【讨论】:

    【解决方案2】:

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

    public Object getSelectedItem()
    

    你的方法需要一个字符串

    public void addTopicToListner(String t)
    

    如果你 100% 确定你的组合框的内容是字符串,你只需要转换它:

    a.addTopicToListner( (String) topicCombobox.getSelectedItem());
    

    就是这样。

    此代码示例完全重现了您的编译错误:

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

    输出:

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

    如您所见,最后一次调用 (workWithString(o) ) 无法编译,即使它 一个 String 对象。结果编译器只知道o 被声明为Object,但它无法知道该对象是字符串还是其他对象(例如Date)。

    我希望这会有所帮助。

    【讨论】:

    • 谢谢你,它工作得很好,解释也很好
    【解决方案3】:

    试试下面的代码

    topicCombobox.getSelectedItem() instanceof String ? (String)topicCombobox.getSelectedItem() : "Socks";
    

    这是一个临时修复,因为我不知道传入的 getSelectedItem() 是否是 String
    如果你知道它总是会被投射出来

    (String)topicCombobox.getSelectedItem()
    

    【讨论】:

    • 正确的做法:String.valueOf(topicCombobox.getSelectedItem())
    • 为什么是String.valueOf 而不是.toString()
    • @Riggy:出于一种可能的原因,String.valueOf 处理 null。但是,我不喜欢默默接受 null 的想法。
    • 您的代码缺少另外三分之一的条件表达式。
    • @Mark 谢谢,我的好奇心已经满足了。
    猜你喜欢
    • 2014-11-23
    • 2018-07-17
    • 2021-12-08
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多