1

i have an xml file with attributes like this:

  <folder name = 'somename' description = ''/>

i want to display the description attribute as 'null' but it force closes and throws a FATAL Exception main in the LogCat.

i have this code below at the startElement() method

   if (localName.equalsIgnoreCase("folder")) {
      /** Get attribute value */
      if(attributes.getValue("description")== "null"){
         parseList.setFolderdesc(null);
      }else{
         String desc = attributes.getValue("description");                   
         parseList.setFolderdesc(desc);
      } 

i tried this code but no luck... how will i solve this without changing my xml file?

6
  • i used SaxParser here from the example at androidpeople.com Commented Dec 12, 2011 at 6:53
  • your problem is not detailed enough. what are you trying to do and why when does it force close? Commented Dec 12, 2011 at 7:03
  • so it force close on parseList.setFolderdesc(null); ? Commented Dec 12, 2011 at 7:09
  • that because it cant be set to null object.. just use something different then .. for example set a Boolean to false and then on the display part - if its false then don't display .. or some other clever maneuver. this force close is totally legit and doesn't relate to your XML .. Commented Dec 12, 2011 at 7:16
  • It would help if you told us what exception is being thrown ... as reported by LogCat. Commented Dec 12, 2011 at 7:18

2 Answers 2

1

try with the following code

String desc = null;
        try{
        desc = attributes.getValue("description");
        if((desc == null) || (desc.length()<=0)){
            desc = null;    
        }
        }catch(Exception ex){
            desc = null;
        }
if(parseList != null){
parseList.setFolderdesc(desc);  
}
Sign up to request clarification or add additional context in comments.

5 Comments

Uggh. Your belt-and-braces exception handling is going to obscure any bugs. Catching Exception is evil ... just don't do it!!
@nhqdualcagony - it might "work", but it is extremely bad practice to catch Exception. For instance, if attributes itself was null due to some bug in your code, then Sunil's code would make it difficult for you to find that bug.
@Stephen, thanks for your effort. I know it is not the good to catch Exception But it is not bad always. in some case eg if you want to so generic error message to the user irrespective of any type of Exception i that case you have to handle catch.
@All, Also I feel Handle Catch in outermost layer (calling environment) if possible.
@SunilKumarSahoo - I'll grant you that case. But your example is not that case.
1

This code doesn't do what you expect:

  if (attributes.getValue("description") == "null") {

You are comparing the attribute value with the String "null" not with a java null. (And you are testing strings the unsafe way too! Strings should be tested for equality using String.equals() not the == operator.)

That test should be written as follows:

  if (attributes.getValue("description") == null) {

or better still:

  if (attributes.getValue("description") == null || 
      attributes.getValue("description").isEmpty()) {

(I'm not sure whether this will fix you problem, because I don't understand your problem description.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.