Jfree chart throwing null pointer exception - java

I am using the following code to get a candle stick graph following the tutorial from rose india. However when i try to populate my array it throws a null pointer at create dataset .
public class CandleStickChart extends ApplicationFrame {
static String date[]=new String[2000];
static double open[]=new double[2000];
static double close[]=new double[2000];
static double high[]=new double[2000];
static double low[]=new double[2000];
static double volume[]=new double[2000];
static Date d[]=new Date[2000];
public CandleStickChart(String titel) {
super(titel);
final DefaultHighLowDataset dataset = createDataset();
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(600, 350));
setContentPane(chartPanel);
}
private DefaultHighLowDataset createDataset() {
DefaultHighLowDataset data = new DefaultHighLowDataset(
"", d, high, low, open, close, volume);
return data;
}
private JFreeChart createChart(final
DefaultHighLowDataset dataset) {
final JFreeChart chart = ChartFactory.createCandlestickChart(
"Candlestick Demo", "Time", "Price", dataset, false);
return chart;
}
public static void main(String args[])
{
//populating arrays using data
//checking if array is populated.
for(int i=0;i<temp;i++)
{
System.out.println(" "+high[i]+" "+low[i]+" "+open[i]+" "+close[i]+" "+volume[i]);
System.out.println(d[i]);
}
CandleStickChart chart = new CandleStickChart("Candle Stick Chart");
chart.pack();
RefineryUtilities.centerFrameOnScreen(chart);
chart.setVisible(true);
}
}
Exception
java.lang.NullPointerException
at org.jfree.data.xy.DefaultHighLowDataset.getX(DefaultHighLowDataset.java:147)
at org.jfree.data.xy.AbstractXYDataset.getXValue(AbstractXYDataset.java:75)
at org.jfree.data.general.DatasetUtilities.iterateDomainBounds(DatasetUtilities.java:777)
at org.jfree.data.general.DatasetUtilities.findDomainBounds(DatasetUtilities.java:677)
at org.jfree.data.general.DatasetUtilities.findDomainBounds(DatasetUtilities.java:650)
at org.jfree.chart.plot.XYPlot.getDataRange(XYPlot.java:4551)
at org.jfree.chart.axis.DateAxis.autoAdjustRange(DateAxis.java:1284)
at org.jfree.chart.axis.DateAxis.configure(DateAxis.java:716)
at org.jfree.chart.axis.Axis.setPlot(Axis.java:968)
at org.jfree.chart.plot.XYPlot.<init>(XYPlot.java:666)
at org.jfree.chart.ChartFactory.createCandlestickChart(ChartFactory.java:1946)
at CandleStickChart.createChart(CandleStickChart.java:74)
at CandleStickChart.<init>(CandleStickChart.java:30)
at CandleStickChart.main(CandleStickChart.java:189)
What am i doing wrong
I realise the exception has something to do with the date array. BUt when i print the date array this is what i get.The time may be zero but does that mean it has to throw a null pointer exception.
Wed Mar 10 00:00:00 IST 2010
Tue Mar 09 00:00:00 IST 2010
Mon Mar 08 00:00:00 IST 2010
Fri Mar 05 00:00:00 IST 2010
Thu Mar 04 00:00:00 IST 2010
Wed Mar 03 00:00:00 IST 2010
Tue Mar 02 00:00:00 IST 2010
Mon Mar 01 00:00:00 IST 2010
Fri Feb 26 00:00:00 IST 2010
Thu Feb 25 00:00:00 IST 2010
Wed Feb 24 00:00:00 IST 2010
Tue Feb 23 00:00:00 IST 2010
Mon Feb 22 00:00:00 IST 2010
Fri Feb 19 00:00:00 IST 2010
Thu Feb 18 00:00:00 IST 2010
Wed Feb 17 00:00:00 IST 2010
Tue Feb 16 00:00:00 IST 2010
Fri Feb 12 00:00:00 IST 2010
Thu Feb 11 00:00:00 IST 2010
Wed Feb 10 00:00:00 IST 2010
Tue Feb 09 00:00:00 IST 2010
Mon Feb 08 00:00:00 IST 2010
Fri Feb 05 00:00:00 IST 2010

