Read-only archive. Login and posting are unavailable.
Reply
 
Thread Tools
  #1  
Old 10-06-2010, 23:13
mrdam1001's Avatar
Senior Member
Join Date: 10-2007
Posts: 441
[Nhờ vả anh em] Bác nào pro Java xem giúp mình đoạn code này

Hix , mới học Java , tập viết chương trình đơn giản là : Nhập vàothông tin SV , sau đó nhấn nút "Nhập thông tin" , kết qủa sẽ in ra ở TextArea phía dưới.

Mà sao build ra nó cứ báo lỗi như này , mình ko biết sửa:
Code:
Exception in thread "main" java.lang.NullPointerException
    at Info.InitComp(Info.java:48)
    at Info.(Info.java:40)
    at Program.main(Program.java:12)
Process completed
Đây là đọan code của mình , các bác ném gạch nhẹ tay tí vì mới học mà.

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Info extends JFrame{
	
	private static final long serialVersionUID = 1L;
	JPanel p1,p2,p3,p4,p5,p6,p7,pCenter,pSouth = new JPanel();
	JLabel lbHoTen = new JLabel("Ho Ten");
	JLabel lbNgaySinh = new JLabel("Ngay Sinh");
	JLabel lbGioiTinh = new JLabel("Gioi Tinh");
	JLabel lbDiaChi = new JLabel("Dia Chi");
	JLabel lbMSSV = new JLabel("Ma so SV");
	JLabel lbLop = new JLabel("Lop");
	JButton btnNhap = new JButton("Nhap Thong Tin");
	JTextField txtHoTen = new JTextField(20);
	JTextField txtNgaySinh = new JTextField(20);
	JTextField txtDiaChi = new JTextField(20);
	JTextField txtMSSV = new JTextField(20);
	String[] Lop = {"TH1","TH2","TH3"};
	JComboBox cmbLop = new JComboBox(Lop);
	CheckboxGroup gGioiTinh = new CheckboxGroup();
	Checkbox gNam = new Checkbox("Nam",gGioiTinh,true);
	Checkbox gNu = new Checkbox("Nu",gGioiTinh,false);
	JTextArea txtThongTin = new JTextArea(10,60);
	JScrollPane scrollPane = new JScrollPane(txtThongTin); 
	DefaultListModel df = new DefaultListModel();
	
	public Info(String st)
	{
		super(st);
		InitComp();
		registerEvent();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public void InitComp()
	{
		this.setLayout(new BorderLayout());
		p1.setLayout(new FlowLayout());
		p2.setLayout(new FlowLayout());
		p3.setLayout(new FlowLayout());
		p4.setLayout(new FlowLayout());
		p5.setLayout(new FlowLayout());
		p6.setLayout(new FlowLayout());
		p7.setLayout(new GridLayout(6,1));
		pCenter.setLayout(new FlowLayout());
		pSouth.setLayout(new FlowLayout());
		
		txtThongTin.setEditable(false);		
		p1.add(lbHoTen);
		p1.add(txtHoTen);				
		p2.add(lbNgaySinh);
		p2.add(txtNgaySinh);		
		p3.add(lbGioiTinh);
		p3.add(gNam);
		p3.add(gNu);		
		p4.add(lbDiaChi);
		p4.add(txtDiaChi);		
		p5.add(lbMSSV);
		p5.add(txtMSSV);		
		p6.add(lbLop);
		p6.add(cmbLop);		
		p7.add(p1);
		p7.add(p2);
		p7.add(p3);
		p7.add(p4);
		p7.add(p5);
		p7.add(p6);				
		pCenter.add(btnNhap);		
		pSouth.add(txtThongTin);

		this.getContentPane().add(p7,BorderLayout.NORTH);
		this.getContentPane().add(pCenter,BorderLayout.CENTER);
		this.getContentPane().add(pSouth,BorderLayout.SOUTH);
		this.pack();
		this.getContentPane().setVisible(true);
	}
	
	public void registerEvent()
	{
		EventHandler listener = new EventHandler(this);
		btnNhap.addActionListener(listener);
	}
}

class EventHandler implements ActionListener
{
	Info I;
	String s;
	public EventHandler(Info I)
	{
		this.I = I;
	}
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()== I.btnNhap)
		{
			s = I.txtThongTin.getText().toString();
			s = "Thong tin sinh vien vua nhap" + "\n" + "Ho Ten :" + I.txtHoTen.getText()
			+ "\n" + "Ngay sinh :" + I.txtNgaySinh.getText() + "\n" + "Gioi Tinh :" + I.gGioiTinh.toString() + "\n"
			+ "Dia Chi :" + I.txtDiaChi.getText() + "\n" + "Ma so SV :" + I.txtMSSV.getText() + "\n" + "Lop :" + I.cmbLop.getSelectedItem().toString();
			I.txtThongTin.setText(s);
		}
	}
}

Hàm main mình bỏ vào class này :
Code:
public class Program {
	public static void main (String[] args)
	{
		Info I = new Info("My Student");
	}    
}

Các bác ơi vào giúp em với , lỗi này là dư lào mà em ko bít sửa ....
Cám ơn các bác nhiều lắm.
Reply With Quote
  #2  
Old 10-06-2010, 23:29
mrdam1001's Avatar
Senior Member
Join Date: 10-2007
Posts: 441
Re: [Nhờ vả anh em] Bác nào pro Java xem giúp mình đoạn code này

Các bác trong hội VOZer Giỏi Lập trình đâu rồi. Xem giúp em 1 tý ...
Reply With Quote
  #3  
Old 10-06-2010, 23:42
bungmap's Avatar
Senior Member
Join Date: 06-2007
Posts: 881
Re: [Nhờ vả anh em] Bác nào pro Java xem giúp mình đoạn code này

Sửa lại dòng này bạn nhé
Code:
JPanel p1= new JPanel(),p2= new JPanel(),p3= new JPanel(),p4= new JPanel(),p5= new JPanel(),p6= new JPanel(),p7= new JPanel(),pCenter= new JPanel(),pSouth = new JPanel();

Còn nữa ở class Program thêm 1 dòng lệnh I.show() ở cuối nữa nhé.Mới check lại
Reply With Quote
  #4  
Old 10-06-2010, 23:50
mrdam1001's Avatar
Senior Member
Join Date: 10-2007
Posts: 441
Re: [Nhờ vả anh em] Bác nào pro Java xem giúp mình đoạn code này

Quote:
Originally Posted by bungmap View Post
Sửa lại dòng này bạn nhé
Code:
JPanel p1= new JPanel(),p2= new JPanel(),p3= new JPanel(),p4= new JPanel(),p5= new JPanel(),p6= new JPanel(),p7= new JPanel(),pCenter= new JPanel(),pSouth = new JPanel();

Còn nữa ở class Program thêm 1 dòng lệnh I.show() ở cuối nữa nhé.Mới check lại
Cám ơn bác nhiều lắm. Ko có bác chắc mình vẫn dốt cái lỗi ngớ ngẩn như này.
Reply With Quote
Reply

« Previous Thread | Next Thread »

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


All times are GMT +7. The time now is 16:14.