一个Frame添加了Panel 报错:at java.awt.Container.addImpl(Unknown Source)
写了一个JFrame类和两个Jpanel类 可是只能显示Memberpanel,Itempanel无法显示报错:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at View.MainFrame.<init>(MainFrame.java:23)
at View.MainFrame.main(MainFrame.java:27)
小女子 真的弱爆了 自己改了好久 没改对
------------------------------------------------------------------------------------------------------------
package View;
import java.awt.BorderLayout;
import javax.swing.*;
public class MainFrame extends JFrame{
MemberPanel member_panel;
ItemPanel item_panel;
public MainFrame(){
setVisible(true);
setSize(800,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("超市销售系统");
member_panel=new MemberPanel();
JPanel mainpanel=new JPanel();
mainpanel.setSize(800,600);
mainpanel.add(member_panel,BorderLayout.NORTH);
this.add(mainpanel);
this.add(item_panel);
}
public static void main(String[] args){
MainFrame mf=new MainFrame();
System.out.println("123");
}
}
--------------------------------------------------------------------------------------------------
package View;
import javax.swing.*;
import Model.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ItemPanel extends JPanel {
ItemList item_list;
JPanel leftPanel;
JPanel rightPanel;
JLabel ID_label;
// JLabel title_label;
JTextField ID_field;
JButton id_button;
JTextArea item_area;
JLabel num_label;
JTextField num_field;
JLabel total_label;
JTextField total_field;
JButton finish;
JLabel shouldChargr;
public ItemPanel(){
item_list=new ItemList();
setSize(800, 450);
leftPanel=new JPanel();leftPanel.setSize(650,450);
item_area=new JTextArea();item_area.setSize(650,450);
leftPanel.add(item_area);
rightPanel=new JPanel();rightPanel.setSize(150,450);
ID_label=new JLabel();ID_label.setText("商品ID");ID_field=new JTextField(10);
num_label=new JLabel();num_label.setText("商品数量");num_field=new JTextField(10);
id_button=new JButton("确认");
Item_ID_listener iil=new Item_ID_listener();id_button.addActionListener(iil);
total_label=new JLabel();total_label.setText("收取");
total_field=new JTextField(10);//收入
shouldChargr=new JLabel();shouldChargr.setText("0.0");
finish=new JButton("找零");
Finish_listener fl=new Finish_listener();finish.addActionListener(fl);
rightPanel.setLayout(new BoxLayout(rightPanel,BoxLayout.Y_AXIS));
rightPanel.add(ID_label);rightPanel.add(ID_field);
rightPanel.add(num_label);rightPanel.add(num_field);
rightPanel.add(id_button);
rightPanel.add(total_label);rightPanel.add(ID_field);rightPanel.add(shouldChargr);
rightPanel.add(finish);
setLayout(new BorderLayout());
this.add(leftPanel,BorderLayout.WEST);
this.add(rightPanel,BorderLayout.EAST);
System.out.println("itempanel");
}
class Item_ID_listener implements ActionListener{
public void actionPerformed(ActionEvent event){
try{
//得到商品
String temp=null;
temp=ID_field.getText()+"";
Item nowItem=item_list.getItem(temp);
//计算价格
//确定单价
double perPrice=nowItem.getPrice();
double sprice=nowItem.getBargain_Price();
double sure_price=0.0;
if(perPrice>=sprice){sure_price=sprice;}
else{sure_price=sprice;}
//确定数量计算单项总价
double perTotal_Price=0.0;
int perNum=Integer.parseInt(num_field.getText());
perTotal_Price=perNum*sure_price;String total=perTotal_Price+"";
//计算累计价格
double zongjia=Double.parseDouble(shouldChargr.getText());
zongjia=zongjia+perTotal_Price;
//打印结果
String zongStr=zongjia+"";
shouldChargr.setText(zongStr);
String s=nowItem.getitem_ID()+" "+nowItem.getItem_name()+" "+
nowItem.getPrice()+" "+nowItem.getBargain_Price()+" "+total+num_field.getText();
item_area.append(s+"\n");
}catch(Exception c){}
}
}
class Finish_listener implements ActionListener{
public void actionPerformed(ActionEvent event){
try{
//计算收取找零
double inMoney=Double.parseDouble(total_field.getText());
double zongjia=Double.parseDouble(shouldChargr.getText());
double outMoney=inMoney-zongjia;
String inStr=inMoney+"";String outString=outMoney+"";
String downStr="收取 "+inStr+"\n"+"应付 "+shouldChargr.getText()+"\n"+"找零 "+outString;
item_area.append(downStr);}
catch (Exception ec) {
// TODO: handle exception
}
}
}
}
------------------------------------------------------------------------------------------------------------
package View;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import Model.*;
public class MemberPanel extends JPanel{
MemberList mem_list;
// =new MemberList();
JLabel vip_id_lable;
JTextField vip_id_field;
JLabel vip_infor_lable;
JButton vip_ensure_button;
public MemberPanel(){
mem_list=new MemberList();
setSize(800,150);
vip_id_lable=new JLabel("会员编号");
vip_id_field=new JTextField(10);
vip_infor_lable=new JLabel();
vip_ensure_button=new JButton("确认");
Login_vipListener bl=new Login_vipListener();
vip_ensure_button.addActionListener(bl);
add(vip_id_lable);add(vip_id_field);add(vip_ensure_button);add(vip_infor_lable);
// System.out.println("122e读取");
}
class Login_vipListener implements ActionListener{
public void actionPerformed(ActionEvent event){
try{
// Member now_member=new Member();
String temp=null;
temp=vip_id_field.getText()+"";
Member now_member=mem_list.getMember(temp);
vip_infor_lable.setText(now_member.toString());
}catch(Exception c){vip_infor_lable.setText("不存在该会员");}
}
}
}
--------------------编程问答--------------------
public MainFrame(){
// ...
this.add(item_panel);
}
item_panel 没有赋值,为 null --------------------编程问答-------------------- 定义变量的时候赋初值,就不会出现这种空指针异常的问题了。 --------------------编程问答-------------------- 楼上正解。。在函数内定义变量时,特别是定义一个类或者接口的引用,最好初始化,否则容易空指针异常 --------------------编程问答-------------------- 这个 问题已经处理好了 ~
itempanel和 memberpanel是按照BorderLayout,分别是north,south放在mainpanel上的
public MainFrame(){
//
mainpanel.add(member_panel,BorderLayout.NORTH);
mainpanel.add(item_panel,BorderLayout.SOUTH);
this.add(mainpanel);
}
可是出来的界面,itempanel和memberpanel是等高的,而且当输入会员ID后,显示会员信息,itempanel中的rightpanel会向右移动,输出商品信息后,itempanel才回到memberpanel下方,并且当item_area增高时,rightpanel中的JField也会随之增高
求解啊 --------------------编程问答-------------------- 那就是你代码的问题了 --------------------编程问答-------------------- 已经处理好了 改变了一个布局管理器
补充:Java , Java SE