java applet problem

import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;

public class ClientApplet extends Applet
{
  public void init() {
    String host = getParameter( "localhost" );
    int port = Integer.parseInt( getParameter( "80" ) );
    setLayout( new BorderLayout() );
    add( "Center", new Client( host, port ) );
  }
 
  static public void main( String args[] ) throws Exception {

    new ClientApplet();
  }
}




import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class Client extends Panel implements Runnable
{
  // Components for the visual display of the chat windows
  private TextField tf = new TextField();
  private TextArea ta = new TextArea();

  // The socket connecting us to the server
  private Socket socket;

  // The streams we communicate to the server; these come
  // from the socket
  private DataOutputStream dout;
  private DataInputStream din;

  // Constructor
  public Client( String host, int port ) {

    // Set up the screen
    setLayout( new BorderLayout() );
    add( "North", tf );
    add( "Center", ta );

    // We want to receive messages when someone types a line
    // and hits return, using an anonymous class as
    // a callback
    tf.addActionListener( new ActionListener() {
      public void actionPerformed( ActionEvent e ) {
        processMessage( e.getActionCommand() );
      }
    } );

    // Connect to the server
    try {

      // Initiate the connection
      socket = new Socket( host, port );

      // We got a connection!  Tell the world
      System.out.println( "connected to "+socket );

      // Let's grab the streams and create DataInput/Output streams
      // from them
      din = new DataInputStream( socket.getInputStream() );
      dout = new DataOutputStream( socket.getOutputStream() );

      // Start a background thread for receiving messages
      new Thread( this ).start();
    } catch( IOException ie ) { System.out.println( ie ); }
  }

  // Gets called when the user types something
  private void processMessage( String message ) {
    try {

      // Send it to the server
      dout.writeUTF( message );

      // Clear out text input field
      tf.setText( "" );
    } catch( IOException ie ) { System.out.println( ie ); }
  }

  // Background thread runs this: show messages from other window
  public void run() {
    try {

      // Receive messages one-by-one, forever
      while (true) {

        // Get the next message
        String message = din.readUTF();

        // Print it to our text window
        ta.append( message+"\n" );
      }
    } catch( IOException ie ) { System.out.println( ie ); }
  }
}
      
 
Hi
   I'm trying to create a client for a chat server...both the files shown above which are clientapplet.java and client.java compile fine..i'm trying to run the clientapplet.java using appletviewer and it gives me the following error

java.lang.NumberFormatException: null
        at java.lang.Integer.parseInt(Integer.jav
        at java.lang.Integer.parseInt(Integer.jav
        at ClientApplet.init(ClientApplet.java:10
        at sun.applet.AppletPanel.run(AppletPanel
        at java.lang.Thread.run(Thread.java:534)

seems to be on this line

int port = Integer.parseInt( getParameter( "80" ) );

i cant figure out whats wrong though

can someone help?
      
desiboy1974Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
you don't have a parameter named '80' defined in your html
0
objectsCommented:
if you just want to hardcode it then it should be:

    String host = "localhost";
    int port = 80;
0
petmagdyCommented:
obvoiusly getParameter( "80" )  returns null

try this:

  public void init() {
    String host = getParameter( "localhost" );
    try{
    String strPort = getParameter( "80" ) ;
    System.out.println("Pot: " + strPort);
    int port = Integer.parseInt(strPort );
    }
    catch(Exception ex)
    {
       ex.printStackTrace();
    }
    setLayout( new BorderLayout() );
    add( "Center", new Client( host, port ) );
  }
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

desiboy1974Author Commented:
i tried both and it gave me the same error again??
0
desiboy1974Author Commented:
C:\j2sdk1.4.2_05\bin>appletviewer a.html
java.lang.NumberFormatException: null
        at java.lang.Integer.parseInt(Integer.jav
        at java.lang.Integer.parseInt(Integer.jav
        at ClientApplet.init(ClientApplet.java:10
        at sun.applet.AppletPanel.run(AppletPanel
        at java.lang.Thread.run(Thread.java:534)

the error i get is shown above
0
desiboy1974Author Commented:
this is what is in a.html

<html>
<body>
<applet code="ClientApplet.class" width=513 height=205>
</applet>
</body>
</html>
0
petmagdyCommented:
u didn't got what i mean, anyway, ok just try this:

  public void init() {
     setLayout( new BorderLayout() );
    add( "Center", new Client( "localhost", 80 ) );
  }

or pass the paramters from html files using <applet> tag
0
objectsCommented:
<applet code="ClientApplet.class" width=513 height=205>
<param name="80" value="80"/>
<param name="localhost" value="localhost"/>
</applet>
0
objectsCommented:
> u didn't got what i mean, anyway, ok just try this:

thats the same as what I mentioned earlier
0
petmagdyCommented:
if u want to use the html parameters try this:

1- in html:
<html>
<body>
<applet code="ClientApplet.class" width=513 height=205>
<PARAM NAME="host" VALUE="localhost">
<PARAM NAME="port" VALUE="80">

</applet>
</body>
</html>

2- in ur applet class:

  public void init() {
    String host = getParameter( "host" );
     String strPort = getParameter( "port" ) ;
    try{
        System.out.println("Pot: " + strPort);
    int port = Integer.parseInt(strPort );
    }
    catch(Exception ex)
    {
       ex.printStackTrace();
    }
    setLayout( new BorderLayout() );
    add( "Center", new Client( host, port ) );
  }


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
objectsCommented:
try this:

public class ClientApplet extends Applet
{
  public void init() {
    String host = getParameter( "host" );
    if (host==null) host = "localhost";
    int port = 80;
    try {
      port = Integer.parseInt( getParameter( "port" ) );
    } catch (Exception ex) {}
    setLayout( new BorderLayout() );
    add( "Center", new Client( host, port ) );
  }
}

with the following html:

<applet code="ClientApplet.class" width=513 height=205>
<param name="port" value="80"/>
<param name="host" value="localhost"/>
</applet>

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.