java - JavaFX chart variable scope -


final categoryaxis yaxis = new categoryaxis(); final numberaxis zaxis = new numberaxis();   if (cbtypegraphview.equals("bar chart")) {     barchart<string, number> chart = new barchart<string, number>(yaxis,xaxis); } if (cbtypegraphview.equals("line chart")) {     linechart<string, number> chart = new linechart<string, number>(yaxis,xaxis); }  anchorpane.settopanchor(chart, 110d); anchorpane.setleftanchor(chart, 10d); anchorpane.setrightanchor(chart, 5d); anchorpane.setbottomanchor(chart, 50d); 

the chart variable looses scope outside if statement wondering how can fixed chart not lose scope outside if statement. thinking using it's parent class xychart class. i'm not sure how add barchart or linechart xychart.

you can do

final categoryaxis yaxis = new categoryaxis(); final numberaxis zaxis = new numberaxis();   xychart<string, number> chart = null ;  if (cbtypegraphview.equals("bar chart")) {     chart = new barchart<string, number>(yaxis,xaxis); } if (cbtypegraphview.equals("line chart")) {     chart = new linechart<string, number>(yaxis,xaxis); }  anchorpane.settopanchor(chart, 110d); anchorpane.setleftanchor(chart, 10d); anchorpane.setrightanchor(chart, 5d); anchorpane.setbottomanchor(chart, 50d); 

you should deal case neither if statement executes, in order avoid null pointer exceptions.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -