Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Applet Error

  1. #1
    Junior Member
    Join Date
    Dec 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Applet Error

    I'm making this simple chatserver/chatclient gui in java using an applet instead of JFrame. The problem I'm having is that I set the correct run configuration but error comes up when client coonects and when i got to that line the error is on iI don't know what to do. Any help is much appriciated.

    ChatClient:

    public class ChatClient extends Applet {
    	private Socket socket = null;
    	private DataInputStream console = null;
    	private DataOutputStream streamOut = null;
    	private ChatClientThread client = null;
    	private TextArea display = new TextArea();
    	private TextField input = new TextField();
    	private Button send = new Button("Send"), connect = new Button("Connect"), quit = new Button("Bye");
    	private String serverName = "localhost";
    	private int serverPort = 8080;
     
    	public void init() {
    		Panel keys = new Panel();
    		keys.setLayout(new GridLayout(1, 2));
    		keys.add(quit);
    		keys.add(connect);
    		Panel south = new Panel();
    		south.setLayout(new BorderLayout());
    		south.add("West", keys);
    		south.add("Center", input);
    		south.add("East", send);
    		Label title = new Label("Simple Chat Client Applet", Label.CENTER);
    		title.setFont(new Font("Helvetica", Font.BOLD, 14));
    		setLayout(new BorderLayout());
    		add("North", title);
    		add("Center", display);
    		add("South", south);
    		quit.disable();
    		send.disable();
    		getParameters();
    		display.append(str.toString());
    	}
     
    	public boolean action(Event e, Object o) {
    		if (e.target == quit) {
    			input.setText(".bye");
    			send();
    			quit.disable();
    			send.disable();
    			connect.enable();
    		} else if (e.target == connect) {
    			connect(serverName, serverPort);
    		} else if (e.target == send) {
    			send();
    			input.requestFocus();
    		}
    		return true;
    	}
     
    	public void connect(String serverName, int serverPort) {
    		println("Establishing connection. Please wait ...");
    		try {
    			socket = new Socket(serverName, serverPort);
    			println("Connected: " + socket);
    			open();
    			send.enable();
    			connect.disable();
    			quit.enable();
    		} catch (UnknownHostException uhe) {
    			println("Host unknown: " + uhe.getMessage());
    		} catch (IOException ioe) {
    			println("Unexpected exception: " + ioe.getMessage());
    		}
    	}
     
    	private void send() {
    		try {
    			streamOut.writeUTF(input.getText());
    			streamOut.flush();
    			input.setText("");
    		} catch (IOException ioe) {
    			println("Sending error: " + ioe.getMessage());
    			close();
    		}
    	}
     
    	public void handle(String msg) {
    		if (msg.equals(".bye")) {
    			println("Good bye. Press RETURN to exit ...");
    			close();
    		} else
    			println(msg);
    	}
     
    	public void open() {
    		try {
    			streamOut = new DataOutputStream(socket.getOutputStream());
    			client = new ChatClientThread(this, socket);
    		} catch (IOException ioe) {
    			println("Error opening output stream: " + ioe);
    		}
    	}
     
    	public void close() {
    		try {
    			if (streamOut != null)
    				streamOut.close();
    			if (socket != null)
    				socket.close();
    		} catch (IOException ioe) {
    			println("Error closing ...");
    		}
    		client.close();
    		client.stop();
    	}
     
    	private void println(String msg) {
    		display.appendText(msg + "\n");
    	}
     
    	public void getParameters() {
    		serverName = getParameter("host");
    		serverPort = Integer.parseInt(getParameter("port"));
    	}
    }


    ChatClientThread:

    public class ChatClientThread extends Thread {
    	private Socket socket = null;
    	private ChatClient client = null;
    	private DataInputStream streamIn = null;
     
    	public ChatClientThread(ChatClient _client, Socket _socket) {
    		client = _client;
    		socket = _socket;
    		open();
    		start();
    	}
     
    	public void open() {
    		try {
    			streamIn = new DataInputStream(socket.getInputStream());
    		} catch (IOException ioe) {
    			System.out.println("Error getting input stream: " + ioe);
    			client.stop();
    		}
    	}
     
    	public void close() {
    		try {
    			if (streamIn != null)
    				streamIn.close();
    		} catch (IOException ioe) {
    			System.out.println("Error closing input stream: " + ioe);
    		}
    	}
     
    	public void run() {
    		while (true) {
    			try {
    				client.handle(streamIn.readUTF());
    			} catch (IOException ioe) {
    				System.out.println("Listening error: " + ioe.getMessage());
    				client.stop();
    			}
    		}
    	}
    }

    ChatServerThread:

    public class ChatServerThread extends Thread {
    	private ChatServer server = null;
    	private Socket socket = null;
    	private int ID = -1;
    	private DataInputStream streamIn = null;
    	private DataOutputStream streamOut = null;
     
    	public ChatServerThread(ChatServer _server, Socket _socket) {
    		super();
    		server = _server;
    		socket = _socket;
    		ID = socket.getPort();
    	}
     
    	public void send(String msg) {
    		try {
    			streamOut.writeUTF(msg);
    			streamOut.flush();
    		} catch (IOException ioe) {
    			System.out.println(ID + " ERROR sending: " + ioe.getMessage());
    			server.remove(ID);
    			stop();
    		}
    	}
     
    	public int getID() {
    		return ID;
    	}
     
    	public void run() {
    		System.out.println("Server Thread " + ID + " running.");
    		while (true) {
    			try {
    				server.handle(ID, streamIn.readUTF());
    			} catch (IOException ioe) {
    				System.out.println(ID + " ERROR reading: " + ioe.getMessage());
    				server.remove(ID);
    				stop();
    			}
    		}
    	}
     
    	public void open() throws IOException {
    		streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
    		streamOut = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
    	}
     
    	public void close() throws IOException {
    		if (socket != null)
    			socket.close();
    		if (streamIn != null)
    			streamIn.close();
    		if (streamOut != null)
    			streamOut.close();
    	}
    }


    ChatServer:

    public class ChatServer implements Runnable {
    	private ChatServerThread clients[] = new ChatServerThread[50];
    	private ServerSocket server = null;
    	private Thread thread = null;
    	private int clientCount = 0;
     
    	public ChatServer(int port) {
    		try {
    			System.out.println("Binding to port " + port + ", please wait  ...");
    			server = new ServerSocket(port);
    			System.out.println("Server started: " + server);
    			start();
    		} catch (IOException ioe) {
    			System.out.println("Can not bind to port " + port + ": " + ioe.getMessage());
    		}
    	}
     
    	public void run() {
    		while (thread != null) {
    			try {
    				System.out.println("Waiting for a client ...");
    				addThread(server.accept());
    			} catch (IOException ioe) {
    				System.out.println("Server accept error: " + ioe);
    				stop();
    			}
    		}
    	}
     
    	public void start() {
    		if (thread == null) {
    			thread = new Thread(this);
    			thread.start();
    		}
    	}
     
    	public void stop() {
    		if (thread != null) {
    			thread.stop();
    			thread = null;
    		}
    	}
     
    	private int findClient(int ID) {
    		for (int i = 0; i < clientCount; i++)
    			if (clients[i].getID() == ID)
    				return i;
    		return -1;
    	}
     
    	public synchronized void handle(int ID, String input) {
    		if (input.equals(".bye")) {
    			clients[findClient(ID)].send(".bye");
    			remove(ID);
    		} else
    			for (int i = 0; i < clientCount; i++)
    				clients[i].send(ID + ": " + input);
    	}
     
    	public synchronized void remove(int ID) {
    		int pos = findClient(ID);
    		if (pos >= 0) {
    			ChatServerThread toTerminate = clients[pos];
    			System.out.println("Removing client thread " + ID + " at " + pos);
    			if (pos < clientCount - 1)
    				for (int i = pos + 1; i < clientCount; i++)
    					clients[i - 1] = clients[i];
    			clientCount--;
    			try {
    				toTerminate.close();
    			} catch (IOException ioe) {
    				System.out.println("Error closing thread: " + ioe);
    			}
    			toTerminate.stop();
    		}
    	}
     
    	private void addThread(Socket socket) {
    		if (clientCount < clients.length) {
    			System.out.println("Client accepted: " + socket);
    			clients[clientCount] = new ChatServerThread(this, socket);
    			try {
    				clients[clientCount].open();
    				clients[clientCount].start();
    				clientCount++;
    			} catch (IOException ioe) {
    				System.out.println("Error opening thread: " + ioe);
    			}
    		} else
    			System.out.println("Client refused: maximum " + clients.length + " reached.");
    	}
     
    	public static void main(String args[]) {
    		ChatServer server = null;
    		if (args.length != 1)
    			System.out.println("Usage: java ChatServer port");
    		else
    			server = new ChatServer(Integer.parseInt(args[0]));
    	}
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Applet Error

    error comes up
    Please copy the full text of the error message and paste it here. It has important info about the error.

    How are you executing the program?

    Note: It would be helpful if the posted code had all the needed import statements.

    Also posted at: https://coderanch.com/t/703639/java/Applet-Error
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Applet Error

    eror.JPG

    The errors are in the image or here:

    java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:542)
    at java.lang.Integer.parseInt(Integer.java:615)
    at chat.ChatClient.getParameters(ChatClient.java:129)
    at chat.ChatClient.init(ChatClient.java:42)
    at sun.applet.AppletPanel.run(AppletPanel.java:434)
    at java.lang.Thread.run(Thread.java:748)

  4. #4
    Junior Member
    Join Date
    Dec 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Applet Error

    The imports are:

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

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Applet Error

    java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:542)
    at java.lang.Integer.parseInt(Integer.java:615)
    at chat.ChatClient.getParameters(ChatClient.java:129)
    at chat.ChatClient.init(ChatClient.java:42)
    The code at line 129 calls the getParameter method which returns a null value. That null value is passed to the parseInt method which throws the exception.
    Make sure that the value passed to the parseInt method is not null. Use an if test to check if the value is null before using the parse method.

    How are you executing the applet code? Where are the parameters defined?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Applet Error: InvocationTargerException
    By NorrinGalan in forum Java Applets
    Replies: 3
    Last Post: May 9th, 2014, 05:45 PM
  2. Replies: 4
    Last Post: August 16th, 2013, 03:25 PM
  3. error in print image onto applet
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 20th, 2012, 08:17 PM
  4. Problem Applet Error
    By mohsendeveloper in forum Java SE APIs
    Replies: 24
    Last Post: January 19th, 2012, 04:00 PM
  5. Error while running Applet
    By rameshiit19 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: August 1st, 2011, 04:02 AM

Tags for this Thread