You are not providing any data, only emtpy arrays (default initialized). Thus your date array contains null values, which cause the NullPointerException. I cannot see (from your code) how printing the date array can give you such a result. I tried your code and the date (d to be more exactly) contains only null values.
The line org.jfree.data.xy.DefaultHighLowDataset.getX(DefaultHighLowDataset.java:147) accesses the date array and calls getTime on the item (maybe something different in the version you are using).

The time may be zero but does that mean it has to throw a null pointer
exception. It do throws a null pointer exception means your dataset itself is not null but when it do createCandlestickChart method with a specify data in your dataset the specify data may be null. I guess you should focus on some X value of the dataset.(org.jfree.data.xy.DefaultHighLowDataset.getX(DefaultHighLowDataset.java:147))

Related

Loop in csv file

I asked this question before however I did not get the answer that I expected. Therefore I opened this new question.
My try:
String fileName = "placements.csv";
try {
// Assume default encoding.
FileWriter fileWriter = new FileWriter(fileName);
// Always wrap FileWriter in BufferedWriter.
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
// First row, write the head of the csv file.
bufferedWriter.write(FILE_HEADER);
bufferedWriter.newLine();
// Increase the begin date 1 seconds until test end date.
int seconds = 0;
Calendar cal = Calendar.getInstance();
cal.setTime(beginDate);
for (int j = 0; j < convertedDifference; j++) {
for (Job currentJob : NEHCalculator.sequenceOrderListofJobs()) {
int times = (int) ((convertedDifference / currentJob.getInterval()) * testDevices());
for (int i = 0; i < currentJob.getNeededTestTime() * times; i++) {
cal.add(Calendar.SECOND, 1);
beginDate.getTime();
// write the test date
bufferedWriter.write(String.valueOf(cal.getTime()));
bufferedWriter.write(";");
bufferedWriter.write(String.valueOf(seconds));
bufferedWriter.write(";");
bufferedWriter.write(currentJob.getJobname());
bufferedWriter.write(";");
bufferedWriter.newLine();
}// end of currentjob loop
seconds++;
}// end first for loop
}
bufferedWriter.close();// Always close files.
} catch (IOException ex) {
System.out.println("Error writing to file '" + fileName);
}
}
First for loop for (int j = 0; j < convertedDifference; j++) : I restrict my program until test end date. If I enter 1 hour test time, i expected to see number 0 until 3599 seconds.
Second for loop for (Job currentJob : NEHCalculator.sequenceOrderListofJobs()): I want to test every devices in the list.
For third for loop for (int i = 0; i < currentJob.getNeededTestTime() * times; i++): I expected to test first job currentJob.getNeededTestTime() * times times.
For example I have 1 hour test time and interval of fist job is 15 min and it tests 2 devices and test needed time 2 seconds. So the output must be:
Mon Feb 22 12:59:59 CET 2016;0;WAF5-H;
Mon Feb 22 13:00:00 CET 2016;1;WAF5-H;
Mon Feb 22 13:00:01 CET 2016;2;WAF5-H;
Mon Feb 22 13:00:02 CET 2016;3;WAF5-H;
Mon Feb 22 13:00:03 CET 2016;4;WAF5-H;
Mon Feb 22 13:00:04 CET 2016;5;WAF5-H;
Mon Feb 22 13:00:05 CET 2016;6;WAF5-H;
Mon Feb 22 13:00:06 CET 2016;7;WAF5-H;
Mon Feb 22 13:00:07 CET 2016;8;WAF5-H;
Mon Feb 22 13:00:08 CET 2016;9;WAF5-H;
Mon Feb 22 13:00:09 CET 2016;10;WAF5-H;
Mon Feb 22 13:00:10 CET 2016;11;WAF5-H;
Mon Feb 22 13:00:11 CET 2016;12;WAF5-H;
Mon Feb 22 13:00:12 CET 2016;13;WAF5-H;
Mon Feb 22 13:00:13 CET 2016;14;WAF5-H;
Mon Feb 22 13:00:14 CET 2016;15;WAF5-H; then it will continue with second job until end of the job list.
However the output of my code is:
Mon Feb 22 12:59:59 CET 2016;0;WAF5-H;
Mon Feb 22 13:00:00 CET 2016;0;WAF5-H;
Mon Feb 22 13:00:01 CET 2016;0;WAF5-H;
Mon Feb 22 13:00:02 CET 2016;0;WAF5-H;
Mon Feb 22 13:00:03 CET 2016;0;WAF5-H;
Mon Feb 22 13:00:04 CET 2016;2;WAF5-H;
Mon Feb 22 13:00:05 CET 2016;2;WAF5-H;
Mon Feb 22 13:00:06 CET 2016;2;WAF5-H;
Mon Feb 22 13:00:07 CET 2016;2;WAF5-H;
Mon Feb 22 13:00:08 CET 2016;2;WAF5-H; until Mon Feb 22 17:55:08 CET 2016;7082;WAF5-H;
It is completely wrong and I spend really so soo much time but I failed with this task. Could someone please help me.
Best regards,
What about changing this code
bufferedWriter.write(String.valueOf(seconds));
for this one:
bufferedWriter.write(String.valueOf(cal.get(Calendar.SECOND)));
I think it might work

