Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Null Pointer exception

  Asked By: Manju    Date: Jan 12    Category: Java    Views: 820
  

Iam developing this window for my chat server and on running this i
get a null pointer exception.How can i handle that.And why is my
applet not running.

-----------------------------
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.awt.event.*;
/*
<applet code = "MessageWindow" width=400 height = 200>
</applet>
*/

public class MessageWindow extends Applet implements ActionListener
{
Button Send;
TextArea A1,A2;

public void init()
{
setBackground(Color.cyan);
setLayout(new BorderLayout());
add(new TextArea(" ",10,30),BorderLayout.NORTH);
add(new TextArea(" ",5,30),BorderLayout.SOUTH);
add(new Button("Send"),BorderLayout.WEST);
Send.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str = ae.getActionCommand();
if(str.equals("Send"))
{
repaint();
}
else
{
repaint();
};
}
}


Share: 

 

2 Answers Found

 
Answer #1    Answered By: Bonnie Hughes     Answered On: Jan 12

try this

Send = new Button("Send");
add(Send,BorderLayout.WEST);



--------------------------------------------------------------------------------

// Modifed
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.awt.event.*;
/*
<applet code = "MessageWindow" width=400 height = 200>
</applet>
*/

public class  MessageWindow extends applet  implements ActionListener
{
Button Send;
TextArea A1,A2;

public void  init()
{
setBackground(Color.cyan);
setLayout(new BorderLayout());
add(new TextArea(" ",10,30),BorderLayout.NORTH);
add(new TextArea(" ",5,30),BorderLayout.SOUTH);

// I put this
Send = new Button("Send");
//I change dis:)
add(Send,BorderLayout.WEST);

Send.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str = ae.getActionCommand();
if(str.equals("Send"))
{
repaint();
}
else
{
repaint();
};
}
}

 
Answer #2    Answered By: Percy Morgan     Answered On: Jan 12

I generally use an inner class  when working with listeners. Otherwise
the new event  model didn't do very much to improve on the old AWT
method.
Modified:
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.awt.event.*;
/*
<applet code = "MessageWindow" width=400 height = 200>
</applet>
*/

public class MessageWindow extends Applet
{
Button Send;
TextArea A1, A2;

public void  init ()
{
setBackground (Color.cyan);
setLayout (new BorderLayout ());
add (new TextArea (" ", 10, 30), BorderLayout.NORTH);
add (new TextArea (" ", 5, 30), BorderLayout.SOUTH);

// I put this
Send = new Button ("Send");
//I change dis:)
add (Send, BorderLayout.WEST);

Send.addActionListener (new Listen ());
}


class Listen implements ActionListener
{

public void actionPerformed (ActionEvent ae)
{
repaint ();
}
}
}

 
Didn't find what you were looking for? Find more on Null Pointer exception Or get search suggestion and latest updates.




Tagged: