Issue with (NSDictionnary).remove()

30 views
Skip to first unread message

Julian GUERIN

unread,
Oct 8, 2012, 12:59:05 PM10/8/12
to plist-...@googlegroups.com
Hi everyone,

I using dd-plist to parse a plist in which I'd like delete some keys. The structure is quite complicated, here's a sample :

<dict>
      (...)
      <key>tabs</key>
      <dict>
            (...)
            <key>tabs</key>
            <array>
                  (...)
                  <dict>
                       (...)
                       <key>url-bag-key</key>      // I want to delete this key !
                       <string>some string</string>
                  </dict>
            </array>
      </dict>
</dict>


Here's my code : 

46.        NSDictionary footer = (NSDictionary)PropertyListParser.parse(body.getBytes("UTF-8"));
47.  
48.        for( int i = 0; i <= 4 ;i++){
49.           ((NSDictionary)((NSArray)((NSDictionary)footer.objectForKey("tabs")).objectForKey("tabs")).objectAtIndex(i)).remove("url-bag-key");        // <=== this is the line which triggers the exception
50.            ((NSDictionary)((NSArray)((NSDictionary)footer.objectForKey("tabs")).objectForKey("tabs")).objectAtIndex(i)).put("url","http://someurl.com");
51.            ((NSDictionary)((NSArray)((NSDictionary)footer.objectForKey("tabs")).objectForKey("tabs")).objectAtIndex(i)).put("active-tab",1);
52.        }    


And here's the error that i get :
     Error (233) in script : serverscripts.nativejava.Footer_resp.main(Footer_resp.java]:49)

Footer is the name of my project, I don't use an IDE because the code is use by an ICAP server wrote in JAVA (Greasyspoon). I have a very knowledge of Java and i'm totally unable to troubleshot the error, Google couldn't help me so I hope that someone will accept to help me.


I've tried a smaller code :
NSDictionary tmp = new NSDictionary();
temp.put("foo", "bar");
temp.remove("foo");

This also triggers the same exception, is there a know issue with .remove()  or am i doing some thing wrong ?

Sorry for my bad english and thank you all,

Julian

Daniel Dreibrodt

unread,
Oct 8, 2012, 1:38:54 PM10/8/12
to plist-...@googlegroups.com
Hello Julian,

the NSDictionary remove method does nothing but call the remove method of the underlying HashMap. And I see no way how any use of that could yield an error.

As the error message you provided does not provide information about the type of error I think the error could (most probably) either be a NullPointerException (no object for key "tabs" exists), IndexOutOfBoundsException (Index i not contained in the NSArray) or an error during casting. Check whether the property list which you parse actually has the format you expect. Or could you maybe get a more detailed error message?

By the way you could improve your code a little bit (less runtime due to less dictionary gets):

NSArray tabArray =((NSArray)((NSDictionary)footer.objectForKey("tabs")).objectForKey("tabs"));
for( int i = 0; i < tabArray.count() ;i++){ //This assumes you want to iterate over the whole array, prevents IndexOutOfBoundsException
            NSDictionary tabDict = (NSDictionary)tabArray.objectAtIndex(i);
            tabDict.remove("url-bag-key");
            tabDict.put("url","http://someurl.com");
            tabDict.put("active-tab",1);
        }  

Regards,
Daniel

Julian GUERIN

unread,
Oct 9, 2012, 11:13:31 AM10/9/12
to plist-...@googlegroups.com
Hi Daniel,

Thank you very much for your help, I can't give you further information about my issue because that's all I've got for debugging :\. Anyway I followed you improvement advices and i avoid the error by putting a wrong value in the key instead of deleting. I know that's not the right way to do it but it will do the job and i'm under time constraints.
My research makes me think that the error comes from my environment, i'm not using an IDE to run the code, it's a server written in Java which dynamically loads the code (and the librairies). But anyway i'm not skilled enough in Java to solve this problem quickly. If by any chance I find a solution to my issue, i'll post it here !

Once again, thank you very much Daniel for help and thanks to the developers for this useful lib !

Julian 

Daniel Dreibrodt

unread,
Oct 9, 2012, 11:43:19 AM10/9/12
to plist-...@googlegroups.com

Hi,

Then i would turn to the developer of the server and let him check for a bug in HashMap.remove.

Regards,
Daniel

Reply all
Reply to author
Forward
0 new messages