hey guys, got a question. i designed a method which creates a JPanel. for the parameters, i used (String title, JBButton a, n JButton r); now, when i run the program, i recive this error:
init:
deps-jar:
compile-single:
run-single:
java.lang.NullPointerException
at MainFrame.createWestPanel(MainFrame.java:93)
at MainFrame.<init>(MainFrame.java:31)
at VideoStore.main(VideoStore.java:23)
Exception in thread “main”
Java Result: 1
not sure whats going on. the reason i am creating this method is because im gonna use it multiple times. and its easier then writing the code multiple times. i pasted the source, but i will zip them up if its easier for some people. can someone help me out. heres the source:
1st class:
import java.awt.*;
import java.awt.event.
import javax.swing.*;
import javax.swing.border.
public class VideoStore extends JFrame{
public static void main(String args[]){
MainFrame frame = new MainFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.show();
frame.setJMenuBar(frame.createMenu());
}
public VideoStore(){
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
2nd class:
import java.awt.*;
import java.awt.event.
import javax.swing.*;
import javax.swing.border.
public class MainFrame extends VideoStore implements ActionListener{
JMenuItem jmiAddC, jmiRemoveC, jmiRemoveD, jmiAddD, jmiExit, jmiAbt;
JButton jbtAddC, jbtRemoveC, jbtRemoveD, jbtAddD;
String[] s = {“hello”, “hellfo”, “hefllo”, “helldfo”};
String[] m = {“dante hicks”, “wes cooper”, “hello kitty”, “die lord”};
public MainFrame() {
JPanel wPanel = new JPanel();
wPanel.setLayout(new GridLayout(2,1));
wPanel.add(createWestPanel(“Customer”, jbtAddC, jbtRemoveC));
wPanel.add(createWestPanel(“Disc”, jbtAddD, jbtRemoveD));
JPanel cPanel = new JPanel();
cPanel.setLayout(new GridLayout(1,1));
cPanel.add(createCenterPanel(s,”Customers”));
cPanel.add(createCenterPanel(m, “Discs”));
getContentPane().setLayout(new BorderLayout());
getContentPane().add(wPanel, BorderLayout.WEST);
getContentPane().add(cPanel, BorderLayout.CENTER);
}
/****************** creates a JMenuBar upon call ******************/
JMenuBar createMenu(){
JMenuBar bar = new JMenuBar();
JMenu jmFile = new JMenu(“File”);
bar.add(jmFile);
JMenu jmAbout = new JMenu(“About”);
bar.add(jmAbout);
jmFile.add(jmiAddC = new JMenuItem(“Add Customer”, ‘c’));
jmFile.add(jmiRemoveC = new JMenuItem(“Remove Customer”, ‘v’));
jmFile.add(jmiAddD = new JMenuItem(“Add Disc”, ‘b’));
jmFile.add(jmiRemoveD = new JMenuItem(“Remove Disc”, ‘n’));
jmFile.add(jmiExit = new JMenuItem(“Exit”, ‘e’));
jmAbout.add(jmiAbt = new JMenuItem(“About this program”, ‘a’));
return bar;
}
/****** creates the west section for the content layout ******/
JPanel createWestPanel(String text, JButton a, JButton r){
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(3,2));
p1.add(new JLabel(Customer.getCount() + ” ” + text + “s total”));
p1.add(new JLabel(“”));
p1.add(new JLabel(“Add New ” + text));
p1.add(a = new JButton(“add”));
p1.add(new JLabel(“Remove ” + text));
p1.add(r = new JButton(“Remove”));
p1.setBorder(new TitledBorder(“Add/Remove ” + text ));
return p1;
}
/************** creates center panel upon call ****************/
JPanel createCenterPanel(Object[] o, String t){
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(1,1));
p2.add(new JList(o));
p2.setBorder(new TitledBorder(“Sorted List of ” + t));
return p2;
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == jbtAddC){
JFrame addCus = new JFrame(“add a Customer”);
addCus.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addCus.setSize(200,200);
addCus.show();
}
}
}