Display data from database in a Table

68 views
Skip to first unread message

Grace Adewumi

unread,
Oct 1, 2015, 6:49:40 AM10/1/15
to CodenameOne Discussions
Hello, I want to display information I retrieved from MySQL database into a table. I can see the JSON result in my console but I need it displayed in my app. Here is a part of my code:
byte[] data = cr.getResponseData();

Map response = (Stock.ParseJsonObject(data));

Stock.killNetworkAccess();
java.util.List statementResult = (java.util.List) response.get("StatementResult");
System.out.println("Statement Result:" + statementResult);

for (Object saveResult1 : statementResult) {
Map tempHash = (Map) saveResult1;
System.out.println(tempHash);
String result = tempHash.get("Status").toString();
String sellerName = tempHash.get("SellerName").toString();
String stockName = tempHash.get("StockName").toString();
if (result.equalsIgnoreCase("ok")) {
// System.out.print("Success");
System.out.println(stockName); //this works: I can see the list of stock names from my database

String[] columnNames = { "Seller's Name", "Stock Name", "Unit", "Price" };
Object[] dat = { sellerName, stockName };

Table tb = new Table(new DefaultTableModel(columnNames, (Object[][]) dat, false));
allrec.addComponent(tb);
}

}
The console shows this ->>>>>>
Statement Result:[{ID=1, SellerName=, StockName=Seven UP, Unit=23, Price=1, TimeCreated=0000-00-00 00:00:00, Status=Ok}, {ID=2, SellerName=, StockName=Seven UP, Unit=23, Price=1, TimeCreated=0000-00-00 00:00:00, Status=Ok}, {ID=3, SellerName=, StockName=Wallmart, Unit=76, Price=34, TimeCreated=0000-00-00 00:00:00, Status=Ok}, {ID=5, SellerName=, StockName=Africa Insurance, Unit=3, Price=23, TimeCreated=0000-00-00 00:00:00, Status=Ok}
{ID=1, SellerName=, StockName=Seven UP, Unit=23, Price=1, TimeCreated=0000-00-00 00:00:00, Status=Ok}
Seven UP
That shows the data is being retrieved from the database. However, when I try to get it in a table I get this error dialog that says: 
Error: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to Ljava.lang.Object]

I am sure my error is somewhere here: ->>>>
String[] columnNames = { "Seller's Name", "Stock Name", "Unit", "Price" };
Object[] dat = { sellerName, stockName };
Table tb = new Table(new DefaultTableModel(columnNames, (Object[][]) dat, false));

I have these in another class called Stock:
public static Map ParseJsonObject(byte[] data) {
try {
JSONParser parser = new JSONParser();
Map response = parser.parseJSON(new InputStreamReader(new ByteArrayInputStream(data), "UTF-8"));
return response;
} catch (IOException ioe) {
}
return null;
}
public static void killNetworkAccess() {
NetworkManager.getInstance().shutdown();
}


Can someone please help me out? Thank you.

Shai Almog

unread,
Oct 1, 2015, 11:32:25 PM10/1/15
to CodenameOne Discussions
Hi,
you should always use Log.e(err) or printStackTrace with your exceptions otherwise you won't get the error messages for debugging.
You need to look at the stack trace that will point you at the exact line number. You are casting one of the results incorrectly.

eni.it...@gmail.com

unread,
Oct 5, 2015, 3:57:35 PM10/5/15
to CodenameOne Discussions
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [[Ljava.lang.Object;
at com.grace.stock.AccStatement$1.run(AccStatement.java:154)
at java.lang.Thread.run(Unknown Source)
That is the stack trace

Shai Almog

unread,
Oct 5, 2015, 11:56:29 PM10/5/15
to CodenameOne Discussions, eni.it...@gmail.com
Look at line 154 of AccStatement.java where you are performing the cast of a single dimension object array to a multi-dimension object array.
Reply all
Reply to author
Forward
0 new messages