java.util.GregorianCalendar issue with add() when applying DST changes

My requirement is determining the next date based on frequency of schedule.
So if frequency is DAILY and the first date is 25-Oct-2015 23:59:59,
the next duedate should be exactly 24 hours apart ie 26-Oct-2015 23:59:59
Calendar.add(int field, int amount) seems to be taking care of the same
Eg:
DAILY frequency -- calendar.add(Calendar.DATE, 1);
WEEKLY frequency -- calendar.add(Calendar.DATE, 7);
MONTHLY frequency -- calendar.add(Calendar.MONTH, 1);
The following is the code abstract of the same:
public static void main(String[] args) {
Calendar calendar = new GregorianCalendar();
for(int i=0;i<10;i++) {
calendar.add(Calendar.DATE, 1);
System.out.println(calendar.getTime());
}
}
==================================================
Thu Oct 29 17:17:26 IST 2015 -- in all cases diff is 24 hrs
Fri Oct 30 17:17:26 IST 2015
Sat Oct 31 17:17:26 IST 2015
Sun Nov 01 17:17:26 IST 2015
Mon Nov 02 17:17:26 IST 2015
Tue Nov 03 17:17:26 IST 2015
Wed Nov 04 17:17:26 IST 2015
Thu Nov 05 17:17:26 IST 2015
Fri Nov 06 17:17:26 IST 2015
In case of DAILY frequency and server being in Eastern time (EDT), a few anomaly was ocuuring with add()
As of Nov-1, DST settings change the same are reflected in add
public static void main(String[] args) {
Calendar calendar = new GregorianCalendar();
calendar.setTimeZone(TimeZone.getTimeZone("US/Eastern"));
for(int i=0;i<10;i++) {
calendar.add(Calendar.DATE, 1);
System.out.println(calendar.getTime());
}
}
-------------------------------------
Wed Oct 28 17:18:14 IST 2015
Thu Oct 29 17:18:14 IST 2015
Fri Oct 30 17:18:14 IST 2015
Sat Oct 31 17:18:14 IST 2015
Sun Nov 01 18:18:14 IST 2015 -- here diff is of 24 + 1 hr
Mon Nov 02 18:18:14 IST 2015
Tue Nov 03 18:18:14 IST 2015
Wed Nov 04 18:18:14 IST 2015 -- else everywhere diff is 24 hours
Thu Nov 05 18:18:14 IST 2015
Fri Nov 06 18:18:14 IST 2015
In case by first date is 25-Oct-2015 23:59:59, in this case, the extra 1 hour shift is causing anomaly as after
31-Oct-2015 23:59:59,
the next date is 2-Nov-2015 00:59:59
Further observing the Code, found out that
// The rest of the fields (week, day or AM_PM fields)
// require time zone offset (both GMT and DST) change
// adjustment.
Actually the server is in EDT, where I'm getting following I/O relation.
I merely tried to debug it on my local instance which is in IST.
public static void main(String[] args) {
Calendar calendar = new GregorianCalendar();
for(int i=0;i<10;i++) {
calendar.add(Calendar.DATE, 1);
System.out.println(calendar.getTime());
}
}
Wed Oct 28 08:09:48 EDT 2015
Thu Oct 29 08:09:48 EDT 2015
Fri Oct 30 08:09:48 EDT 2015
Sat Oct 31 08:09:48 EDT 2015
Sun Nov 01 08:09:48 EST 2015
Mon Nov 02 08:09:48 EST 2015
Tue Nov 03 08:09:48 EST 2015
Wed Nov 04 08:09:48 EST 2015
Thu Nov 05 08:09:48 EST 2015
Fri Nov 06 08:09:48 EST 2015
What should be a reliable way of using a library to ensure that my dates generated are in proper sequence.
Try the below code. The below code doesn't simpley add 1 to DAY or MONTH. It adds milliseconds for a day in order to get the proper result.
public static void main(String[] args) {
DateFormat formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
Calendar calendar = Calendar.getInstance();
formatter.setTimeZone(TimeZone.getTimeZone("US/Eastern"));
calendar.setTimeInMillis(System.currentTimeMillis());
long time = calendar.getTimeInMillis();
for(int i=0;i<10;i++) {
System.out.println(formatter.format(calendar.getTime()));
time = time + 86400000;
calendar.setTimeInMillis(time);
}
}
Definitely formatting of the date can be changed by changing the line -
new SimpleDateFormat("E MMM dd hh:mm:ss z yyyy");
It gives output -
Di Okt 27 08:15:59 EDT 2015
Mi Okt 28 08:15:59 EDT 2015
Do Okt 29 08:15:59 EDT 2015
Fr Okt 30 08:15:59 EDT 2015
Sa Okt 31 08:15:59 EDT 2015
So Nov 01 07:15:59 EST 2015
Mo Nov 02 07:15:59 EST 2015
Di Nov 03 07:15:59 EST 2015
Mi Nov 04 07:15:59 EST 2015
Do Nov 05 07:15:59 EST 2015
I think the anomaly is because, the daylight is ending for EDT zone on that day(31st October night).
NO such problem is occurring for IST as India do not have daylight saving.

