We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi , I am trying to plot the data with the country that the user has entered.
My data is formatted in the following way.
Country ,1975 ,1976 ....2014 ;
Afghanistan, 23, 24 ....40.
I want to plot the row data with the code below but I am getting Null Pointer Exception. I can't see where I have gone wrong. Can someone please help me ?
import org.gicentre.utils.stat.*;
import org.gicentre.utils.gui.*;
XYChart Chart;
Table Men ;
TextInput textInput;
float[] yearstoplot = new float[40];
void setup()
{
size(1000, 1000);
Table Men = loadTable ("Mendatare.csv","header,csv");
Chart =new XYChart(this);
Chart.setMaxX(2015);
Chart.setMinX(1974);
Chart.setXFormat("####");
Chart.setMaxY(30);
Chart.setMinY(18);
Chart.showXAxis(true);
Chart.showYAxis(true);
Chart.setPointColour(color(180,4, 110));
Chart.setPointSize(5);
Chart.setLineColour(color(180,4, 110));
Chart.setLineWidth(2);
textSize(20);
PFont fonttextbox = createFont("BodoniMT-48",14);
textInput= new TextInput(this,fonttextbox,14);
for (int i=0; i<yearstoplot.length; i++)
{
yearstoplot[i] = (i+1975);
}
println(yearstoplot);
}
void keyPressed ()
{
textInput.keyPressed();
if (keyCode ==ENTER))
{
String countrytoplot = textInput.getText();
TableRow target = Men.findRow(countrytoplot, "Country/Region/World"); // here is the problem
float x0 = target.getFloat(3); //get value from column 3
float x1 = target.getFloat(4);
float x2 = target.getFloat(5);
float x3 = target.getFloat(6);
float x4 = target.getFloat(7);
float x5 = target.getFloat(8);
float x6 = target.getFloat(9);
float x7 = target.getFloat(10);
float x8 = target.getFloat(11);
float x9 = target.getFloat(12); //get value from column 12
float[] datatoplot = {x0,x1,x2,x3,x4,x5,x6,x7,x8,x9}; //aggregate the values
Chart.setData(yearstoplot,datatoplot);
}
}
void draw()
{
background(255);
fill(0);
textAlign(CENTER);
text("Obesity over the years for countries",300, 20);
strokeWeight(2.0);
stroke(40);
fill(0,200);
rect(770,20,215,60,8);
textAlign(RIGHT);
fill(0);
textSize(10);
text("Please type in a country name", width-50,30);
textInput.draw(780,40);
Chart.draw(30,80,width-400,height-300);
}
Is there a better way of doing it ?
Answers
line 11 you repeat the word Table from 4; this means that you have a local entity overshadowing the global Men
in which line number is your error?
line 44 was my error
I have moved line
Table Men = loadTable ("Mendatare.csv","header,csv");
intovoidkeyPressed()
(beforeString countrytoplot = textInput.getText();
). Now I am getting an error of NullPointerException on line45(above code) when i input a countryloadTable should be in setup()
again: Table Men is a global array defined in line 4
when you load it in line 11 just say Men = ...... without the word Table!!
otherwise line 44 finds the table from line 4 empty because line 11 didn't fill the table from line 4 but a 2nd local table in line 11. that's because you use the word Table again thus declaring a new table and not referring to the old one from line 4.
Hi , I have taken your advice and put
Men = loadTable ("Mendatare.csv","header,csv");
on line 11. The problem still persists. This time, the NullPointerException problem happens on line 45. It cannot find a value and the programme freezes.different reasons
check if Men is not null.
target is not null
and then if it has 12 columns
post your entire code and some 12 lines from your csv
I have checked that the Table Men is not null and for some reason that target is null(but I don't understand why)
The actual thing has got 40 columns instead of 12. I have copied the header and the first 4 rows of data.
Many thanks
Country/Region/World ISO Sex 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
Afghanistan AFG Men 19.47844827 19.53808995 19.59789939 19.65844556 19.72076313 19.78481453 19.85084724 19.9192174 19.9902131 20.06281388 20.13563768 20.20752221 20.27837123 20.34625175 20.40941512 20.46297612 20.50766189 20.5419083 20.56277773 20.57254058 20.59909744 20.6290308 20.66439575 20.69757962 20.73685294 20.77379915 20.80194467 20.85381958 20.90901423 20.95817052 21.00987443 21.05929068 21.11625593 21.17027834 21.23799826 21.30640465 21.3719749 21.44237223 21.5086472 21.56641901
Albania ALB Men 23.73478892 23.81212054 23.88995725 23.96830254 24.04696226 24.11090699 24.17786861 24.24501562 24.31165921 24.37529132 24.43716757 24.50007492 24.55849847 24.61230736 24.67119213 24.72081015 24.7417872 24.76108692 24.79467316 24.84130104 24.90494189 24.98045039 25.04798092 25.12918918 25.21935391 25.31584684 25.41747544 25.51476672 25.61000324 25.7032725 25.79514583 25.88617039 25.9785683 26.07097873 26.15785119 26.24095647 26.32010579 26.39507014 26.4649792 26.52991471
Algeria DZA Men 21.37254244 21.46797202 21.56539923 21.6672982 21.77219649 21.87589224 21.98136265 22.08816741 22.19660246 22.30482798 22.41118857 22.51219916 22.6078033 22.69829134 22.78842782 22.8756329 22.95875593 23.03953878 23.11395402 23.18224947 23.24975981 23.31544178 23.37688539 23.43837998 23.49709251 23.55214202 23.60744448 23.66489482 23.72553324 23.78569298 23.84304818 23.89572402 23.94758581 23.99747131 24.04496947 24.09122119 24.13508542 24.17762739 24.2180873 24.25830878
American Samoa ASM Men 29.00456338 29.28691508 29.56723838 29.84752846 30.12531312 30.38934527 30.64877218 30.90449073 31.15297212 31.39450662 31.62935138 31.85668373 32.071196 32.2745201 32.46316896 32.63751513 32.79058115 32.92051846 33.02689742 33.11274442 33.17474197 33.21359919 33.22868432 33.22794794 33.21055667 33.18340752 33.14602735 33.09892673 33.04599066 32.98369263 32.91580274 32.84282323 32.76662339 32.68912698 32.60405257 32.51870526 32.43223327 32.34683573 32.26077439 32.17584501
This is crying out for an array and a for loop. In fact you use an array for them immediately afterwards...
Ctrl-t in the editor will indent the code nicely.
So , do I do something like this ?
for ( int i=3; i<41;i++) { float[] datatoplot =target.getFloat(i); }
Ctrl-t indeed is very helpful. Thanks
Do you still get the error? It could be caused by a empty line in your file.
When nothing is printed in line 54, target might be null. Maybe your findRow is wrong, the parameters swapped---- or country world region is too long (or the / are wrong)
Thanks for the help. I have managed to solve the problem.
Great!