我有应用程序在Jbutton上单击我创建具有自己的类和导入文件的新JFrame
当我读取文件时,我将该文件传递给我的MainGui类的方法,在那里我解析数据并将其存储在全局字段的对象数组中。
当我点击MainGui的另一个按钮时,我无法访问该对象数组而且没有任何字段(我得到nullPointerException)
这是我的代码:
MainGui Class
public class MainGui implements ActionListener {
private JFrame frame;
private JPanel panel;
GrupaArtikala[] objektiArtikli; // ARRAY OF OBJECTS
JPanel centerPanel = new JPanel(new BorderLayout());
private final int MAX_TABLES = 8;
JButton buttonImport;
JButton [] buttonsTables = new JButton[MAX_TABLES];
String tables[] = {
"Grupa artikala",
"Osnovna mera",
"Jedinica mere",
"Naknada",
"Popust",
"Artikal",
"Normativ",
"Normativ stavka",
};
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainGui window = new MainGui();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private void createAndShowPodesavanja() {
GuiFileChooser guiPodesavanja= new GuiFileChooser(); // HERE I CREATE NEW FRAME
guiPodesavanja.initialize();
guiPodesavanja.setVisible(true);
// this.setVisible(false);
}
public MainGui() {
initialize();
}
public void pars(String file) {
try {
String content = new String(Files.readAllBytes(Paths.get(file)));
int startArtikitag = content.indexOf(":ISSI");
int endArticalTag = content.indexOf("#TABLENAME:OSNOVNA_MJERA");
String gupaArtikalaString = content.substring(startArtikitag + 5, endArticalTag - 15);
System.out.println(gupaArtikalaString);
String[] lines =gupaArtikalaString.split(System.lineSeparator());
objektiArtikli = new GrupaArtikala[lines.length];
for(int i = 1; i < lines.length; i++) {
objektiArtikli[i] = new GrupaArtikala(lines[i]); // HERE I INITALIAZE ARRAY OF OBJECTS
}
System.out.println("rec u konstruktoru lokalno " + objektiArtikli[1].getId());
System.out.println("rec u gobalno" + objektiArtikli[1].getId());
// HERE I HAVE OUTPUT
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
initialize();
}
private void initialize() {
frame = new JFrame("FreshPos baza podataka");
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
// Main paneel
panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBorder( BorderFactory.createEmptyBorder(10,10,10,10) );
frame.getContentPane().add(panel);
//West panel;
JPanel panelWest = new JPanel(new GridLayout(14,0,0,2));
panelWest.setBorder( BorderFactory.createEmptyBorder(200,0,10,10) );
panel.add(panelWest, BorderLayout.WEST);
for (int i = 0; i < MAX_TABLES; i++) {
buttonsTables[i] = new JButton(tables[i]);
//buttonsTables[i].setMaximumSize(new Dimension(Integer.MAX_VALUE, buttonsTables[i].getMinimumSize().height));
//buttonsTables[i].setPreferredSize(new Dimension(200,400));
panelWest.add(buttonsTables[i]);
}
buttonsTables[0].addActionListener(this); // HERE I CLICK ON BUTTON!!
//North panel;
JPanel northPanel = new JPanel(); // Donji layout za dugmice
northPanel.setBorder( BorderFactory.createEmptyBorder(0,0,0,0) );
panel.add(northPanel, BorderLayout.NORTH);
//northPanel.setBackground(Color.green);
buttonImport = new JButton("Importuj datoteku");
buttonImport.setPreferredSize(new Dimension(180,40));
northPanel.add(buttonImport, BorderLayout.WEST);
buttonImport.addActionListener(this);
JButton ButtonRecord = new JButton("Snimi datoteku");
ButtonRecord.setPreferredSize(new Dimension(180,40));
northPanel.add(ButtonRecord, BorderLayout.WEST);
}
public void showGrupaArtikala () {
centerPanel.removeAll();
centerPanel.updateUI();
centerPanel.setBackground(Color.GRAY);
panel.add(centerPanel, BorderLayout.CENTER);
JPanel southInCentral = new JPanel(); // Donji layout za dugmice
centerPanel.add(southInCentral, BorderLayout.SOUTH);
JButton buttonDodaj = new JButton("Dodaj");
//buttonDodaj.setLocation(100, 100);
buttonDodaj.setPreferredSize(new Dimension(180,40));
southInCentral.add(buttonDodaj);
JButton buttonIzmeni = new JButton("Izmeni");
buttonIzmeni.setPreferredSize(new Dimension(180,40));
southInCentral.add(buttonIzmeni);
JButton butonObrisi = new JButton("Obrisi");
butonObrisi.setPreferredSize(new Dimension(180,40));
southInCentral.add(butonObrisi);
// Center layout in central panel
JPanel centralInCentral = new JPanel(); //
centerPanel.add(centralInCentral);
// Jtable
String[] columnNames = {"ID,",
"SIFRA",
"NAZIV",
"IKONA_ID"};
Object[][] data = {
{"Kathy", "Smith", "Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe", "Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black", "Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White", "Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown", "Pool", new Integer(10), new Boolean(false)}
};
JTable table = new JTable(data, columnNames);
//JTable(Object[][] rowData, Object[] columnNames)
JScrollPane scrollPane = new JScrollPane(table);
centerPanel.add(scrollPane);
panel.repaint();
panel.revalidate();
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == buttonsTables[0]) {
System.out.println("String here "+ objektiArtikli[1].getId()); // HERE OUTPUT IS NULL
showGrupaArtikala();
} else if (e.getSource() == buttonImport) {
System.out.println("aa");
createAndShowPodesavanja();
}
}
}
GuiFileChooser Class
public class GuiFileChooser {
private JFrame frame;
JTextField jEditText;
public GuiFileChooser () {
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuiFileChooser window = new GuiFileChooser();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private void fileChoose(ActionEvent evt) {
JFileChooser chooser = new JFileChooser();
int i=chooser.showOpenDialog(frame);
if(i==JFileChooser.APPROVE_OPTION){
File f = chooser.getSelectedFile();
String filename = f.getAbsolutePath();
MainGui m = new MainGui();
m.pars(filename); // HERE I CALL pars(filename) in MainGui method
jEditText.setText(filename);
}
}
void initialize() {
frame = new JFrame();
frame.setTitle("Dodaj datoteku");
frame.setBounds(750, 350, 460, 150); // top
//Dimension d = new Dimension(440, 200);
// Dimension d1 = new Dimension(451, 451);
frame.setResizable(false);
//frame.setPreferredSize(d);
//frame.setMinimumSize(d);
// frame.setMaximumSize(d1);
// frame.setSize(450, 450);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton lblNaziv = new JButton("Izaberite datoteku..");
lblNaziv.setBounds(10 ,10, 160, 25); // horizontalna margina , vertikalna margina , visina, duzina
lblNaziv.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fileChoose(evt);
}
});
frame.getContentPane().add(lblNaziv);
jEditText = new JTextField();
jEditText.setBounds(180 ,10, 250, 25); // horizontalna margina , vertikalna margina , visina, duzina
frame.getContentPane().add(jEditText);
JButton buttonOk = new JButton("OK");
buttonOk.setBounds(180,50, 100, 30); // horizontalna margina , vertikalna margina ,
frame.getContentPane().add(buttonOk);
buttonOk.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
frame.dispose();
}
});
}
public void setVisible(boolean b) {
frame.setVisible(b);
}
}