0

I am new to JSON programming. I am trying to write a webservice to fetch data from a SQLite table and write the output to a JSON file. I have added the JSON simple jar to my eclipse project. When I add a line of code as shown below,

JSONObject obj = new JSONObject();

it compiles properly. But, when the webservice is invoked, I get an exception at this line as shown below,

Exception: java.lang.reflect.InvocationTargetException Message: java.lang.reflect.InvocationTargetException 

This is my complete java file...

package wtp;

import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

import org.json.simple.JSONObject;
import org.json.simple.JSONArray;

public class CollegeProcessor2 {

    public static String fetchCollegeList2(String input)
    {
        Connection c = null;
        Statement stmt = null;
        String output = null;
        java.util.Date date= new java.util.Date();

        JSONObject collegeList = new JSONObject();     
        JSONObject college = new JSONObject();
        JSONArray list = new JSONArray();       

        try {

          Class.forName("org.sqlite.JDBC");
          c = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Vevin.Kumar\\workspace\\JSONProject\\collegelist.db");
          c.setAutoCommit(false);
          System.out.println("Opened database successfully");

          stmt = c.createStatement();
          ResultSet rs = stmt.executeQuery( "SELECT * FROM COLLEGES WHERE DISTRICT LIKE \"" + input +"\" ;");         

          ResultSetMetaData metaData = rs.getMetaData();
          int numberOfColumns=metaData.getColumnCount();

          while ( rs.next() ) {          
             for(int i = 1; i <= numberOfColumns; i++)
             {
                college.put(String.valueOf(metaData.getColumnName(i)), String.valueOf(rs.getObject(i)));
             }      
             list.add(college);
          } 

          collegeList.put("collegeslist", list);

          rs.close();
          stmt.close();
          c.close();

        } catch ( Exception e ) {
          System.err.println( e.getClass().getName() + ": " + e.getMessage() );
          System.exit(0);
        }

        try{     

           FileWriter file = new FileWriter("c:\\json_outputs\\collegelist_" + date.getTime() +".json");
           file.write(collegeList.toJSONString());
           file.flush();
           file.close();

        } catch ( IOException e ){
           System.err.println( e.getClass().getName() + ": " + e.getMessage() );
           System.exit(0);            
        }

        System.out.println("Operation done successfully");
        output = "c:\\json_outputs\\collegelist_" + date.getTime() +".json";
        return output;
    }   

    /*public static void main (String[] args)
    {
        String output = fetchCollegeList2("CHENNAI");
        System.out.println(output);
    }*/
}

If I run this as a Java application, the JSON file is getting generated properly. If I create a bottom down web service from this and invoke the method from it, I am getting the exception mentioned above. The exception occurs right at the line where the JSONObject is initialized.

Can anyone please help me out on this?

Thanks & Regards, Vevin.

  • Please post your complete logcat – Nabin Sep 17 '14 at 14:06
  • @Nabin - This is the only exception that I am getting. I do not see anything else on my console. – Vevin Kumar Sep 17 '14 at 14:18
  • The exception does not seem to be in this line of code. You may want to post some more of your code. – Pushkar Sep 17 '14 at 15:24
  • @Pushkar - I have added my complete code in the question. Please have a look at it. Thanks a lot for your time. – Vevin Kumar Sep 17 '14 at 17:17
  • I wonder if you have possibly conflated JSON-Simple with some other JSON package that is attempting to JSONize your classes. – Hot Licks Sep 17 '14 at 17:38
  • See if you can get the web service to provide a stack dump. – Hot Licks Sep 17 '14 at 17:47
  • Ah! I'm guessing that the web service classpath does not properly include the JSON-Simple jar and any pre-reqs. – Hot Licks Sep 17 '14 at 17:49
  • @HotLicks - Thank you for your comments. That seems like a possible cause. How do I include the JSON-Simple jar to the web service classpath? Currently, I have included the JSON-Simple jar to the Libraries under Project Properties. – Vevin Kumar Sep 18 '14 at 06:37

0 Answers0