json to HashMap<Date, Integer>

I have to convert json Strings like this
{"Tue Jan 13 00:00:00 MEZ 2015":2131165194,"Mon Jan 12 00:00:00 MEZ 2015":2131165194,
"Thu Dec 11 00:00:00 MEZ 2014":2131165194,"Fri Dec 12 00:00:00 MEZ 2014":2131165194,
"Mon Jan 19 00:00:00 MEZ 2015":2131165194,"Wed Dec 10 00:00:00 MEZ 2014":2131165194,
"Mon Jan 05 00:00:00 MEZ 2015":2131165194}
into a HashMap.
Is it really necessary to write a parser?
I try to avoid this, because I never made this bevor, and it seems quite difficult. I would be glad for any help and thanks in advance!
If your keys are unique – you can use the next code
String s = "{\"Tue Jan 13 00:00:00 MEZ 2015\":2131165194,\"Mon Jan 12 00:00:00 MEZ 2015\":2131165194,\n" +
"\"Thu Dec 11 00:00:00 MEZ 2014\":2131165194,\"Fri Dec 12 00:00:00 MEZ 2014\":2131165194,\n" +
"\"Mon Jan 19 00:00:00 MEZ 2015\":2131165194,\"Wed Dec 10 00:00:00 MEZ 2014\":2131165194,\n" +
"\"Mon Jan 05 00:00:00 MEZ 2015\":2131165194}";
Map<Date, Long> map = new GsonBuilder().setDateFormat("E MMM dd HH:mm:ss 'MEZ' yyyy").create().fromJson(s, new TypeToken<HashMap<Date, Long>>() {
}.getType());
Since you mentioned about Gson , you need to set the date format for the Gson object(you can build it via GsonBuilder) and then declare TypeToken<Map<Date, Integer>> after that just invoke fromJson E.g.
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.Map;
public class Test {
public static void main(String[] args) {
String json = "{\"Tue Jan 13 00:00:00 MEZ 2015\":2131165194,\"Mon Jan 12 00:00:00 MEZ 2015\":2131165194,\n" +
"\"Thu Dec 11 00:00:00 MEZ 2014\":2131165194,\"Fri Dec 12 00:00:00 MEZ 2014\":2131165194,\n" +
"\"Mon Jan 19 00:00:00 MEZ 2015\":2131165194,\"Wed Dec 10 00:00:00 MEZ 2014\":2131165194,\n" +
"\"Mon Jan 05 00:00:00 MEZ 2015\":2131165194}";
Type type = new TypeToken<Map<Date, Integer>>() {
}.getType();
Gson gson = new GsonBuilder().setDateFormat("EEE MMM dd HH:mm:ss 'MEZ' yyyy").create();
Map<String, String> myMap = gson.fromJson(json, type);
System.out.println(myMap);
}
}
Output
{Tue Jan 13 00:00:00 CET 2015=2131165194, Mon Jan 12 00:00:00 CET 2015=2131165194, Thu Dec 11 00:00:00 CET 2014=2131165194, Fri Dec 12 00:00:00 CET 2014=2131165194, Mon Jan 19 00:00:00 CET 2015=2131165194, Wed Dec 10 00:00:00 CET 2014=2131165194, Mon Jan 05 00:00:00 CET 2015=2131165194}

