Friday, June 23, 2017

java: Reflection with array argument - java.lang.IllegalArgumentException: wrong number of arguments

For example, you have am arrayPrint method, which takes a String[] as its argument, in the Invoked class.

public class Invoked {
      public void arrayPrint(String[] items) {
            int i = 1;
            for (String item : items) {
                  System.out.println("Item "+ i ++ ": " + item);
            }
      }
}

Now, you want to call this arrayPrint method via reflection in the Invoker class.

public class Invoker {
      public static void main(String[] args) {
            String[] items = new String[3];
            items[0] = "Hello Dear!";
            items[1] = "So glad to see you.";
            items[2] = "How have you been?"

           Class invokedClass = Class.forName("Invoked");
           Method printMethod = invokedClass.getMethod("arrayPrint", String[].class);

           //This line will throw an Exception
           printMethod.invoke(new Invoked(), items);
     }
}

When it is executed, it throws the below exception.

Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at test.Invoker.main(Invoker.java:29)

This exception occurs because the invoke method takes each element in the items array as a separate argument, therefore, in this case, there are three arguments in total. But the arrayPrint method only takes one argument.

To fix it:
Change the line above that gives the exception to this:

      printMethod.invoke(new Invoked(), new Object[] {items});

-----------------------------------------------------------------------------------------------------------------
Watch the blessing and loving online channel: SupremeMasterTV live




If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book for free here.


Wednesday, June 21, 2017

[Solved] How to make a JOptionPane scrollable?

Here are  the steps for making a JOptionPane scrollable.

1. Create a JTextArea with fixed rows and columns to hold your text information.

      String message = <my long message to display>

      JTextArea textArea = new JTextArea(messate, <rows>,  <columns>);
     
      It is very critical to set the rows and columns. They determine the size of the view in the JOptionPane.

2. Create a JScrollPane which has the JTextArea as the view port.

      JScrollPane sp = new JScrollPane(textArea);
      sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

3. Create the JOptionPane using the JScrollPane as its displaying component

      JOptionPane.showMessageDialog(null, sp);

---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.
     

Thursday, June 15, 2017

[Solved] ORA-12519, TNS:no appropriate service handler found

Unpredictably, one instance of your program gives the exception below, while most of the time the program works just fine.

java.sql.SQLException: Listener refused the connection with the following error:
ORA-12519, TNS:no appropriate service handler found
                at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:673)
                at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:715)
                at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:385)
                at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:30)
                at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:564)
                at java.sql.DriverManager.getConnection(Unknown Source)

                at java.sql.DriverManager.getConnection(Unknown Source)

This exception is usually caused by that the number of database connections exceeded the number of connections the listener can handle. Check through your code to make sure that all the open connections are necessary and all of them are closed after their jobs are done. Especially, when you use a connection in a loop, make sure you don't create a connection in each iteration, unless you have to, then close it immediately once it is not needed.

Quick fixes to this problem:

1. Restart the database. Most of the time, this is sufficient to solve the problem.

2. If the problem keeps coming back after you tried solution 1 above, increase the database System processes so that the listener is set to handle more connections.

Log on to your database as the database administrator.

SQL>alter system set processes=300 scope=spfile;
SQL>alter system set open_cursors=500;
SQL>shutdown immediate
SQL>startup

3. If you do not want to increase the System processes, take the steps below to solve the problem.

SQL>select count(*) from v$process;
SQL>select count(*) from v$session;

If the count you get is close to 150, the default maximum number of processes allowed, run the following SQL to find out who is doing what.

SQL> SELECT sn.process, sn.status, sn.username, sn.schemaname, sql.sql_text
           FROM v$session sn, v$sql sql
           WHERE sql.sql_id(+) = sn.sql_id and sn.type='USER' and sql.elapsed_time is not null                       ORDER BY sql.elapsed_time desc;


---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Tuesday, June 13, 2017

[Solved] String split does not pick up the trailing /ending empty string

For example, you have a string:
      String str = "You are,indeed,very,very beautiful!, ......,,,,";

Then you use the split method to get all the tokens.

      String [] tokens = str.split(",");

You expect this: {"You are", "indeed", "very",  "very beautiful!", " ......", "", "," "", ""}
But you get this: {"You are", "indeed", "very",  "very beautiful!, " ......"}

The String split("<delimiter>") method, removes the trailing empty strings from the result by default. To include these empty trailing strings in the result array, you need to use its overloaded form, split("<delimiter>", <a negative integer>) or split("<delimiter>", Integer.MAX_VALUE).

      String[] tokens = str.split(",", -1);

will give you the array you expect.

---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Thursday, June 1, 2017

[Solved] Oracle: Copy a column into another column within the same table

If for some reason you want to add a new column and copy the data from an old column into the new column instead of resetting the column name or data type of the old column, you can do it by executing a sql statement.

A. Copy all the values from an old column into a new column.

Update tableName set oldColumn = newColumn;

B. Copy all the values from several old columns into several new columns

Update tableName set oldColumn1 = newColumn1, oldColumn2 = new column2, oldColumn3 = newColumn3 . . . . . . ;

C. Copy only these row having a specific value in a column;

Update tableName set oldColumn = newColumn where specificColumn  = aValue;

The specificColumn can be the oldColumn itself.

---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.