I have a class that implements card layout:
CardWindow.java
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import extraction.gui.Diagram;
import extraction.main.Main;
public class CardWindow extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
private JFrame f;
private JPanel panel_1, panel_2;
private JButton btnNext, btnBack;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CardWindow window = new CardWindow();
window.f.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public CardWindow() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
f = new JFrame();
f.setBounds(100, 100, 1000, 480);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(new CardLayout(0, 0));
panel_1 = new Main_1();
panel_2 = new Snd();
f.getContentPane().add(panel_1);
f.getContentPane().add(panel_2);
panel_1.setVisible(true);
panel_2.setVisible(false);
btnNext = new JButton("Next");
btnNext.setBounds(829, 417, 161, 29);
panel_1.add(btnNext);
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel_1.setVisible(false);
panel_2.setVisible(true);
}
});
btnBack = new JButton("Back");
btnBack.setBounds(919, 5, 75, 29);
panel_2.add(btnBack);
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel_2.setVisible(false);
panel_1.setVisible(true);
}
});
}
}
Main_1.java
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
public class Main_1 extends JPanel {
private JTextField textField;
/**
* Create the panel.
*/
public Main_1() {
setLayout(null);
textField = new JTextField();
textField.setBounds(131, 69, 134, 28);
add(textField);
textField.setColumns(10);
JButton btnCalculate = new JButton("Calculate");
btnCalculate.setBounds(131, 122, 117, 29);
add(btnCalculate);
btnCalculate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Calculate cal = new Calculate(textField.getText());
}
});
}
}
Snd.java
import javax.swing.JPanel;
import javax.swing.JLabel;
public class Snd extends JPanel {
/**
* Create the panel.
*/
public Snd() {
setLayout(null);
Calculate _cal = new Calculate();
JLabel label = new JLabel("New label");
label.setBounds(180, 120, 61, 16);
add(label);
label.setText(_cal.getNum().get(0).toString());
}
}
Calculate.java
import java.util.ArrayList;
import java.util.List;
public class Calculate {
private List<Integer> abc;
public Calculate () {
}
public Calculate(String number) {
abc = new ArrayList<Integer>();
int num = Integer.valueOf(number);
for (int i=0;i<num;i++) {
abc.add(i);
}
System.out.println(abc);
}
public List<Integer> getNum() {
return abc;
}
}
I have two other JPanel
classes: Main_1.java
, Snd.java
Basically when I open the application, it will show Main_1.java
. I enter some values and it run something in Calculate.java
. Then when I click the next button btnNext
, it's supposed to display the processed data onto the next card Snd.java
. But I got java.lang.NullPointerException
when trying to run the application.
NPE at this line label.setText(_cal.getNum().get(0).toString());
Error
java.lang.NullPointerException
at Snd.<init>(Snd.java:18)
at CardWindow.initialize(CardWindow.java:59)
at CardWindow.<init>(CardWindow.java:46)
at CardWindow$1.run(CardWindow.java:33)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
I understand why it's giving NPE but I don't know how to solve it. When I run the program, I don't understand why it's trying to run Snd.java
as well because I didn't click any button to go to the next card, which is Snd.java
.
This is just a runnable example which is to show what I want to achieve. Basically I want to process something in Main_1.java
and transfer the data to Snd.java
where I will do another process there.