powered by Jive Software

Problem PacketListener

Hi Everyone!

I’‘m new user of Smack and I need your help for my school project that’'s to create a Instant Messaging Client.

I’'ve a problem with the method (XMPPConnexion).addPacketListener(PacketListener, Filter)

When I launch my program, I’'ve always an JavaNullPointerException but sending and receiving Message work…

Another problem is that I’'ve a lot of Message Body receive to null when I Chat on my program with a spark client ( when the spark user write a message on chat window and when I send a message with my client)

Ps : Sorry for the vocabulary but i’'m french and my english is not good

Could you post your code that is causing the null pointer exception.

My code is :

/b

import javax.swing.*;

import java.awt.*;

//import java.awt.event.*;

import org.jivesoftware.smack.*;

import org.jivesoftware.smack.packet.*;

import org.jivesoftware.smack.filter.*;

public class Fenetre extends JFrame implements PacketListener{

public XMPPConnection con;

public JPanel global, ecrire;

public JTextArea ecr, rec;

public JScrollPane scrollecr, scrollrec;

public JButton envoi;

public static Chat chat;

public PacketListener myListener;

public PacketFilter filter;

public Fenetre(){

this.setSize(400,200);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.global = new JPanel();

this.global.setLayout(new BorderLayout());

this.ecr = new JTextArea();

this.rec = new JTextArea();

this.rec.setEditable(false);

this.rec.setRows(7);

this.ecr.setRows(3);

this.ecr.setLineWrap(true);

this.rec.setLineWrap(true);

this.envoi = new JButton(“Envoyer”);

this.scrollecr = new JScrollPane(this.ecr);

this.scrollrec = new JScrollPane(this.rec);

this.ecrire = new JPanel();

this.ecrire.setLayout(new BorderLayout());

this.ecrire.add(scrollecr, BorderLayout.CENTER);

this.ecrire.add(envoi, BorderLayout.EAST);

this.global.add(scrollrec, BorderLayout.CENTER);

this.global.add(ecrire, BorderLayout.SOUTH);

this.add(global);

this.setTitle(“Conversation Window Test”);

this.setVisible(true);

boolean co = this.createConnexion();

if (co){

this.setTitle(“Connected”);

}

this.envoi.addActionListener(new Action_Listener(this));

this.filter = new PacketTypeFilter(Message.class);

this.con.addPacketListener(this, filter);

}

public boolean createConnexion(){

try{

//Create a connection to the jivesoftware.com XMPP server.

XMPPConnection.DEBUG_ENABLED = true;

XMPPConnection con = new XMPPConnection(“yourserver”);

// Most servers require you to login before performing other tasks.

con.login(“your username”, "your pasword);

this.chat = con.createChat(“username of friend”);

filter = new PacketTypeFilter(Message.class);

con.addPacketListener(this, filter);

return true;

}catch(XMPPException e){

e.getMessage();

return false;

}

}

public void processPacket(Packet packet) {

Message message = (Message)packet;

String from = message.getFrom();

String body = message.getBody();

this.rec.append(this.chat.getParticipant()+ " dit : \n " + body + “\n”);

}

public static void main (String[] args){

Fenetre fen = new Fenetre();

}

}

Thanks for help

EDIT : I’‘ve found that my exception came from the listener in the constructor that i’‘ve forget to remove … then I’'ve no Exception when I run the program but the messages null when I chat with a spark client are always receive.

I can made a filter when the received message body is null but I don’‘t know if it’'s the best solution…

Message was edited by: Slashsnakit

One more thing can you post your exact exception stack trace that you are getting?

I’‘ve found that my exception came from the listener in the constructor that i’‘ve forget to remove … then I’'ve no Exception when I run the program but the messages null when I chat with a spark client are always receive.

I can made a filter when the received message body is null but I don’‘t know if it’'s the best solution…

(You answer the same time I edit my post )

There are message packets sent by spark that are status updates, like that a message is being typed, etc. These messages don’‘t contain a body, so you’'ll always wawnt to check your Message packets to make sure they have a body.

Alex

Many Thanks! That’'s what I think and you confirm

Ok then I can continue to program

I’‘ve seen that you’'re Developper but you work for jive or they choose moderators on net programmers ?

It’'s cool to speak with programmers who have experience and lives so far.

Message was edited by: Slashsnakit

Thx to AWenckus