![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2004
Posts: 17
Rep Power: 0
![]() |
n00b gui program help
ok i said this was a n00b program. im still a beginner in this java gui thing...
![]() what's wrong with this program? it says exception in thread "main". java.lang.nullexception... i cant seem to make this work. i checked the syntax, maybe anyone can help? thanks. import java.awt.*;
public class ChatClient1 {
private Frame f1;
private Panel p1;
private Button bSend, bQuit, bWhite, bYellow, bBlue;
private TextArea txtA1;
private TextField txtF1;
public void ChatClient1() {
f1 = new Frame("Chat Room");
p1 = new Panel();
bSend = new Button("Send");
bQuit = new Button("Quit");
bWhite = new Button("White");
bYellow = new Button("Yellow");
bBlue = new Button("Blue");
txtA1 = new TextArea();
txtF1 = new TextField();
}
public void launchFrame() {
f1.add(txtA1, "Center");
f1.add(txtF1, "South");
f1.add(p1, "East");
p1.add(bSend);
p1.add(bQuit);
p1.add(bWhite);
p1.add(bYellow);
p1.add(bBlue);
f1.pack();
f1.setVisible(true);
}
public static void main(String args[]) {
ChatClient1 guiWindow = new ChatClient1();
guiWindow.launchFrame();
}
} |
|
|
|
|
|
#2 |
|
Professional Programmer
|
Where are you getting the NullPOinterException? it should give you a line number. When you know that, check all the variables on that line, a NullPointerException gets throws when a variable is accessed that does not point to anything. it points to Null, so make sure all your variables point to something that exists.
good luck dizz |
|
|
|
|
|
#3 |
|
Programmer
|
No worries. Take "void" out of your constructor. It (and all constructors you make) should only have visibilty and a name with no return type. It should read..
public ChatClient1() {
f1 = new Frame("Chat Room");
p1 = new Panel();
etc...
}The way you have it right now, because it's not a proper constructor, none of those variables are being initialized. When that first add() method is called in launchFrame(), it throws a NullPointerException because the frame f1 doesn't even exist yet (ie, it's still null).
__________________
I can pick my friends. And I can pick my nose. So, why can't I pick my friend's nose? |
|
|
|
|
|
#4 |
|
Professional Programmer
|
ah, i wasn't aware of that. thanks for letting me know that Ed.
|
|
|
|
|
|
#5 |
|
Programmer
|
Just doing my part to save the rainforest.
![]()
__________________
I can pick my friends. And I can pick my nose. So, why can't I pick my friend's nose? |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Nov 2004
Posts: 17
Rep Power: 0
![]() |
hey thanks a lot! it worked great.
i just saw a sample code from Sun (java student guide) where the constructor is declared void. so i just followed that. anyway thanks again! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|