count number of certain value occurances in a string

I have a string which contains my mail details such as
GitHub Sat Jun 01 13:32:02 IST 2013
eBay Mon Jun 03 17:37:40 IST 2013
YouTube Tue Jun 04 00:18:50 IST 2013
YouTube Sat Jun 08 01:20:47 IST 2013
eBay Sat Jun 08 13:19:22 IST 2013
eBay Sat Jun 08 13:17:53 IST 2013
eBay Mon Jun 10 15:43:01 IST 2013
YouTube Mon Jun 10 15:47:02 IST 2013
eBay Wed Jun 12 11:10:15 IST 2013
eBay Wed Jun 12 19:25:50 IST 2013
eBay Thu Jun 13 17:22:14 IST 2013
eBay Thu Jun 13 18:09:18 IST 2013
Clark University Thu Jun 13 19:30:09 IST 2013
is there any way to get number of times eBay ,Youtube or anything has occured so that i can have a number of mails received from a particular person??
You can split the string using String tokenizer and match the value of first token.
If eBay is always going to be the first word then, you can use startsWith() method of String class.
try to use regular expressions with grouping of data.
This solution supposes two things:
names of days cannot appear in the first part;
each entry is separated by a newline:
[extra line to fix SO bug with list items and code extracts]
private static final Set<String> DAYS = new HashSet<String>(Arrays.asList(
"Mon ", "Tue ", "Wed ", "Thu ", "Fri ", "Sat ", "Sun "
));
public int nrMatches(final String mailList, final String sender)
{
int ret = 0;
int index;
for (final String line: mailList.split("\r?\n"))
for (final String day: DAYS) {
index = line.indexOf(day);
if (index == -1)
continue;
if (line.subString(0, index).trim().equals(sender))
ret++;
}
return ret;
}

Quartz cron schedule output not as expected

I want to schedule a daily job at 23:59:59 only in weekdays (monday - friday).
i use this cron expression
"59 59 23 ? * MON-FRI",
but the output has tripe value for monday
Wed Aug 29 23:59:59 ICT 2012
Thu Aug 30 23:59:59 ICT 2012
Fri Aug 31 23:59:59 ICT 2012
Mon Sep 03 23:59:59 ICT 2012
Mon Sep 03 23:59:59 ICT 2012
Mon Sep 03 23:59:59 ICT 2012
Tue Sep 04 23:59:59 ICT 2012
Wed Sep 05 23:59:59 ICT 2012
Thu Sep 06 23:59:59 ICT 2012
Fri Sep 07 23:59:59 ICT 2012
is the expression wrong? need help.
i'm getting this output by loop through specific date, here the code
`try {
CronExpression ce = new CronExpression(59 59 23 ? * MON-FRI);
Calendar start = Calendar.getInstance();
start.setTime(new Date());
Calendar end = Calendar.getInstance();
Date endDate = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse("Fri Sep 29 23:59:59 ICT 2012");
end.setTime(endDate);
for (; !start.after(endDate); start.add(Calendar.DATE, 1)) {
Date current = start.getTime();
System.out.println(ce.getNextValidTimeAfter(current));
}
} catch (ParseException ex) {
Logger.getLogger(HelloJob.class.getName()).log(Level.SEVERE, null, ex);
}
}`
The problem isn't in you rule or in Quartz, it's OK and you may use it.
The problem is in your test code.
for (; !start.after(endDate); start.add(Calendar.DATE, 1)) {
Date current = start.getTime();
System.out.println(ce.getNextValidTimeAfter(current));
}
You're not iterating on valid dates but on all days between startDate and endDate.
The loop content is called for invalid days too and for each of those 2 invalid days the "next valid time" after current date is monday. So you have thrice monday, that's perfectly logic.
Hence your log.

Categories

Resources