Please help with setData of Datagram class

hi
im new to J2ME stuff. while working with BOOK "wireless java programming with java 2 micro edition" i encountered a problem in networking chapter.
Im gating NULLPointer Exception while using setData() method of Datagram Class
               byte[] message = new byte[length];
               System.arraycopy(msg.getBytes(),0,message,0,length);
// null poninter exception// sendDatagram.setData(message,0,length);
               sendDatagram.setLength(length);
the whole code of client program is as...
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class DatagramClient extends MIDlet implements CommandListener{
     private Form mainScreen;
     private TextField sendingField;
     private Display myDisplay = null;
     private DatagramClientHandler client;
     //define the GUI components for displaying the returned message
     private Form resultScreen;
     private StringItem resultField;
     private String resultString;
     Command sendCommand = new Command("SEND",Command.OK,1);
     public DatagramClient() {
          //intialize the GUI components
          myDisplay = Display.getDisplay(this);
          mainScreen = new Form("Datagram Client");
          sendingField = new TextField("Enter your message",null,30,TextField.ANY);
          mainScreen.append(sendingField);
          mainScreen.addCommand(sendCommand);
          mainScreen.setCommandListener(this);
     public void startApp(){
          myDisplay.setCurrent(mainScreen);
          client = new DatagramClientHandler();
     public void pauseApp(){
     public void destroyApp(boolean unconditional){
     public void commandAction (Command c, Displayable s) {
          if(c==sendCommand){
               //get the message text from user input
               String sendMessage = sendingField.getString();
               try{
                    resultString = client.send_receive(sendMessage);
               }catch(IOException e){
                    System.out.println("Failed in Send_receive(); "+e);
               resultScreen = new Form ("Message confirmed:");
               resultField = new StringItem(null,resultString);
               resultScreen.append(resultField);
               resultScreen.setCommandListener(this);
               myDisplay.setCurrent(resultScreen);
     public class DatagramClientHandler extends Object {
          private DatagramConnection dc;
          private Datagram sendDatagram;
          private Datagram receiveDatagram;
          public DatagramClientHandler() {
               try{
                    dc = (DatagramConnection)Connector.open("datagram://"+":9000");
                    receiveDatagram = dc.newDatagram(dc.getMaximumLength());
               } catch(IOException e){
                    System.out.println(e.toString());
          public String send_receive(String msg) throws IOException {
               int length = msg.length();
               System.out.println("length of string "+length+"message "+msg);
               byte[] message = new byte[length];
               System.arraycopy(msg.getBytes(),0,message,0,length);
               System.out.println("message is : "+message+" length is "+length);
               sendDatagram.setData(message,0,length);
               sendDatagram.setLength(length);
               String retval = "";
               try{
                    dc.send(sendDatagram);
                    dc.receive(receiveDatagram);
                    byte[] data = receiveDatagram.getData();
                    retval = new String(data,0,receiveDatagram.getLength());
               }catch (Exception e ){System.out.println("sirfra excep: "+e);}
               finally{
                    if(dc!=null) dc.close();
               return retval;
sir i hav to submit an assignment on networking and RMS on thursday. so please help me as early as possible . ill be really very thankful to you.

please change "sendDatagram" to "receiveDatagram"
public String send_receive(String msg) throws IOException {
int length = msg.length();
System.out.println("length of string "+length+"message "+msg);
byte[] message = new byte[length];
System.arraycopy(msg.getBytes(),0,message,0,length);
System.out.println("message is : "+message+" length is "+length);
sendDatagram.setData(message,0,length);
sendDatagram.setLength(length);

Similar Messages

Maybe you are looking for

  • Simple question on view but solution seems to be bit confusin plz help

    i want to construct a view on top of a table with a condition: I should not modify the view each time a new column is added to table. eg: table has empno ename address grade 1 aaa US A 2 bbb US c 1 aaa US B 2 ddd AUS B I want to create a view on top

  • Restricting which orders stock can be issued to.

    Hi, Is it possible to restrict which order types a user can issue stock to using transaction MB1A? We would like to prevent users issuing stock to orders created by other service areas. This could be restricted by the order type or planner group of t

  • Interactive quicktime failure by keynote

    i am using keynotes 2.0 with quicktime pro. i am thinking to creating a quicktime movie, which can be clickable in last frame linked to a webage. i tried adding HREFtrack in quicktime pro but it fails. so i import the movie to keynotes and try to exp

  • Sende Button funktioniert nicht bei jedem

    Hallo, folgendes Problem. Ich habe ein Formular erstellt was über einen "Send Button" an eine feste Mailadresse als pdf versendet werden soll. Bei mir selbst auf dem PC funktioniert das ganze auch aber bei anderen passiert nichts wenn der send button

  • How do I load and save to my iMac a dvd purchased elsewhere?

    how do I load and save to my iMac a dvd purchased elsewhere?  Thank you.