Tuesday, March 26, 2013

Setting NumberPicker's setWrapSelectorWheel to true generates exception

Today while working with an Android project, I was puzzled when the following code generated an exception:

// weeks
nums = new String[5];
for (int i = 0; i < nums.length; i++)
nums[i] = Integer.toString(i);

npWeeks.setMinValue(0);
npWeeks.setMaxValue(4);
npWeeks.setWrapSelectorWheel(false);
npWeeks.setDisplayedValues(nums);
npWeeks.setValue(0);

The exception that generated was:

java.lang.IllegalStateException: Range less than selector items count

The following code which behaves exactly the same way, did not throw any exception:
// days
nums = new String[30];
for (int i = 0; i < nums.length; i++)
nums[i] = Integer.toString(i+1);

npDays.setMinValue(1);
npDays.setMaxValue(30);
npDays.setWrapSelectorWheel(true);
npDays.setDisplayedValues(nums);
npDays.setValue(1);

So after digging online, I found the catch: DO NOT SET .setWrapSelectorWheel to 'true' if you have less than 5 items. The mininum > maximum values should be greater than that for this feature to work as NumberPicker widget shows 5 items at a time. When it does not find enough items to wrap, it generates an exception. Happy Coding!

For more information, see this link.


Wednesday, February 13, 2013

Generate a list of installed programs on Windows 7

I wanted to upgrade to Windows 7 Professional and needed a list of installed programs as backup. This was a precautionary step in case I need for format C:\ drive when upgrading. I wanted a list of programs in a text file which I can refer to.

How to do it

Follow these simple steps to generate a list of commands:
  1. Open 'Command Prompt' from All Programs>Accessories> Command Prompt
  2. Right click and run as administrator
  3. Type the following command: wmic 
  4. You will have a prompt wmic:root\cli>
  5. Type /output:C:\MyInstalledPrograms.txt product
Note: This would generate a list with a lot of details which you might not be interested in such as Help Telephone number of the installed program, Install State, package name, package location, package cache,etc.


Generate a list with only name and version
Follow the same steps as above except for Step 5, type this command:
/output:C:\MyInstalledProgramsNames.txt product get name,version

If you found this useful, please note you can look up the Windows Management Instrument Command line tool for more powerful features. 

Hope this helps! 


Wednesday, January 9, 2013

Datasource not updated from context.xml in Tomcat

Today while working on a project I encountered an interesting problem. The project I am working on is basically a web application which behaves as a server for a J2ME mobile applicaton and consists of a bunch of application servlets that handle requests from a mobile application. The database is MySQL 5.1 and I am using plain and simple JDBC, no OR mapping and fancy stuff like Hibernate. My application's context.xml is the place where I store connections looks like this:


<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/ABCSchool">

<!-- Development Environment -->
<Resource name="jdbc/myDB" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="myproject" password="mypassword"    driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://remoteserver:3306/projectdb". />
  
</Context>

The context.xml file is located under Configuration Files in a Netbeans Web Application project. After the project is built and cleaned, the WAR file produced would contain 'context.xml' in META-INF folder in the root directory of your project. When I deployed the application I realized that I have given the wrong database since the mobile application could not connect. I tried the following, all of which DID NOT work:
  1. Changed the Resource tag in the context.xml file after stopping the Tomcat sever 
  2. Modified context.xml after stopping Tomcat and restarting it
  3. Removed WAR file and deleted application directory. Deployed a fresh copy of my application with the correct database URL in the context.xml but it still did not work!
  4. Cleaned the contents of the 'work' directory and repeated step 2 & 3. No luck.
Solution    
Turned out I was editing the wrong file (thanks to Mark Thomas's reply here). Tomcat copies the context.xml file from the META-INF folder and copies to its 'conf' folder. You can find it under:

CATALINA_BASE/conf/<enginename>/<hostname>/<appname>.xml 

(usually CATALINA_BASE/conf/Catalina/localhost)

My CATALINA_BASE if for instance /var/lib/tomcat6. Every time a project is deployed for the first time, Tomcat copies the context.xml to an XML file named after the application's name eg. ProjectABCSchool's context.xml would be copied to a file called ProjectABCSchool.xml in the 'conf' folder.

Hope this helps anyone stuck with the similar problem!