Hi, I'm trying to convert a ResultSet to XYDataset, then create a scatter plot, but encountered numerous error messages, namely java.lang.NullPointerException. Any help would be greatly appreciated.
The main function is:
public static void main(String[] args) {
java.sql.ResultSet rs = jdbc.getResults2("select te,tce from table8 where te > 5000");
MyXYDataset xyData = new MyXYDataset(rs);
JFreeChart sChart = ChartFactory.createScatterPlot("Scatter Plot","xLabel","yLabel",xyData,true);
JFreeChartFrame frame = new JFreeChartFrame("Scatter Plot", sChart, true);
frame.pack();
frame.setVisible(true);
}
The constructor of MyXYDataset(rs) is:
--note that the print out of xValues and yValues does show that the arrays are filled with valid values:
public MyXYDataset(java.sql.ResultSet rs) {
try{
int item = 0;
while(rs.next()){
xValues[0][item] = new Double(rs.getDouble("te")/1000); yValues[0][item] = new Double(rs.getDouble("tce")/10000);
System.out.println(xValues[0][item] + " , " + yValues[0][item]);
item++;
}
System.out.println("done");
}catch(java.sql.SQLException e){
System.out.println(e.getMessage());
}
}
ResultSet to XYDataset
Re: ResultSet to XYDataset
Never mind, I found and solved the problem. If anyone wants the solution, conctact me.
Re: ResultSet to XYDataset
I have gotten requests for my solution, so here it is:
1. Create a new version of the XYDataset class so that ITEM_COUNT is public, then make the constructor take a ResultSet as an argument. I called this new class MyXYDataset.
2. In another class, MyFirstChart, produce a ResultSet, instatiate MyXYDataset with the ResultSet as an argument, set ITEM_COUNT to the number of observations from the ResultSet.
Sorry for the lengthy inclusion of code, but I was asked for it. If there's a more efficient solution than mine, please let me know.
//MyXYDataset
package com.jrefinery.chart.demo;
import com.jrefinery.data.*;
public class MyXYDataset extends AbstractSeriesDataset implements XYDataset {
public static int SERIES_COUNT = 1;
public static int ITEM_COUNT = 0;
public static double RANGE = 0;
public Double[][] xValues = new Double[SERIES_COUNT][ITEM_COUNT];
public Double[][] yValues = new Double[SERIES_COUNT][ITEM_COUNT];
public MyXYDataset(java.sql.ResultSet rs) {
try{
int item = 0;
while(rs.next()){
xValues[0][item] = new Double(rs.getDouble(1));
yValues[0][item] = new Double(rs.getDouble(2));
//System.out.println(xValues[0][item] + " , " + yValues[0][item]);
item++;
}
System.out.println("done");
}catch(java.sql.SQLException e){
System.out.println(e.getMessage());
}
}
/**
* Returns the number of items in the specified series.
* @param series The index (zero-based) of the series;
* @return The number of items in the specified series.
*/
public int getItemCount(int series) {
return ITEM_COUNT;
}
/**
* Returns the number of series in the data source.
* @return The number of series in the data source.
*/
public int getSeriesCount() {
return SERIES_COUNT;
}
/**
* Returns the name of the series.
* @param series The index (zero-based) of the series;
* @return The name of the series.
*/
public String getSeriesName(int series) {
return "Series" + series;
}
/**
* Returns the x-value for the specified series and item. Series are numbered 0, 1, ...
* @param series The index (zero-based) of the series;
* @param item The index (zero-based) of the required item;
* @return The x-value for the specified series and item.
*/
public Number getXValue(int series, int item) {
return xValues[series][item];
}
/**
* Returns the y-value for the specified series and item. Series are numbered 0, 1, ...
* @param series The index (zero-based) of the series;
* @param item The index (zero-based) of the required item;
* @return The y-value for the specified series and item.
*/
public Number getYValue(int series, int item) {
return yValues[series][item];
}
/**
* Insert the method's description here.
* Creation date: (2/25/02 1:22:47 PM)
* @return com.jrefinery.data.XYDataset
*/
public XYDataset newMethod() {
return null;
}
}
//MY FIRST CHART
package com.jrefinery.chart.demo;
import com.jrefinery.data.*;
import com.jrefinery.chart.ChartFactory;
import com.jrefinery.chart.JFreeChart;
import com.jrefinery.chart.JFreeChartFrame;
public class MyFirstChart {
public static void main(String[] args) {
JDBC jdbc = new JDBC();
JDBC jdbc1 = new JDBC();
int count = 0;
jdbc.connect("sqldb");
jdbc1.connect("sqldb");
java.sql.ResultSet rs = jdbc.getResults2("select varA,varB from tableA ");
java.sql.ResultSet rs1 = jdbc1.getResults2("select count(varA) as count from tableA");
try{
while(rs1.next()){
count = rs1.getInt(1);
System.out.println(count);
}
}catch(java.sql.SQLException e){}
MyXYDataset.ITEM_COUNT = count;
XYDataset xyData = new MyXYDataset(rs);
jdbc.close();
jdbc1.close();
JFreeChart sPlot = ChartFactory.createScatterPlot("Scatter Plot","TE","TCE",xyData,true);
JFreeChartFrame frame = new JFreeChartFrame("Scatter Plot", sPlot, true);
frame.pack();
frame.setVisible(true);
}
}
1. Create a new version of the XYDataset class so that ITEM_COUNT is public, then make the constructor take a ResultSet as an argument. I called this new class MyXYDataset.
2. In another class, MyFirstChart, produce a ResultSet, instatiate MyXYDataset with the ResultSet as an argument, set ITEM_COUNT to the number of observations from the ResultSet.
Sorry for the lengthy inclusion of code, but I was asked for it. If there's a more efficient solution than mine, please let me know.
//MyXYDataset
package com.jrefinery.chart.demo;
import com.jrefinery.data.*;
public class MyXYDataset extends AbstractSeriesDataset implements XYDataset {
public static int SERIES_COUNT = 1;
public static int ITEM_COUNT = 0;
public static double RANGE = 0;
public Double[][] xValues = new Double[SERIES_COUNT][ITEM_COUNT];
public Double[][] yValues = new Double[SERIES_COUNT][ITEM_COUNT];
public MyXYDataset(java.sql.ResultSet rs) {
try{
int item = 0;
while(rs.next()){
xValues[0][item] = new Double(rs.getDouble(1));
yValues[0][item] = new Double(rs.getDouble(2));
//System.out.println(xValues[0][item] + " , " + yValues[0][item]);
item++;
}
System.out.println("done");
}catch(java.sql.SQLException e){
System.out.println(e.getMessage());
}
}
/**
* Returns the number of items in the specified series.
* @param series The index (zero-based) of the series;
* @return The number of items in the specified series.
*/
public int getItemCount(int series) {
return ITEM_COUNT;
}
/**
* Returns the number of series in the data source.
* @return The number of series in the data source.
*/
public int getSeriesCount() {
return SERIES_COUNT;
}
/**
* Returns the name of the series.
* @param series The index (zero-based) of the series;
* @return The name of the series.
*/
public String getSeriesName(int series) {
return "Series" + series;
}
/**
* Returns the x-value for the specified series and item. Series are numbered 0, 1, ...
* @param series The index (zero-based) of the series;
* @param item The index (zero-based) of the required item;
* @return The x-value for the specified series and item.
*/
public Number getXValue(int series, int item) {
return xValues[series][item];
}
/**
* Returns the y-value for the specified series and item. Series are numbered 0, 1, ...
* @param series The index (zero-based) of the series;
* @param item The index (zero-based) of the required item;
* @return The y-value for the specified series and item.
*/
public Number getYValue(int series, int item) {
return yValues[series][item];
}
/**
* Insert the method's description here.
* Creation date: (2/25/02 1:22:47 PM)
* @return com.jrefinery.data.XYDataset
*/
public XYDataset newMethod() {
return null;
}
}
//MY FIRST CHART
package com.jrefinery.chart.demo;
import com.jrefinery.data.*;
import com.jrefinery.chart.ChartFactory;
import com.jrefinery.chart.JFreeChart;
import com.jrefinery.chart.JFreeChartFrame;
public class MyFirstChart {
public static void main(String[] args) {
JDBC jdbc = new JDBC();
JDBC jdbc1 = new JDBC();
int count = 0;
jdbc.connect("sqldb");
jdbc1.connect("sqldb");
java.sql.ResultSet rs = jdbc.getResults2("select varA,varB from tableA ");
java.sql.ResultSet rs1 = jdbc1.getResults2("select count(varA) as count from tableA");
try{
while(rs1.next()){
count = rs1.getInt(1);
System.out.println(count);
}
}catch(java.sql.SQLException e){}
MyXYDataset.ITEM_COUNT = count;
XYDataset xyData = new MyXYDataset(rs);
jdbc.close();
jdbc1.close();
JFreeChart sPlot = ChartFactory.createScatterPlot("Scatter Plot","TE","TCE",xyData,true);
JFreeChartFrame frame = new JFreeChartFrame("Scatter Plot", sPlot, true);
frame.pack();
frame.setVisible(true);
}
}
Re: ResultSet to XYDataset
Hi Victor,
Thanks for posting your code. I've also uploaded some code (from Bryan Scott), for reading data via JDBC, to the contributions section on the JFreeChart web page. When I get a chance, I'll work through both (and anything else that anyone wants to contribute) and see what I can put in a future version of JFreeChart.
Interested parties should post comments here - I don't work with JDBC a lot, so I'm happy to take advice on the best course...
Regards,
Dave Gilbert
Thanks for posting your code. I've also uploaded some code (from Bryan Scott), for reading data via JDBC, to the contributions section on the JFreeChart web page. When I get a chance, I'll work through both (and anything else that anyone wants to contribute) and see what I can put in a future version of JFreeChart.
Interested parties should post comments here - I don't work with JDBC a lot, so I'm happy to take advice on the best course...
Regards,
Dave Gilbert
Re: ResultSet to XYDataset
Dave, thanks for the feedback. I'm only concerned that this approach may be slow for large datasets because first it loops through the ResultSet to create xValues & yValues arrays, then creates the chart. If only the process could be reduced to where the ResultSet is immediately plotted without going through a conversion process. I think it takes a lot more work to achieve that level of efficiency than what I suggested.
Victor
Victor