发送消息时出现空点异常

Posted

技术标签:

【中文标题】发送消息时出现空点异常【英文标题】:Null point Exception while sending message 【发布时间】:2016-02-01 06:59:38 【问题描述】:

我在 /com.quickblox.q_municate/ui/friends 下添加了新的类文件。我想向另一个用户发送消息。我使用 Quickblox API 编写了代码,代码如下。

public class ChangeDSConfiguration extends Activity

TextView OpenSwitchRelayFor;
TextView OpenPowerRelayFor;
TextView PowerRelayCode;
TextView SwitchRelayCode;
TextView SecretPassword;
EditText OpenSwitchRelayForValue;
EditText OpenPowerRelayForValue;
EditText PowerRelayCodeValue;
EditText SwitchRelayCodeValue;
EditText SecretPasswordValue;
Button ChangeConfiguration;

String password;
String configurationDetails;
int id;

public static void start(Context context, int id) 
    Intent intent = new Intent(context, ChangeDSConfiguration.class);
    intent.putExtra(QBServiceConsts.USER_ID, id);
    context.startActivity(intent);


@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_change_dsconfiguration);

    id = getIntent().getExtras().getInt(QBServiceConsts.USER_ID);
    Log.e("ChangeConfig", "id = "+id);


    OpenSwitchRelayFor = (TextView) findViewById(R.id.openSwitchRelayFor);
    OpenPowerRelayFor = (TextView) findViewById(R.id.openPowerRelayFor);
    PowerRelayCode = (TextView) findViewById(R.id.powerRelayCode);
    SwitchRelayCode = (TextView) findViewById(R.id.switchRelayCode);
    SecretPassword = (TextView) findViewById(R.id.secretPassword);
    OpenSwitchRelayForValue = (EditText) findViewById(R.id.etOpenSwitchRelayFor);
    OpenPowerRelayForValue = (EditText) findViewById(R.id.etOpenPowerRelayFor);
    PowerRelayCodeValue = (EditText) findViewById(R.id.etPowerRelayCode);
    SwitchRelayCodeValue = (EditText) findViewById(R.id.etSwitchRelayCode);
    SecretPasswordValue = (EditText) findViewById(R.id.etSecretPassword);
    ChangeConfiguration = (Button) findViewById(R.id.changeConfiguration);

    ChangeConfiguration.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            showAskForSecretPasswordDialog();
        
    );


public void showAskForSecretPasswordDialog()
    LayoutInflater inflater = getLayoutInflater();
    View view = inflater.inflate(R.layout.layout_enter_password, null);

    final EditText secret_password;
    secret_password = (EditText) view.findViewById(R.id.secretPassword);
    secret_password.setText("");
    secret_password.setFocusable(true);
    AlertDialog.Builder askForPasswordDialog = new AlertDialog.Builder(ChangeDSConfiguration.this);

    askForPasswordDialog.setView(view);
    askForPasswordDialog.setCancelable(false);
    askForPasswordDialog.setPositiveButton("Continue", new DialogInterface.OnClickListener() 
        @Override
        public void onClick(DialogInterface dialog, int which) 
            password = secret_password.getText().toString();
            Log.e("FriendsListFragment", "password = " + password);
            if(password.equals(""))
                Toast.makeText(ChangeDSConfiguration.this, "Please enter password before you continue", Toast.LENGTH_LONG).show();
                return;
            else
                password = "Change Configuration:" + password;      // frame relay code message, with header and relay type
                sendConfigurartionDetails(password);
                finish();
            
        
    );

    askForPasswordDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
        @Override
        public void onClick(DialogInterface dialog, int which) 
            return ;
        
    );

    askForPasswordDialog.setTitle(R.string.title_enter_relay_code);
    askForPasswordDialog.setMessage(R.string.message_enter_relay_code);
    askForPasswordDialog.show();

    return;


public void sendConfigurartionDetails(String password)

    if (!QBChatService.isInitialized()) 
        Log.e("ChangeConfig", "(!QBChatService.isInitialized())");
        QBChatService.init(this);
    

    configurationDetails = password + ":" + getConfigurationDetails();
    Log.e("ChangeDSConfig", "configurationDetails = "+configurationDetails);

    try 
    QBChatMessage message = new QBChatMessage();
    message.setBody(configurationDetails);

    QBPrivateChatManager privateChatManager = QBChatService.getInstance().getPrivateChatManager();
        Log.e(" ChangeDSConfig", "id = "+id);
    QBPrivateChat privateChat = privateChatManager.getChat(id);
        Log.e(" ChangeDSConfig", "getChat(id);");
    if (privateChat == null) 
        privateChat = privateChatManager.createChat(id, null);
        Log.e(" ChangeDSConfig", "Private chat created");
    

        privateChat.sendMessage(message);
        Log.e(" ChangeDSConfig", "Power Relay Code sent");
     catch (XMPPException e) 
        Toast.makeText(ChangeDSConfiguration.this,e.getMessage(), Toast.LENGTH_LONG).show();
     catch (SmackException.NotConnectedException e) 
        Toast.makeText(ChangeDSConfiguration.this,e.getMessage(), Toast.LENGTH_LONG).show();
    



public String getConfigurationDetails()

    String str1 = OpenSwitchRelayForValue.getText().toString();
    String str2 = OpenPowerRelayForValue.getText().toString();
    String str3 = PowerRelayCodeValue.getText().toString();
    String str4 = SwitchRelayCodeValue.getText().toString();
    String str5 = SecretPasswordValue.getText().toString();

    String str6 = str1 + ":" + str2 + ":" + str3 + ":" + str4 + ":" + str5 ;

    return str6;


我在这里得到空点异常

    QBPrivateChat privateChat = privateChatManager.getChat(id);

【问题讨论】:

【参考方案1】:

显然之前的调用返回null

QBChatService.getInstance().getPrivateChatManager();

检查您的 getPrivateChatManager() 实施。

【讨论】:

以上是关于发送消息时出现空点异常的主要内容,如果未能解决你的问题,请参考以下文章

使用 PHP 发送 zip 文件时出现空消息

尝试通过 websocket 转换和发送时出现空指针异常

队列不存在或您没有足够的权限来执行该操作。通过 MSMQ 发送消息时出现异常

向 kafka 主题发送消息时出现 TimeoutException

在 Java 中读取 protobuf 消息时出现异常

使用 PEAR 邮件发送测试消息时出现错误消息