Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Scope fail

Scope fail

Scheduled Pinned Locked Moved The Weird and The Wonderful
androidlearning
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andrei Straut
    wrote on last edited by
    #1

    Hey guys, i'm a software developer from Romania, and I've been a lurker here for quite a while. Currently, i'm working on an Android project, and, well, this morning I've caused a wtf fail. Thankfully, I realized it immediately (i.e. after the first NullPointerException) and quickly fixed it. However, I could not resist posting this here, as it's a beginner's mistake, and I shouldn't have done it in the first place (shame on me). Variable and method names changed, and the original class was much, much larger

    public class CategoryProducts extends ListActivity {
    private RangeSeekBar seekBar;

    /\*Dummy initializers that receive their normal values in the constructor\*/
    private int minPrice = 0;
    private int maxPrice = 100000;
    
    private void initFilterView() {	
    	if(seekBar == null) {
    		RangeSeekBar seekBar = new RangeSeekBar(minPrice, maxPrice);
    		seekBar.setOnRangeSeekBarChangeListener(new PriceLimitListener());
    	}
    	
    	((LinearLayout) filterView.findViewById(R.id.priceSeekBar)).addView(seekBar);
    }	
    

    }

    The explanation (if you haven't spot it already) is that seekBar gets declared as local in the "if" of the initFilterView method, and visible only in that scope. The local "seekBar" and the global "seekBar" are in fact 2 different vars, and nobody guarantees that the global "seekBar" will not be null (which happens in this case). The correct code was

    ...(same as above)
    if(seekBar == null) {
    seekBar = new RangeSeekBar(minPrice, maxPrice);
    seekBar.setOnRangeSeekBarChangeListener(new PriceLimitListener());
    }
    ...(same as above)

    A OriginalGriffO 2 Replies Last reply
    0
    • A Andrei Straut

      Hey guys, i'm a software developer from Romania, and I've been a lurker here for quite a while. Currently, i'm working on an Android project, and, well, this morning I've caused a wtf fail. Thankfully, I realized it immediately (i.e. after the first NullPointerException) and quickly fixed it. However, I could not resist posting this here, as it's a beginner's mistake, and I shouldn't have done it in the first place (shame on me). Variable and method names changed, and the original class was much, much larger

      public class CategoryProducts extends ListActivity {
      private RangeSeekBar seekBar;

      /\*Dummy initializers that receive their normal values in the constructor\*/
      private int minPrice = 0;
      private int maxPrice = 100000;
      
      private void initFilterView() {	
      	if(seekBar == null) {
      		RangeSeekBar seekBar = new RangeSeekBar(minPrice, maxPrice);
      		seekBar.setOnRangeSeekBarChangeListener(new PriceLimitListener());
      	}
      	
      	((LinearLayout) filterView.findViewById(R.id.priceSeekBar)).addView(seekBar);
      }	
      

      }

      The explanation (if you haven't spot it already) is that seekBar gets declared as local in the "if" of the initFilterView method, and visible only in that scope. The local "seekBar" and the global "seekBar" are in fact 2 different vars, and nobody guarantees that the global "seekBar" will not be null (which happens in this case). The correct code was

      ...(same as above)
      if(seekBar == null) {
      seekBar = new RangeSeekBar(minPrice, maxPrice);
      seekBar.setOnRangeSeekBarChangeListener(new PriceLimitListener());
      }
      ...(same as above)

      A Offline
      A Offline
      Ankush Bansal
      wrote on last edited by
      #2

      My vote of 5, to accept your mistake. Newton's unspecified law: No code in world is bug free!

      1 Reply Last reply
      0
      • A Andrei Straut

        Hey guys, i'm a software developer from Romania, and I've been a lurker here for quite a while. Currently, i'm working on an Android project, and, well, this morning I've caused a wtf fail. Thankfully, I realized it immediately (i.e. after the first NullPointerException) and quickly fixed it. However, I could not resist posting this here, as it's a beginner's mistake, and I shouldn't have done it in the first place (shame on me). Variable and method names changed, and the original class was much, much larger

        public class CategoryProducts extends ListActivity {
        private RangeSeekBar seekBar;

        /\*Dummy initializers that receive their normal values in the constructor\*/
        private int minPrice = 0;
        private int maxPrice = 100000;
        
        private void initFilterView() {	
        	if(seekBar == null) {
        		RangeSeekBar seekBar = new RangeSeekBar(minPrice, maxPrice);
        		seekBar.setOnRangeSeekBarChangeListener(new PriceLimitListener());
        	}
        	
        	((LinearLayout) filterView.findViewById(R.id.priceSeekBar)).addView(seekBar);
        }	
        

        }

        The explanation (if you haven't spot it already) is that seekBar gets declared as local in the "if" of the initFilterView method, and visible only in that scope. The local "seekBar" and the global "seekBar" are in fact 2 different vars, and nobody guarantees that the global "seekBar" will not be null (which happens in this case). The correct code was

        ...(same as above)
        if(seekBar == null) {
        seekBar = new RangeSeekBar(minPrice, maxPrice);
        seekBar.setOnRangeSeekBarChangeListener(new PriceLimitListener());
        }
        ...(same as above)

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        Interesting! How did that compile without an error: "A local variable named 'seekBar' cannot be declared in this scope because it would give a different meaning to 'seekBar ', which is already used in a 'parent or current' scope to denote something else" Because it won't for me!

        Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        A _ 2 Replies Last reply
        0
        • OriginalGriffO OriginalGriff

          Interesting! How did that compile without an error: "A local variable named 'seekBar' cannot be declared in this scope because it would give a different meaning to 'seekBar ', which is already used in a 'parent or current' scope to denote something else" Because it won't for me!

          Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

          A Offline
          A Offline
          Andrei Straut
          wrote on last edited by
          #4

          EDIT: I believe you've tried this in VS, I dunno if Eclipse has that sort of stuff :laugh: I can also post pics ;P

          A 1 Reply Last reply
          0
          • A Andrei Straut

            EDIT: I believe you've tried this in VS, I dunno if Eclipse has that sort of stuff :laugh: I can also post pics ;P

            A Offline
            A Offline
            Ankush Bansal
            wrote on last edited by
            #5

            Now I am starting to believe that world is moving to .net, :thumbsup: God save JAVA :laugh:

            A 1 Reply Last reply
            0
            • A Ankush Bansal

              Now I am starting to believe that world is moving to .net, :thumbsup: God save JAVA :laugh:

              A Offline
              A Offline
              Andrei Straut
              wrote on last edited by
              #6

              Yeah, although Java was the first language I've learned and I do still love it with all my heart, after I worked with .NET I've started to understand why people are moving to it. I think VS2010 Pro is a beauty of an IDE. I didn't get 2011 yet, but from what I've read around here, people aren't really satisfied with it :laugh:

              1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                Interesting! How did that compile without an error: "A local variable named 'seekBar' cannot be declared in this scope because it would give a different meaning to 'seekBar ', which is already used in a 'parent or current' scope to denote something else" Because it won't for me!

                Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                _ Offline
                _ Offline
                _beauw_
                wrote on last edited by
                #7

                Most languages other than C# allow this sort of thing. C, C++, and (apparently) Java all do. I'm not sure what the legitimate case for this sort of thing is, though. It seems like a potential trap to me, and little more.

                OriginalGriffO 1 Reply Last reply
                0
                • _ _beauw_

                  Most languages other than C# allow this sort of thing. C, C++, and (apparently) Java all do. I'm not sure what the legitimate case for this sort of thing is, though. It seems like a potential trap to me, and little more.

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  Agreed - variable masking is a bad idea, it leads to some very hard to spot bugs.

                  Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups