Finding the size of a char array in Java

You can use b.length to find out how many characters there are. This is only the number of characters, not indexing, so if you iterate over it with a for loop, remember to write it like this: Note the < (not a <=). It’s also important to note that since the array isn’t a class, .length isn’t a function, so you shouldn’t have … Read more

“Field can be converted to a local variable” message appearing when setting Android ActionBar colour

What the warning is telling you is that actionBarColor shouldn’t be a global variable (i.e. a field), because it’s only used in one method (onCreate). This is good advice: you should always minimize the scope of your variables, because it improves readability and reduces possibilities for programming errors. To get rid of the warning, fix the problem … Read more

break statement in “if else” – java

Because your else isn’t attached to anything. The if without braces only encompasses the single statement that immediately follows it. Not using braces is generally viewed as a bad practice because it can lead to the exact problems you encountered. In addition, using a switch here would make more sense. Note that instead of an infinite for loop I used a while(boolean), making it … Read more

this: Cannot use this in static context

See, “this” keyword refers to current object due to which method is under exceution. As, you cannot call static method using instance of class. That is why, “this” can’t be used in the above example in a static method as it is trying to print current instance wich is not at all created. So, I … Read more

How to pass an object from one activity to another on Android

One option could be letting your custom class implement the Serializable interface and then you can pass object instances in the intent extra using the putExtra(Serializable..) variant of the Intent#putExtra() method. Pseudocode: Note: Make sure each nested class of your main custom class has implemented Serializable interface to avoid any serialization exceptions. For example:

How do I test a class that has private methods, fields or inner classes?

Update: Some 10 years later perhaps the best way to test a private method, or any inaccessible member, is via @Jailbreak from the Manifold framework. This way your code remains type-safe and readable. No design compromises, no overexposing methods and fields for the sake of tests. If you have somewhat of a legacy Java application, and you’re not allowed to change … Read more

Unreachable code in eclipse

The first message, Exception in thread “main” java.lang.Error: Unresolved compilation problem:means your code does not compile. You need to identify the error and fix it. Modern IDEs e.g. Eclipse, Netbeans, etc flag compile errors. They can help you to quickly identify the source. The second error: means that the code at line 24 will never be … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)