×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Registered User
    Join Date
    Dec 2008
    Location
    Bombay
    Posts
    47

    Question clearing stringbuffer in j2me canvas based application

    Hi all;
    I am having rectangle in canvas inside which I am giving chance to user to enter input.
    I want to refresh that textfield in which I have used StringBuffer.
    I have cleared stringbuffer but afer that drawString method is giving null pointer exception becoz string is getting null as stringbuffer object had been nullified
    So anyone have solution for it
    waiting for reply
    thanking you

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: clearing stringbuffer in j2me canvas based application

    I'm struggling to understand what you mean?

    You mean, you are doing this?

    PHP Code:
    StringBuffer buffer;

    buffer null;
    g.drawString(buffer.toString(), 000); 
    This will throw a NullPointerException. Perhaps what you mean to do is:

    PHP Code:
    buffer.setLength(0); 
    Cheers,
    Graham.

  3. #3
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Re: clearing stringbuffer in j2me canvas based application

    Quote Originally Posted by j.wawdhane View Post
    Hi all;
    I am having rectangle in canvas inside which I am giving chance to user to enter input.
    I want to refresh that textfield in which I have used StringBuffer.
    I have cleared stringbuffer but afer that drawString method is giving null pointer exception becoz string is getting null as stringbuffer object had been nullified
    So anyone have solution for it
    waiting for reply
    thanking you
    Hi,
    Just keep on adding/removing the itmes in buffer..and for the every keypressed you have to call the repaint().

    not nullify the buffer,
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  4. #4
    Registered User
    Join Date
    Dec 2008
    Location
    Bombay
    Posts
    47

    Re: clearing stringbuffer in j2me canvas based application

    Hi Graham
    I have used that setlength method but still problem i sthat it is giving exception
    length of buffer become equals to zero but when keyPressed event occur . it is giving exception: ArrayIndexOutOfBoundsException
    drawstring method is once again unable to work.
    pls explain a little bit more
    thanx

  5. #5
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: clearing stringbuffer in j2me canvas based application

    Where is the exception thrown from? Can you create the smallest possible keyPressed() and paint() code to reproduce the problem, then paste them here?

  6. #6
    Registered User
    Join Date
    Dec 2008
    Location
    Bombay
    Posts
    47

    Re: clearing stringbuffer in j2me canvas based application

    case STATE_SEARCHFORM:
    switch (keyCode) {

    case KEY_NUM0:
    case KEY_NUM1:
    case KEY_NUM2:
    case KEY_NUM3:
    case KEY_NUM4:
    case KEY_NUM5:
    case KEY_NUM6:
    case KEY_NUM7:
    case KEY_NUM8:
    case KEY_NUM9:
    case KEY_CLEAR:
    //repaint();
    numOfWords = 0;//numberofwords to be displayed on canvas
    genWords = null;//generated array of words according to keywords
    if (str != null) { //str = user input = buffer.toString
    //currentText.insert(0, str);

    search();

    if (str != null && numOfWords == 0) {
    System.out.println("2");
    switchState(STATE1);
    }
    }
    meaningPos = 0;
    break;
    case KEY_SOFT_RIGHT:
    buffer.setLength(0);
    switchState(STATE2);
    break;
    case KEY_FIRE:
    case KEY_SOFT_LEFT:
    if (str != null && numOfWords != 0) {
    switchState(STATE3);
    //setText(canvasWidth - 10, canvasHeight - 50, filename0, false);
    } else if (str.length() == 0 && numOfWords == 0) {
    System.out.println("9");
    switchState(STATE4);
    }
    break;
    case KEY_UP:
    //System.out.println("mp: "+meaningPos);
    meaningPos = changePos(numOfWords, meaningPos, keyCode);
    //System.out.println("mp: "+meaningPos);
    strUpdateCnt = 0;
    break;
    case KEY_DOWN:
    meaningPos = changePos(numOfWords, meaningPos, keyCode);
    strUpdateCnt = 0;
    break;

    }
    break;






    public void paint(Graphics g) {
    switch (currentState) {
    case STATE_SPLASH:
    drawSplashScreen(g);
    break;
    case STATE_ABOUTINFO:
    drawAboutInfo(g);
    break;
    case STATE1:
    drawSTATE1(g);
    break;
    case STATE2:
    drawtextField(g);
    break;
    }
    repaint();
    }



    drawtextField(g){
    g.drawString(buffer.toString, x, y, g.top | g.left);
    }
    exception occured in drawTextField as buffer is not having any string to draw on rectangle at specified coordinate as we are setting lenght of buffer to zero whenever user came to this page from prevoious page of it.

  7. #7
    Registered User
    Join Date
    Dec 2008
    Location
    Bombay
    Posts
    47

    Re: clearing stringbuffer in j2me canvas based application

    case STATE_SEARCHFORM:
    switch (keyCode) {

    case KEY_NUM0:
    case KEY_NUM1:
    case KEY_NUM2:
    case KEY_NUM3:
    case KEY_NUM4:
    case KEY_NUM5:
    case KEY_NUM6:
    case KEY_NUM7:
    case KEY_NUM8:
    case KEY_NUM9:
    case KEY_CLEAR:
    //repaint();
    numOfWords = 0;//numberofwords to be displayed on canvas
    genWords = null;//generated array of words according to keywords
    if (str != null) { //str = user input = buffer.toString
    //currentText.insert(0, str);

    search();

    if (str != null && numOfWords == 0) {
    System.out.println("2");
    switchState(STATE1);
    }
    }
    meaningPos = 0;
    break;
    case KEY_SOFT_RIGHT:
    buffer.setLength(0);
    switchState(STATE2);
    break;
    case KEY_FIRE:
    case KEY_SOFT_LEFT:
    if (str != null && numOfWords != 0) {
    switchState(STATE3);
    //setText(canvasWidth - 10, canvasHeight - 50, filename0, false);
    } else if (str.length() == 0 && numOfWords == 0) {
    System.out.println("9");
    switchState(STATE4);
    }
    break;
    case KEY_UP:
    //System.out.println("mp: "+meaningPos);
    meaningPos = changePos(numOfWords, meaningPos, keyCode);
    //System.out.println("mp: "+meaningPos);
    strUpdateCnt = 0;
    break;
    case KEY_DOWN:
    meaningPos = changePos(numOfWords, meaningPos, keyCode);
    strUpdateCnt = 0;
    break;

    }
    break;






    public void paint(Graphics g) {
    switch (currentState) {
    case STATE_SPLASH:
    drawSplashScreen(g);
    break;
    case STATE_ABOUTINFO:
    drawAboutInfo(g);
    break;
    case STATE1:
    drawSTATE1(g);
    break;
    case STATE2:
    drawtextField(g);
    break;
    }
    repaint();
    }



    drawtextField(g){
    g.drawString(buffer.toString, x, y, g.top | g.left);
    }
    exception occured in drawTextField as buffer is not having any string to draw on rectangle at specified coordinate as we are setting lenght of buffer to zero whenever user came to this page from prevoious page of it.

  8. #8
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: clearing stringbuffer in j2me canvas based application

    Are you sure that drawString() is throwing the exception? That doesn't make any sense to me. You can draw an empty string. I think some other part of your code is getting confused.

Similar Threads

  1. Problem in deploying j2me application
    By Basu in forum Mobile Java General
    Replies: 3
    Last Post: 2008-06-05, 12:35
  2. DTMF input consumed by J2ME application (Midlet)
    By darshanbildikar in forum Mobile Java General
    Replies: 3
    Last Post: 2008-06-03, 17:03
  3. Problem developing canvas based application for N95
    By shallu_taneja in forum Mobile Java Tools & SDKs
    Replies: 2
    Last Post: 2007-09-07, 06:20
  4. Replies: 0
    Last Post: 2004-08-25, 11:42

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×