(java.lang.String)不能应用于(java.lang.Object) [英] (java.lang.String) cannot be applied to (java.lang.Object)

查看:2495
本文介绍了(java.lang.String)不能应用于(java.lang.Object)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Ive a Listner class called TopicS Im trying to call it from a gui called readMessages

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

When Im trying to run the class TopicS using the following method,

   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)

addTopicListner(java.lang.String) in Topics Cannot be applied to (java.lang.Object)

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

When I change the String to Object I get other errors. The main method is included below, this works fine without GUI, but I need to add it to GUI. What I am trying to do is take value to combobox which is String array, and place that string into topic (where the (t) is now

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


推荐答案

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

That's because JComboBox.html.getSelectedItem() returns Object

public Object getSelectedItem()

你的方法需要一个字符串

And your method expects a string

public void addTopicToListner(String t)

如果您100%确定组合框的内容是字符串你只需要施放它:

If you're 100% sure the contents of your combobox are string you just have to cast it:

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

就是这样。

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

This code sample reproduces exactly your compilation error:

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 被声明为对象,但它没有办法知道是否该对象是一个字符串或其他东西(例如日期)。

As you see, the last call (workWithString(o) ) doesn't compile even though it is a String object. It turns out the compiler only knows that o was declared as Object but it doesn't have a way to know if that object is a string or is something else ( a Date for instance ).

我希望这有帮助。

这篇关于(java.lang.String)不能应用于(java.lang.Object)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