Gamer Gamer - 2 years ago 254
Java Question

KDB : Inappropriate type casting for aggregation query result

I ran the following query in KDB to create the table strucrute and insert data in it :-

n:1000000;item:`apple`banana`orange`pear;city:`beijing`chicago`london`paris;tab:([]time:asc n?0D0;n?item;amount:n?100;n?city);

Post which, I ran a Java program to make a select query with aggregation. The code is as follows :-

import java.lang.reflect.Array;
import java.util.logging.Level;
import java.util.logging.Logger;

import kx.c;

public class Aggregate {

public static class KxTableModel {
private c.Flip flip;
public void setFlip(c.Flip data) {
this.flip = data;

public int getRowCount() {
return Array.getLength(flip.y[0]);

public int getColumnCount() {
return flip.y.length;

public Object getValueAt(int rowIndex, int columnIndex) {
return[columnIndex], rowIndex);

public String getColumnName(int columnIndex) {
return flip.x[columnIndex];

public static void main(String[] args) {
KxTableModel model = new KxTableModel();
c c = null;
try {
c = new c("localhost", 5001,":");
String query="select sum amount by city from tab";
// String query="0!select last price by sym from trade where date=last date";
model.setFlip((c.Flip) c.k(query));
} catch (Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (c != null) {try{c.close();} catch (IOException ex) {}
System.out.println(model.getValueAt(1, 1));


The above code gave me the following error after execution :-

Apr 14, 2016 9:30:10 AM Aggregate main
SEVERE: null
java.lang.ClassCastException: kx.c$Dict cannot be cast to kx.c$Flip
at Aggregate.main(
Exception in thread "main" java.lang.NullPointerException
at Aggregate$KxTableModel.getRowCount(
at Aggregate.main(

Answer Source

Your query returns a keyed table (because of the by city) You can now either deal with the c.Dict or insert 0! before your select to unkey the table.

0!select sum amount by city from tab

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download