×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Mar 2004
    Location
    Europe, Poland, Słupsk
    Posts
    5

    NullPointerOperationException while trying to readUTF()

    Hi!

    I've got some serious problems with writting data to the memory. Everything seems to be ok, until I try to read string using readUTF() method. Here is my native code:

    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.*;
    import javax.microedition.rms.*;
    import java.io.*;

    public class TestMIDlet extends MIDlet implements CommandListener {

    Display display = Display.getDisplay(this);
    Form mainForm = new Form("Testowy form.");
    Command CMD_OPEN_RECORD_STORE = new Command("Open/add record store", Command.SCREEN, 1);
    Command CMD_SHOW_RECORDS = new Command("Show records", Command.SCREEN, 1);
    Command CMD_ADD_RECORD = new Command("Add record", Command.SCREEN, 1);
    Command CMD_CLEAN_MEMORY = new Command("Clean memory", Command.SCREEN, 1);
    Command CMD_INFO = new Command("Info", Command.SCREEN, 1);
    Command CMD_CLEAN = new Command("Clear screen", Command.SCREEN, 1);
    Command CMD_EXIT = new Command("Exit", Command.EXIT, 1);
    RecordStore rs = null;
    RecordEnumeration re = null;
    TextField commandLine = new TextField("Command Line:", "", 100, TextField.ANY);
    ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
    DataOutputStream outStream = new DataOutputStream(outBytes);
    ByteArrayInputStream inBytes;
    DataInputStream inStream;
    byte[] bytes;
    String[] recordStoreNames;
    String[] recordValues;
    String rsName;
    int recordId;

    protected void startApp() {
    mainForm.append(commandLine);
    mainForm.addCommand(CMD_OPEN_RECORD_STORE);
    mainForm.addCommand(CMD_SHOW_RECORDS);
    mainForm.addCommand(CMD_ADD_RECORD);
    mainForm.addCommand(CMD_CLEAN_MEMORY);
    mainForm.addCommand(CMD_INFO);
    mainForm.addCommand(CMD_CLEAN);
    mainForm.addCommand(CMD_EXIT);
    mainForm.setCommandListener(this);
    display.setCurrent(mainForm);
    info();
    }

    public void commandAction(Command c, Displayable d) {
    if (c == CMD_OPEN_RECORD_STORE) {
    open_record_store();
    }
    if (c == CMD_SHOW_RECORDS) {
    show_records();
    }
    if (c == CMD_ADD_RECORD) {
    add_record();
    }
    if (c == CMD_CLEAN_MEMORY) {
    clean_memory();
    }
    if (c == CMD_INFO) {
    info();
    }
    if (c == CMD_CLEAN) {
    while (mainForm.size() > 0) mainForm.delete(0); // mainForm.deleteAll();
    mainForm.append(commandLine);
    }
    if (c == CMD_EXIT) {
    destroyApp(false);
    notifyDestroyed();
    }
    }

    protected void destroyApp(boolean unconditional) {
    }

    protected void pauseApp() {
    }

    void open_record_store() {
    if (rs != null) try {rs.closeRecordStore();rs = null;} catch (Exception e) {}
    if (commandLine.size() == 0) mainForm.append("You must type the name for the record store."); else {
    mainForm.append("Opening record store \"" + commandLine.getString() + "\"...");
    try {
    rs = RecordStore.openRecordStore(commandLine.getString(), false);
    mainForm.append("Done.");
    } catch (RecordStoreNotFoundException rseNotFound) {
    try {
    rs = RecordStore.openRecordStore(commandLine.getString(), true);
    mainForm.append("There isn't such record store, created new.");
    } catch (RecordStoreException rse) {mainForm.append("Error while creating record store (" + rse + ")");}
    } catch (RecordStoreException rse) { mainForm.append("Error while creating record store (" + rse + ")");}
    }
    }

    void show_records() {
    if (rs == null) mainForm.append("No record store is open."); else {
    try {
    rsName = rs.getName();
    } catch (Exception e) {rsName = "";}
    mainForm.append("Entries in record store \"" + rsName + "\":");
    try {
    re = rs.enumerateRecords(null, null, true);
    } catch (Exception e) {mainForm.append("ex 1");}
    re.rebuild();
    int i = 0;
    while (re.hasNextElement()) {
    i++;
    try {
    inBytes = new ByteArrayInputStream(rs.getRecord(re.nextRecordId()));
    } catch (Exception e) {mainForm.append("ex 2");}
    inStream = new DataInputStream(inBytes);
    try {
    recordValues[i] = inStream.readUTF();
    mainForm.append(i + ": " + recordValues[i]);
    } catch (Exception e) {mainForm.append("ex: " + e);} // and here the exception occurs
    }
    }
    }

    void add_record() {
    if (rs == null) mainForm.append("No record store is open."); else {
    if (commandLine.size() == 0) mainForm.append("You must type the value of the entry."); else {
    try {
    outStream.writeUTF(commandLine.getString());
    } catch (Exception e) {}
    bytes = outBytes.toByteArray();
    try {
    rs.addRecord(bytes, 0, bytes.length);
    } catch (Exception e) {}
    mainForm.append("Record added.");
    }
    }
    }

    void clean_memory() {
    if (rs != null) try {rs.closeRecordStore();rs = null;} catch (Exception e) {}
    recordStoreNames = RecordStore.listRecordStores();
    for (int i = 0; i < recordStoreNames.length; i++) try {
    RecordStore.deleteRecordStore(recordStoreNames[i]);
    } catch (Exception e) {}
    mainForm.append("Memory cleaned.");
    }

    void info() { (...) }


    }

    ------------------------------>>>>>>

    And that's all I hope You haven't fallen asleep and will try to help me. :>

  2. #2
    Registered User
    Join Date
    Mar 2004
    Location
    Europe, Poland, Słupsk
    Posts
    5

    PROBLEM SOLVED

    Hi!

    I've just solved the problem.

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    6

    Re: NullPointerOperationException while trying to readUTF()

    Could you explain how you solved the problem. I'm having the same trouble.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    2

    Re: PROBLEM SOLVED

    hi i'm also facing the same problem.my codings work fine until i read the string using readUTF()..
    please help me..what to do to rectify this.my mail is malathyn.27@gmail.com

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    4,105

    Re: PROBLEM SOLVED

    malathy, please, let us continue in your original post …

Posting Permissions

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