我正在构建一个算法模拟工具,我正在努力让我的大型机类实例化并将所有子组件添加到JFrame中,以获得伪代码所在位置的文件系统位置(用于伪代码面板)。
我已经让每个算法都有一个字符串,它详细说明了每个算法的相关文本文件信息在哪里。我有一个方法,它在传入算法时获取字符串,并将其存储到字符串变量中。然后将此字符串变量传递给面板。不幸的是,这抛出了一个空指针,我一直在试着调试它,但是没能成功。
public class SortAnimator extends JFrame
{
private static final int VALUES_LENGTH = 30;
private static final int FRAME_WIDTH = 1200;
private static final int FRAME_HEIGHT = 700;
private PsuedocodePanel pseudoPanel;
private Menu menu;
private InformationPanel infoPanel;
private String algoName;
public String algoLocation;
public SortAnimator(Sorter s) throws IOException
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menu=new Menu();
pseudoPanel=new PsuedocodePanel();
ArrayComponent panel = new ArrayComponent();
infoPanel= new InformationPanel();
add(menu,BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
add(pseudoPanel,BorderLayout.WEST);
add(infoPanel,BorderLayout.SOUTH);
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setVisible(true);
int[] values = new int[VALUES_LENGTH];
for (int i = 0; i < values.length; i++)
values[i] = (int) (Math.random() * panel.getHeight());
s.setValues(values);
s.setPanel(panel);
Thread t = new Thread(s);
t.start();
algoName=s.getAlgorithmName();
algoLocation =s.getAlgorithmLocation();
System.out.println(algoLocation);
pseudoPanel.passFileLocation(algoLocation);
}
}
public class PsuedocodePanel extends JPanel{
private JTextArea txtArea;
private String textFile;
private String fileLocation;
public PsuedocodePanel() throws FileNotFoundException, IOException{
setLayout(new BorderLayout());
txtArea=new JTextArea();
txtArea.setEditable(false);
add(txtArea,BorderLayout.CENTER);
FileReader fr = new FileReader(this.fileLocation);
BufferedReader reader=new BufferedReader(fr);
txtArea.read(reader,null);
Dimension dim=getPreferredSize();//returns object
System.out.println(getPreferredSize());
dim.width=300;
dim.height=75;
setPreferredSize(dim);
Border innerBorder=BorderFactory.createTitledBorder("Algorithm Psuedocode");
Border outerBorder=BorderFactory.createEmptyBorder(5,5,5,5);
setBorder(BorderFactory.createCompoundBorder(outerBorder,innerBorder));
}
public void passFileLocation(String algoLocation) {
this.fileLocation= algoLocation;
}发布于 2015-02-22 01:58:03
您的PsuedocodePanel类使用this.fileLocation在其构造函数中创建一个FileReader,但在代码中fileLocation将为空-请参阅下面的简化代码片段:
public class PsuedocodePanel extends JPanel{
private String fileLocation; // not initialised
// constructor does not accept a fileLocation...
public PsuedocodePanel() throws FileNotFoundException, IOException{
// ... so this.fileLocation is null here:
FileReader fr = new FileReader(this.fileLocation);https://stackoverflow.com/questions/28649149
复制相似问题