6 Replies Latest reply: Aug 3, 2010 3:36 AM by 782681 RSS

    how to solve this? java.lang.IllegalArgumentException problem

    843851
      The midlet compliled successfully..
      Once run,
      I enter 3 different records...
      then after when I 'VIEW' for example I enter recordID: 1..
      by right, all the details about recordId : 1 would be listed out...somehow, this error pops up.
      java.lang.IllegalArgumentException

      /*
      * To change this template, choose Tools | Templates
      * and open the template in the editor.
      */
      
      import javax.microedition.midlet.*;
      import javax.microedition.lcdui.*;
      import javax.microedition.rms.*;
      
      /**
      * @author RyanLCC
      */
      public class cdSeller extends MIDlet implements CommandListener{
      
      private Display display;
      private Form form;
      private Command add, view, update, delete, exit;
      private TextField rcdId, title, quantity, price, profit, director, publish, actors;
      private RecordStore rs;
      private Alert alert = new Alert("New Data Added !!!");
      private Alert alert1 = new Alert("Database Upated!!!");
      private Alert alert2 = new Alert("Record Deleted!!!");
      private Alert alert3 = new Alert("Looking Data!!!");
      
      public cdSeller()throws RecordStoreException{
      
      display = Display.getDisplay(this);
      exit = new Command("Exit", Command.EXIT, 1);
      add = new Command("Add",Command.SCREEN,2);
      update = new Command("Update",Command.SCREEN,2);
      delete = new Command("Delete",Command.SCREEN,2);
      view = new Command("View",Command.SCREEN,2);
      
      rcdId= new TextField("Record ID :","",5,TextField.NUMERIC);
      title= new TextField("Title :","",11,TextField.ANY);
      quantity= new TextField("Quantity :","",8,TextField.NUMERIC);
      price= new TextField("Retail price :","",8,TextField.ANY);
      profit= new TextField("Profit margin:","",8,TextField.ANY);
      director= new TextField("Director :","",11,TextField.ANY);
      publish= new TextField("Publisher :","",11,TextField.ANY);
      actors= new TextField("Actors :","",11,TextField.ANY);
      
      rs = RecordStore.openRecordStore("My CD Datbase Directory", true);
      form = new Form("My CD Database");
      
      form.append(rcdId);
      form.append(title);
      form.append(quantity);
      form.append(price);
      form.append(profit);
      form.append(director);
      form.append(publish);
      form.append(actors);
      
      form.addCommand(exit);
      form.addCommand(add);
      form.addCommand(update);
      form.addCommand(delete);
      form.addCommand(view);
      form.setCommandListener(this);
      }
      public void startApp() {
      display.setCurrent(form);
      }
      
      public void pauseApp() {
      }
      
      public void destroyApp(boolean unconditional) {
      try {
      rs.closeRecordStore();
      } catch (RecordStoreException ex) {
      ex.printStackTrace();
      }
      }
      
      public void commandAction(Command c, Displayable d) {
      
      alert.setTimeout(3000);
      alert1.setTimeout(3000);
      String str;
      byte bytes[];
      int recordID;
      
      try{
      if(c==add){
      str = title.getString()+":"+quantity.getString()+
      ":"+price.getString()+":" +profit.getString()+
      ":"+director.getString()+":"+publish.getString ()+
      ":"+actors.getString();
      bytes=str.getBytes();
      recordID = rs.addRecord(bytes, 0, bytes.length);
      
      System.out.println("Record of ID:"+recordID+" is added");
      Display.getDisplay(this).setCurrent(alert);
      
      }else if(c==update){
      recordID = Integer.parseInt(rcdId.getString());
      str = title.getString()+":"+quantity.getString()+
      ":"+price.getString()+":" +profit.getString()+
      ":"+director.getString()+":"+publish.getString ()+
      ":"+actors.getString();
      bytes=str.getBytes();
      rs.setRecord(recordID, bytes, 0, bytes.length);
      Display.getDisplay(this).setCurrent(alert1);
      
      }else if(c == delete){
      recordID = Integer.parseInt(rcdId.getString());
      rs.deleteRecord(recordID);
      Display.getDisplay(this).setCurrent(alert2);
      
      }else if(c == view ){
      recordID = Integer.parseInt(rcdId.getString());
      bytes = new byte[rs.getRecordSize(recordID)];
      rs.getRecord(recordID,bytes,0);
      String str1 = new String(bytes);
      int index = str1.indexOf(":");
      title.setString(str1.substring(0));
      quantity.setString(str1.substring(1));
      price.setString(str1.substring(2));
      profit.setString(str1.substring(3));
      director.setString(str1.substring(4));
      publish.setString(str1.substring(5));
      actors.setString(str1.substring(6));
      
      }else if( c == exit){
      destroyApp(true);
      notifyDestroyed();
      }
      
      }catch(Exception e){
      e.printStackTrace();
      }
      
      }
      } 
        • 1. Re: how to solve this? java.lang.IllegalArgumentException problem
          782681
          what is the output of stack trace?

          // your code is quite hard to read: no indentation, non-standard class name "cdSeller" instead of CdSeller. It is also naturally prone to bugs because methods are too long: try [extract method pattern|http://www.refactoring.com/catalog/extractMethod.html|explanation] to improve it

          -----

          PS. update, delete and view commands expect rdcId to contain relevant record id - try to log its actual content prior to invoking
          Integer.parseInt(rcdId.getString())
          Edited by: gnat on Aug 2, 2010 9:32 AM
          • 2. Re: how to solve this? java.lang.IllegalArgumentException problem
            843851
            Starting emulator in execution mode
            Installing suite from: http://127.0.0.1:55530/RecordStore.jad
            Record of ID:1 is added
            Record of ID:2 is added
            Record of ID:3 is added

            java.lang.IllegalArgumentException
            at javax.microedition.lcdui.TextField.setCharsImpl(), bci=79
            at javax.microedition.lcdui.TextField.setString(), bci=37
            at CdSeller.commandAction(CdSeller.java:118)
            at javax.microedition.lcdui.Display$ChameleonTunnel.callScreenListener(), bci=46
            at com.sun.midp.chameleon.layers.SoftButtonLayer.processCommand(), bci=74
            at com.sun.midp.chameleon.layers.SoftButtonLayer.commandSelected(), bci=11
            at com.sun.midp.chameleon.layers.MenuLayer.pointerInput(), bci=170
            at com.sun.midp.chameleon.CWindow.pointerInput(), bci=76
            at javax.microedition.lcdui.Display$DisplayEventConsumerImpl.handlePointerEvent(), bci=19
            at com.sun.midp.lcdui.DisplayEventListener.process(), bci=296
            at com.sun.midp.events.EventQueue.run(), bci=179
            at java.lang.Thread.run(Thread.java:619)

            I had added three records...and then, i try to 'View' recordID: 1...and such error message pop up..

            Thx..
            • 3. Re: how to solve this? java.lang.IllegalArgumentException problem
              782681
              ryanlcc wrote:
              ...
              at javax.microedition.lcdui.TextField.setString(), bci=37
              at CdSeller.commandAction(CdSeller.java:118)
              ...
              per above, you can see that exception is thrown at line 118 in CdSeller.java (did you rename the class since your original post?). At this line, setString() is invoked for some TextField, causing IllegalArgumentException due to parameter being, well, illegal for that text field.

              It's impossible to say for sure without seeing what code is at line 118, but given that quantity textfield has NUMERIC type, I would test what happens if I change it to ANY.
              • 4. Re: how to solve this? java.lang.IllegalArgumentException problem
                843851
                 *To change this template, choose Tools | Templates*
                 and open the template in the editor.
                */*
                
                *import javax.microedition.midlet.*;
                import javax.microedition.lcdui.*;*
                *import javax.microedition.rms.*;
                
                /
                 *@author RyanLCC*
                /
                public class CdSeller extends MIDlet implements CommandListener{
                
                    private Display display;
                    private Form form;
                    private Command add, view, update, delete, exit;
                    private TextField rcdId, title, quantity, price, profit, director, publish, actors;
                    private RecordStore rs;
                    private Alert alert = new Alert("New Data Added !!!");
                    private Alert alert1 = new Alert("Database Upated!!!");
                    private Alert alert2 = new Alert("Record Deleted!!!");
                    private Alert alert3 = new Alert("Looking Data!!!");
                
                    public CdSeller()throws RecordStoreException{
                
                    display = Display.getDisplay(this);
                    exit = new Command("Exit", Command.EXIT, 1);
                    add = new Command("Add",Command.SCREEN,2);
                    update = new Command("Update",Command.SCREEN,2);
                    delete = new Command("Delete",Command.SCREEN,2);
                    view = new Command("View",Command.SCREEN,2);
                
                    rcdId= new TextField("Record ID     :","",5,TextField.NUMERIC);
                    title= new TextField("Title         :","",11,TextField.ANY);
                    quantity= new TextField("Quantity   :","",8,TextField.ANY);
                    price= new TextField("Retail price  :","",8,TextField.ANY);
                    profit= new TextField("Profit margin:","",8,TextField.ANY);
                    director= new TextField("Director   :","",11,TextField.ANY);
                    publish= new TextField("Publisher   :","",11,TextField.ANY);
                    actors= new TextField("Actors       :","",11,TextField.ANY);
                
                    rs = RecordStore.openRecordStore("My CD Datbase Directory", true);
                    form = new Form("My CD Database");
                
                    form.append(rcdId);
                    form.append(title);
                    form.append(quantity);
                    form.append(price);
                    form.append(profit);
                    form.append(director);
                    form.append(publish);
                    form.append(actors);
                
                    form.addCommand(exit);
                    form.addCommand(add);
                    form.addCommand(update);
                    form.addCommand(delete);
                    form.addCommand(view);
                    form.setCommandListener(this);
                    }
                    public void startApp() {
                        display.setCurrent(form);
                    }
                
                    public void pauseApp() {
                    }
                
                    public void destroyApp(boolean unconditional) {
                        try {
                            rs.closeRecordStore();
                        } catch (RecordStoreException ex) {
                            ex.printStackTrace();
                        }
                    }
                
                    public void commandAction(Command c, Displayable d) {
                
                        alert.setTimeout(3000);
                        alert1.setTimeout(3000);
                        String str;
                        byte bytes[];
                        int recordID;
                
                        try{
                            if(c==add){
                                str = title.getString()+":"+quantity.getString()+
                                      ":"+price.getString()+":" +profit.getString()+
                                      ":"+director.getString()+":"+publish.getString()+
                                      ":"+actors.getString();+
                +                bytes=str.getBytes();+
                +                recordID = rs.addRecord(bytes, 0, bytes.length);+
                
                +                System.out.println("Record of ID:"+recordID+" is added");
                                Display.getDisplay(this).setCurrent(alert);
                
                            }else if(c==update){
                                recordID = Integer.parseInt(rcdId.getString());
                                str = title.getString()+":"+quantity.getString()+
                                      ":"+price.getString()+":" +profit.getString()+
                                      ":"+director.getString()+":"+publish.getString()+
                                      ":"+actors.getString();+
                +                bytes=str.getBytes();+
                +                rs.setRecord(recordID, bytes, 0, bytes.length);+
                +                System.out.println("Record of ID:"+recordID+" is updated");
                                Display.getDisplay(this).setCurrent(alert1);
                
                            }else if(c == delete){
                                recordID = Integer.parseInt(rcdId.getString());
                                rs.deleteRecord(recordID);
                                System.out.println("Record of ID:"+recordID+" is deleted");
                                Display.getDisplay(this).setCurrent(alert2);
                
                            }else if(c == view ){
                                recordID = Integer.parseInt(rcdId.getString());
                                bytes = new byte[rs.getRecordSize(recordID)];
                                rs.getRecord(recordID,bytes,0);
                                String str1 = new String(bytes);
                                int index = str1.indexOf(":");
                                title.setString(str1.substring(0));
                                quantity.setString(str1.substring(1));
                                price.setString(str1.substring(2));
                                profit.setString(str1.substring(3));
                                director.setString(str1.substring(4));
                                publish.setString(str1.substring(5));
                                actors.setString(str1.substring(6));
                
                        }else if( c == exit){
                            destroyApp(true);
                            notifyDestroyed();
                        }
                
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                
                    }
                }
                Starting emulator in execution mode
                Installing suite from: http://127.0.0.1:59543/RecordStore.jad
                Record of ID:1 is added
                Record of ID:2 is added
                java.lang.IllegalArgumentException
                at javax.microedition.lcdui.TextField.setCharsImpl(), bci=79
                at javax.microedition.lcdui.TextField.setString(), bci=37
                at CdSeller.commandAction(CdSeller.java:120)
                at javax.microedition.lcdui.Display$ChameleonTunnel.callScreenListener(), bci=46
                at com.sun.midp.chameleon.layers.SoftButtonLayer.processCommand(), bci=74
                at com.sun.midp.chameleon.layers.SoftButtonLayer.commandSelected(), bci=11
                at com.sun.midp.chameleon.layers.MenuLayer.pointerInput(), bci=170
                at com.sun.midp.chameleon.CWindow.pointerInput(), bci=76
                at javax.microedition.lcdui.Display$DisplayEventConsumerImpl.handlePointerEvent(), bci=19
                at com.sun.midp.lcdui.DisplayEventListener.process(), bci=296
                at com.sun.midp.events.EventQueue.run(), bci=179
                at java.lang.Thread.run(Thread.java:619)
                javacall_lifecycle_state_changed() lifecycle: event is JAVACALL_LIFECYCLE_MIDLET_SHUTDOWNstatus is JAVACALL_OK

                I had tired to change the quantity= new TextField("Quantity   :","",8,TextField.ANY);+ but still giving me the same problem...
                Here again..Thankx alot..
                • 5. Re: how to solve this? java.lang.IllegalArgumentException problem
                  843851
                  114| }else if(c == view ){
                           115|        recordID = Integer.parseInt(rcdId.getString());
                           116|        bytes = new byte[rs.getRecordSize(recordID)];
                           117|      rs.getRecord(recordID,bytes,0);
                           118|       String str1 = new String(bytes);
                           119|       int index = str1.indexOf(":");
                           120|       title.setString(str1.substring(0));
                           121|       quantity.setString(str1.substring(1));
                           122|       price.setString(str1.substring(2));
                           123|      profit.setString(str1.substring(3));
                           124|       director.setString(str1.substring(4));
                           125|       publish.setString(str1.substring(5));
                           126|       actors.setString(str1.substring(6));
                  • 6. Re: how to solve this? java.lang.IllegalArgumentException problem
                    782681
                    I see. did you check API docs for TextField.setString method? Given that your input constraint is ANY, the only other reason for this exception, per API docs is that string is too long for the TextField capacity (you set the capacity in constructor, eg for 'title' it's 11 chars).

                    You can check if the parameter is OK by printing it prior to passing to setString:
                    //...
                      // was: title.setString(str1.substring(0));
                      // now:
                      setString(title, str1.substring(0));
                      // above setString is extracted method, see below...
                    //...
                    private void setString(TextField target, String newContents) {
                      System.out.println("new contents: " + newContents);
                      target.setString(newContents);
                    }
                    //...
                    Edited by: gnat on Aug 3, 2010 8:36 AM