import java.awt.*;
import javax.swing.*;
class UseEnableComponet1 extends JPanel
{
JButton b1,b2;
JTextField text;
UseEnableComponet1(){
setLayout(new FlowLayout());
b1 = new JButton("Ok");
b2 = new JButton("Cancel");
b2.setEnabled(false);
add(text);
add(b1);
add(b2);
}
public static void main(String[] args)
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(3);
f.setSize(300,100);
Container c = f.getContentPane();
c.add(new UseEnableComponet1());
f.setVisible(true);
}
}
---------- Run Java ----------
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1045)
at java.awt.Container.add(Container.java:365)
at UseEnableComponet1.<init>(UseEnableComponet1.java:15)
at UseEnableComponet1.main(UseEnableComponet1.java:25)
Output completed (1 sec consumed) - Normal Termination
คืออะไรหรอคับ
ช่วยหน่อย code java run ไม่ได้
Moderator: mindphp, ผู้ดูแลกระดาน
-
- PHP Newbie
- โพสต์: 1
- ลงทะเบียนเมื่อ: 31/01/2010 2:06 pm
-
- PHP Newbie
- โพสต์: 5
- ลงทะเบียนเมื่อ: 04/02/2010 2:09 pm
Re: ช่วยหน่อย code java run ไม่ได้
import java.awt.*;
import javax.swing.*;
class UseEnableComponet1 extends JPanel {
JButton b1, b2;
JTextField text;
UseEnableComponet1() {
setLayout(new FlowLayout());
b1 = new JButton("Ok");
b2 = new JButton("Cancel");
b2.setEnabled(false);
text = new JTextField("Hello java");
add(text);
add(b1);
add(b2);
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(3);
f.setSize(300, 100);
Container c = f.getContentPane();
c.add(new UseEnableComponet1());
f.setVisible(true);
}
}
แก้เป็นแบบนี้ครับ เพราะว่ายังไม่ได้ new text อ่ะครับ
มันก็เลย null pointer exception
import javax.swing.*;
class UseEnableComponet1 extends JPanel {
JButton b1, b2;
JTextField text;
UseEnableComponet1() {
setLayout(new FlowLayout());
b1 = new JButton("Ok");
b2 = new JButton("Cancel");
b2.setEnabled(false);
text = new JTextField("Hello java");
add(text);
add(b1);
add(b2);
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(3);
f.setSize(300, 100);
Container c = f.getContentPane();
c.add(new UseEnableComponet1());
f.setVisible(true);
}
}
แก้เป็นแบบนี้ครับ เพราะว่ายังไม่ได้ new text อ่ะครับ
มันก็เลย null pointer exception
แก้ไขล่าสุดโดย mindphp เมื่อ 04/02/2010 5:33 pm, แก้ไขไปแล้ว 1 ครั้ง.
เหตุผล: ??????????????????????????????????????????????????????????????????????????????????????????????????
เหตุผล: ??????????????????????????????????????????????????????????????????????????????????????????????????
-
- Similar Topics
- ตอบกลับ
- แสดง
- โพสต์ล่าสุด
-
-
โพสต์ใหม่ เขียน Code แปลง Code ข้ามภาษา ด้วย OpenAI ChatGPT
โดย noncup302 » 08/02/2023 6:38 pm » ใน Mindphp Videoman - 0 ตอบกลับ
- 3928 แสดง
-
โพสต์ล่าสุด โดย noncup302
08/02/2023 6:38 pm
-
-
- 0 ตอบกลับ
- 2931 แสดง
-
โพสต์ล่าสุด โดย pataponnew
27/05/2021 5:10 pm
-
-
โพสต์ใหม่ การทำ Android เพื่อใช้สแกน QR Code และ Bar Code Reader
โดย rangsan » 28/05/2018 11:19 am » ใน Mobile Application Developing- Android, iOS - 0 ตอบกลับ
- 7919 แสดง
-
โพสต์ล่าสุด โดย rangsan
28/05/2018 11:19 am
-
ผู้ใช้งานขณะนี้
สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 3