Click here to Skip to main content
15,915,019 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Pro Cache Usage Pin
Member 41627848-Jun-12 11:52
Member 41627848-Jun-12 11:52 
GeneralArrayList.Contains() isn't bool? Pin
Ankush Bansal6-Jun-12 1:36
Ankush Bansal6-Jun-12 1:36 
GeneralRe: ArrayList.Contains() isn't bool? Pin
OriginalGriff6-Jun-12 2:02
mveOriginalGriff6-Jun-12 2:02 
GeneralRe: ArrayList.Contains() isn't bool? Pin
Ankush Bansal6-Jun-12 2:53
Ankush Bansal6-Jun-12 2:53 
GeneralRe: ArrayList.Contains() isn't bool? Pin
OriginalGriff6-Jun-12 3:34
mveOriginalGriff6-Jun-12 3:34 
GeneralRe: ArrayList.Contains() isn't bool? Pin
Member 20530067-Jun-12 6:02
Member 20530067-Jun-12 6:02 
GeneralRe: ArrayList.Contains() isn't bool? Pin
Bernhard Hiller7-Jun-12 23:19
Bernhard Hiller7-Jun-12 23:19 
GeneralScope fail Pin
Andrei Straut5-Jun-12 22:56
Andrei Straut5-Jun-12 22:56 
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

Java
public class CategoryProducts extends ListActivity {
	private RangeSeekBar<Integer> 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<Integer> seekBar = new RangeSeekBar<Integer>(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

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

GeneralRe: Scope fail Pin
Ankush Bansal6-Jun-12 1:40
Ankush Bansal6-Jun-12 1:40 
GeneralRe: Scope fail Pin
OriginalGriff6-Jun-12 2:07
mveOriginalGriff6-Jun-12 2:07 
GeneralRe: Scope fail Pin
Andrei Straut6-Jun-12 2:12
Andrei Straut6-Jun-12 2:12 
GeneralRe: Scope fail Pin
Ankush Bansal6-Jun-12 2:52
Ankush Bansal6-Jun-12 2:52 
GeneralRe: Scope fail Pin
Andrei Straut6-Jun-12 3:15
Andrei Straut6-Jun-12 3:15 
GeneralMessage Closed Pin
7-Jun-12 12:40
_beauw_7-Jun-12 12:40 
GeneralRe: Scope fail Pin
OriginalGriff7-Jun-12 21:26
mveOriginalGriff7-Jun-12 21:26 
GeneralModel lost in space Pin
dakovinc4-Jun-12 21:46
dakovinc4-Jun-12 21:46 
JokeRe: Model lost in space PinPopular
Sentenryu5-Jun-12 0:48
Sentenryu5-Jun-12 0:48 
GeneralRe: Model lost in space Pin
Pankaj Nikam5-Jun-12 6:51
professionalPankaj Nikam5-Jun-12 6:51 
GeneralRe: Model lost in space Pin
ekolis5-Jun-12 11:55
ekolis5-Jun-12 11:55 
GeneralRe: Model lost in space Pin
dakovinc5-Jun-12 20:07
dakovinc5-Jun-12 20:07 
GeneralRe: Model lost in space Pin
CDP18025-Jun-12 20:13
CDP18025-Jun-12 20:13 
GeneralRe: Model lost in space Pin
egenis5-Jun-12 21:00
egenis5-Jun-12 21:00 
GeneralRe: Model lost in space Pin
dakovinc5-Jun-12 23:28
dakovinc5-Jun-12 23:28 
GeneralRe: Model lost in space Pin
RCoate6-Jun-12 16:54
RCoate6-Jun-12 16:54 
GeneralNesting level: 9001 PinPopular
harold aptroot31-May-12 7:12
harold aptroot31-May-12 7:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.