我正在编写一个小程序,它读取一个txt文件并生成一个饼图。我正在使用JavaFX来做到这一点。它适用于所有代码都在同一个类中,但是当我尝试使用2个类并使用构造函数来调用它时,我得到一个InstantiationException。
驱动:
const id = {
_high: 0,
_low: 256,
remainder: { _low: 0, high: 0, remainder: null },
valueOf: function(){
return {}; /* valueOf fails to provide a primitive */
},
toString: function() {
return {}; /* toString fails to provide a primitive as well */
}
};
console.log(id + 10); // a "TypeError" is thrown :(
主类:
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.chart.*;
import javafx.scene.Group;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
public abstract class SeqDiagDriver extends Application {
public static void main(String[] args) {
launch(args);
}
@Override public void start(Stage stage) {
sdPieChart sd = new sdPieChart("seqDiag/test1.txt");
Scene scene = new Scene(new Group());
stage.setTitle("Sequence Diagram");
stage.setWidth(500);
stage.setHeight(500);
//Create new List that stores Name of objects and Number of outgoing messages
List<PieChart.Data> seqData = new ArrayList<PieChart.Data>();
for(int i = 0; i < sd.getNumObj(); i++){
String[] objName = sd.getNameArr();
int[] objOut = sd.getOutArr();
seqData.add(new PieChart.Data(objName[i], objOut[i]));
}
//Adding the list with information to an ObservableList
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(seqData);
final PieChart chart = new PieChart(pieChartData);
chart.setTitle("Sequence Diagram");
((Group) scene.getRoot()).getChildren().add(chart);
stage.setScene(scene);
stage.show();
}
}
这是产生的错误:
import java.io.*;
import java.util.List;
import java.util.ArrayList;
//Reads a txt file, gathers information needed, creates a pie chart with that information
public class sdPieChart {
private String nameArr[];
private int outArr[];
private int numObjects = 0;
sdPieChart(){
}
sdPieChart(String in){
readFile(in);
}
public void readFile(String inFile){
BufferedReader br = null;
List<String> nameObjects = new ArrayList<String>();
List<Integer> numOut = new ArrayList<Integer>();
try {
//File from sequence Diagram containing information
br = new BufferedReader(new FileReader(inFile));
String str;
//read lines and put inforamtion into 3 different Lists
while((str = br.readLine()) != null){
String tempName = str.substring(0, str.indexOf(":"));
nameObjects.add(tempName);
String out = str.substring(str.indexOf(":"), str.indexOf(","));
String tempOut = out.substring(1);
numOut.add(Integer.parseInt(tempOut));
}
//Convert lists to arrays
nameArr = nameObjects.toArray(new String[0]);
outArr = toIntArray(numOut);
numObjects = nameArr.length;
}
catch (IOException e){
e.printStackTrace();
}
finally{
try {
br.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
//Used to create int array
static int[] toIntArray(List<Integer> list){
int[] ret = new int[list.size()];
for(int i = 0;i < ret.length;i++){
ret[i] = list.get(i);
}
return ret;
}
public String[] getNameArr() {
return nameArr;
}
public int[] getOutArr() {
return outArr;
}
public int getNumObj() {
return numObjects;
}
public void setNameArr(String[] nameArr) {
this.nameArr = nameArr;
}
public void setOutArr(int[] outArr) {
this.outArr = outArr;
}
public void setNumObjects(int numObjects) {
this.numObjects = numObjects;
}
}
就像我说的,如果这一切都在一个类中,那么它工作正常,但我需要这样做,以便我能够在几个不同的类中调用它,允许我在多个类中创建饼图。