Results 1 to 5 of 5
  1. #1
    donchini is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default NullPointerException

    //Hi. I'm getting a NullPointerException on the line
    // if (CommandFromClient.equals("FORWARD")) on the server program
    //I'm looking to communicate with the server but each time I load the applet, it returns "connection with server lost" instead of "Welcome! You are now connected and can control the TANK"

    //below are the server and client programs respectively.

    // This is the server program

    // Reference: http://cegt201.bradley.edu/projects/...finalpaper.pdf

    import parport.ParallelPort; // This package is used for parallel port interfacing
    import java.net.Socket; // This package is used for JAVA networking
    import java.net.ServerSocket; // This package is used for JAVA networking
    import java.io.*; // This package is used for JAVA networking
    import java.util.Calendar; // This package is used for date & time functions

    public class teleserver // Declaring the class

    {

    // Declaring below the class variables

    // Instantiating below the parallel port at hex address 378

    public static ParallelPort lpt1 = new ParallelPort(0x378);

    public static int ConnectionPort = 2001; // Variable to hold the connection port

    public static int InitialMinute = 0; // Variable initialized for the timer thread

    public static boolean ConnectionEstablish; // Variable used for true or false for a
    // connection establishment

    public static PrintWriter SendToClient; // Variable to hold the contents used to
    // send to client

    public static BufferedReader ReceiveFromClient; // Variable to hold the contents
    // used to receive from client

    public static ServerSocket SocketOfServer; // Declaring the server socket variable
    // name

    public static Socket ConnectionSocket; // Declaring the socket variable name
    // which will hold the server socket
    // acception

    public static Calendar LogOnTimeStamp; // Declaring the calendar variable

    public static String CommandFromClient; // This variable takes on the
    // BufferedReader variable contents







    public static void main(String[] args) // Declaring the main() method

    {

    lpt1.write(0x0F); // Initialize Parallel port pins

    new HandleTenMins().start(); // Instantiating and starting the thread to handle ten
    // minute timeout

    try // adding a try & catch block as connection with client can throw an exception

    {

    SocketOfServer = new ServerSocket(ConnectionPort); // Server socket
    // instantiation

    // Printing out on screen server initialization status as below

    System.out.println("\nThe server has started and is now waiting for a client at port " + ConnectionPort);

    while (true) // Initializing infinite loop

    {

    try // adding a try & catch block as connection with client can
    // throw an exception

    {

    // Accepting below the client connection request

    ConnectionSocket = SocketOfServer.accept();
    System.out.println("hello1");
    // Taking time stamp of the connection instant

    LogOnTimeStamp = Calendar.getInstance();
    System.out.println("hello2");
    // Printing out on screen client connection status as below

    System.out.println("\nA new client is now connected " +
    ConnectionSocket.getInetAddress() + " on " +
    LogOnTimeStamp.getTime());


    System.out.println("hello3");





    // Taking the first minute of connection as below

    InitialMinute = LogOnTimeStamp.get(Calendar.MINUTE);

    // Below is approximately the ten minute timer logic ....
    System.out.println("hello4");
    InitialMinute += 11;

    if(InitialMinute >= 60)

    InitialMinute -= 60;

    // Assigning below the boolean value true for successful
    // connection
    System.out.println("hello5");
    ConnectionEstablish = true;

    // PrintWriter Instantiation as below
    System.out.println("hello6");
    SendToClient = new
    PrintWriter(ConnectionSocket.getOutputStream());

    // Sending out to client welcome message as below
    System.out.println("do u sennd");
    SendToClient.println("Welcome! You are now connected and can control the TANK");
    System.out.println("do u sennd2");
    //Thread.sleep(60000);
    System.out.println("hello7");
    SendToClient.flush(); // After sending the string, clear the
    // PrintWriter buffer

    // BufferedReader instantiation as below

    ReceiveFromClient = new BufferedReader(new
    InputStreamReader(ConnectionSocket.getInputStream( )));
    System.out.println("hello8");
    // Applying below condition that as long as the connection
    // remains valid, do the following

    while (ConnectionEstablish != false)

    {

    // Assigning below the line input from the client to
    // the CommandFromClient variable

    CommandFromClient = ReceiveFromClient.readLine();

    System.out.println("hello9"); // testing the program up to this point



    // Applying below If condition with respect to the
    // CommandFromClient variable contents

    if (CommandFromClient.equals("FORWARD"))

    {

    // Writing below to parallel port pins and
    // setting the port logic as .... FORWARD

    lpt1.write(0x00);

    // Sending below to client acknowledgement
    // of the FORWARD command

    SendToClient.println("The tank is moving....FORWARD");

    // After sending below the string, clear the
    // PrintWriter buffer

    SendToClient.flush();

    // Printing out on screen parallel port logic
    // status as below

    System.out.println("Parallel port logic....FORWARD");

    } // End of if

    // Applying below If condition with respect to the
    // CommandFromClient variable contents

    if (CommandFromClient.equals("STOP"))

    {

    // Writing below to parallel port pins and
    // setting the port logic as .... STOP

    lpt1.write(0x0F);

    // Send to client acknowledgement of the
    // STOP command as below

    SendToClient.println("The tank has....STOPPED");



    // After sending the string, clear the
    // PrintWriter buffer as below

    SendToClient.flush();

    // Printing out on screen parallel port logic
    // status as below

    System.out.println("Parallel port logic....STOP");

    } // End of if

    // Applying below If condition with respect to the
    // CommandFromClient variable contents

    if (CommandFromClient.equals("LEFT"))

    {

    // Writing below to parallel port pins and
    // setting the port logic as .... LEFT

    lpt1.write(0x20);

    // Send to client acknowledgement of the
    // LEFT command as below

    SendToClient.println("The tank is turning....LEFT");

    // After sending the string, clear the
    // PrintWriter buffer as below

    SendToClient.flush();

    // Printing out on screen parallel port logic
    // status as below

    System.out.println("Parallel port logic....LEFT");

    } // End of if








    // Applying below If condition with respect to the
    // CommandFromClient variable contents

    if (CommandFromClient.equals("RIGHT"))

    {

    // Writing below to parallel port pins and
    // setting the port logic as .... RIGHT

    lpt1.write(0x10);

    // Send to client acknowledgement of the
    // RIGHT command as below

    SendToClient.println("The tank is turning....RIGHT");

    // After sending the string, clear the
    // PrintWriter buffer as below

    SendToClient.flush();

    // Printing out on screen parallel port logic
    // status as below

    System.out.println("Parallel port logic....RIGHT");

    } // End of if

    // Applying below If condition with respect to the
    // CommandFromClient variable contents

    if (CommandFromClient.equals("REVERSE"))
    {

    // Writing below to parallel port pins and
    // setting the port logic as .... REVERSE

    lpt1.write(0x30);

    // Send to client acknowledgement of the
    // REVERSE command as below

    SendToClient.println("The tank is moving in....REVERSE");



    // After sending the string, clear the
    // PrintWriter buffer as below

    SendToClient.flush();

    // Printing out on screen parallel port logic
    // status as below

    System.out.println("Parallel port logic....REVERSE");
    System.out.println("hello20");
    } // End of if

    } // End of While loop

    } // Try ends

    // Catch exception below for abnormal disconnection or after ten
    // minute timeout disconnection

    catch (Exception e)

    { e.printStackTrace();

    // Writing below to parallel port pins and setting the port
    // logic as STOP

    lpt1.write(0x0F);

    ConnectionSocket.close(); // Close the connection with
    // client

    // Assigning below boolean value false on closing connection

    ConnectionEstablish = false;

    // Printing out on screen parallel port logic status as below

    System.out.println("Parallel port logic set to LOW");

    // Printing out on screen client disconnection status below

    System.out.println("Client Logged Out " +
    ConnectionSocket.getInetAddress());







    // Printing out on screen server ready and waiting for new
    // connection status as below

    System.out.println("\nThe server is now ready for a new client connection at port " + ConnectionPort);

    } // Catch ends

    } // End of While loop

    } // Try ends

    // Catch exception below, if any, while establishing connection with the client

    catch (Exception e)
    { // Ignore any actions to be taken

    } // Catch ends

    } // End of main()

    public static void stop() // This is the method called to close connection with the client

    {

    try // adding a try & catch block as closing connection with client can throw an
    // exception

    {

    // Writing below to parallel port pins and setting the port logic as .... STOP

    lpt1.write(0x0F);

    ConnectionSocket.close(); // Close the connection with client

    ConnectionEstablish = false; // Assigning boolean value false on closing
    // connection

    } // Try ends

    // Catch below exception, if any, while closing connection with the client

    catch(Exception e)
    { // Ignore any actions to be taken

    } // Catch ends

    } // End of stop()

    } // End of class teleserver
    // This is the class below used to handle the thread which handles the timeout after
    // approximately 10 minutes

    class HandleTenMins extends Thread

    {

    Calendar LogOnTimeStamp; // Declaring the calendar variable

    int FinalMinute; // Variable declared to hold final minute for the timer thread

    public void run() // This is the thread method which handles the threading action

    {

    while(true) // Initializing infinite loop

    {

    LogOnTimeStamp = Calendar.getInstance(); // Taking time stamp of the
    // connection instant

    // Taking below the final minute of connection

    FinalMinute = LogOnTimeStamp.get(Calendar.MINUTE);

    // Applying below condition that if the boolean value is true for the
    // ConnectionEstablish, then do the following

    if(teleserver.ConnectionEstablish)

    {

    // When the two variables are equal, do the following as below

    if (teleserver.InitialMinute == FinalMinute)

    {

    // Send to client the time up message as below

    teleserver.SendToClient.println("Your 10 minute session has expired!");

    // After sending the string, clear the PrintWriter buffer as
    // below

    teleserver.SendToClient.flush();



    // Printing out on screen the time up message as below

    System.out.println("Client's 10 minutes are up!");

    // Calling below stop() method to close the connection with
    // the client

    teleserver.stop();

    } // End of if

    } // End of if

    } // End of While loop

    } // End of run()

    } // End of class HandleTenMins












    // This is the client interface program

    // Reference: http://cegt201.bradley.edu/projects/...finalpaper.pdf

    import java.awt.*; // This package is used for graphical objects
    import java.awt.event.*; // This package is used for handling events
    import java.applet.*; // This package is used for applets
    import java.io.*; // This package is used for JAVA networking
    import java.net.*; // This package is used for JAVA networking

    // Declaring below the applet class and mentioning that it will run a thread and will perform
    // action listening events

    public class teleclient extends Applet implements Runnable, ActionListener

    {

    // Declaring below the class variables

    String Server = "192.168.1.23"; // Variable to hold the server IP

    int ConnectionPort = 2001; // Variable to hold the connection port

    PrintWriter SendToServer; // Variable to hold the contents used to send to server

    BufferedReader ReceiveFromServer; // Variable to hold the contents used to receive from
    // server

    Socket ConnectionSocket; // Decarling the socket variable name

    Thread ReceiveThread = new Thread(this); // Instantiating thread for handling server
    // feedback

    String CommandFromServer; // This variable takes on the BufferedReader variable
    // contents

    Panel greeting = new Panel(); // Instantiating a panel used to hold the greeting from the
    // server

    // Instantiating and defining below the applet title

    Label title = new Label("TANK COMMAND CENTRE", Label.CENTER);

    Panel buttons = new Panel(); // Instantiating a panel used to hold the applet buttons

    // Below are the various applet buttons being instantiated and defined ....

    Button LeftButton = new Button("LEFT");

    Button ForwardButton = new Button("FORWARD");

    Button StopButton = new Button("STOP");

    Button RightButton = new Button("RIGHT");

    Button ReverseButton = new Button("REVERSE");

    Panel informationbar = new Panel(); // Instantiating a panel used to hold the applet
    // information bar

    TextField feedback = new TextField(); // Instantiating the applet textfield which provides
    // visual update on server feedback

    public void init() // Declaring the applet initialize method

    {

    setFont(new Font("Serif",Font.BOLD,18)); // Defining the applet font and size

    greeting.setLayout(new GridLayout(1,1)); // Defining a gridlayout for applet title

    greeting.add(title); // Adding the applet title to the gridlayout

    buttons.setLayout(new BorderLayout()); // Defining a borderlayout for applet buttons

    ForwardButton.addActionListener(this); // Telling the applet to listen for the button press
    // action for this button

    buttons.add(ForwardButton, "North"); // Adding button in North direction on the applet

    LeftButton.addActionListener(this); // Telling the applet to listen for the button press
    // action for this button

    buttons.add(LeftButton, "West"); // Adding button in West direction on the applet

    StopButton.addActionListener(this); // Telling the applet to listen for the button press
    // action for this button

    buttons.add(StopButton, "Center"); // Adding button in Center direction on the applet

    RightButton.addActionListener(this); // Telling the applet to listen for the button press
    // action for this button

    buttons.add(RightButton, "East"); // Adding button in East direction on the applet


    ReverseButton.addActionListener(this); // Telling the applet to listen for the button press
    // action for this button

    buttons.add(ReverseButton, "South"); // Adding button in South direction on the applet


    informationbar.setLayout(new GridLayout(1,1)); // Defining a gridlayout for applet
    // information bar

    informationbar.add(feedback); // Adding the applet textfield on the applet

    feedback.setEditable(false); // Setting the textfield to non-editable mode

    setLayout(new BorderLayout()); // Defining a borderlayout for adding all the applet
    // panels

    add(greeting, "North"); // Adding the greeting panel in North direction on the applet

    add(buttons, "Center"); // Adding the buttons panel in the Center direction on the applet

    add(informationbar, "South"); // Adding the informationbar panel in the South direction
    // on the applet

    MakeConnection(); // Calling method to make a connection with the server

    } // End of init()

    public void MakeConnection() // This is the method used to make a connection with the server

    {

    try // adding a try & catch block as connection with server can throw an exception

    {

    ConnectionSocket = new Socket(Server, ConnectionPort); // Socket instantiation

    // Below is the PrintWriter instantiation

    SendToServer = new PrintWriter(ConnectionSocket.getOutputStream());

    // Below is the BufferedReader instantiation

    ReceiveFromServer = new BufferedReader(new
    InputStreamReader(ConnectionSocket.getInputStream( )));

    // Assigning below the line input from the server to the CommandFromServer
    // variable

    CommandFromServer = ReceiveFromServer.readLine();







    // Applying below If condition with respect to the CommandFromServer variable
    // contents

    if (CommandFromServer.equals("Welcome! You are now connected and can control the TANK"))

    {

    // Displaying below the CommandFromServer variable contents
    // in the textfield

    feedback.setText(CommandFromServer);

    ReceiveThread.start(); // Start the thread

    } // End of if

    // Applying below If condition with respect to the CommandFromServer variable
    // contents

    if (!CommandFromServer.equals("Welcome! You are now connected and cancontrol the TANK"))

    {

    ConnectionSocket.close(); // Close the connection with server

    } // End of if

    } // Try ends

    catch (Exception e) // Catch exception if server application not running

    {

    stop(); // Calling Stop() method to close the connection with the server

    // Display below the CommandFromServer variable contents in the textfield

    feedback.setText("Please make sure the server application is running on the server");

    } // Catch ends

    } // End of MakeConnection()





    public void run() // This is the thread method which handles the threading action

    {

    while(true) // Initializing infinite loop

    {

    try // adding a try & catch block as reading line inputs from server can throw an
    // exception

    {

    // Assigning below the line input from the server to the
    // CommandFromServer variable

    CommandFromServer = ReceiveFromServer.readLine();

    // Displaying below the CommandFromServer variable contents in the
    // textfield

    feedback.setText(CommandFromServer);

    // Applying below If condition with respect to the CommandFromServer
    // variable contents

    if (CommandFromServer.equals("Your 10 minute session has expired!"))

    {

    // Closing the connection with server

    ConnectionSocket.close();

    // Once connection closed then break out from the
    // infinite loop as below

    break;

    } // End of if

    } // Try ends

    catch(Exception e) // Catch exception if connection with the server lost

    {

    stop(); // Calling stop() method to close the connection with the server



    // Displaying below the CommandFromServer variable contents in the
    // textfield

    feedback.setText("Connection with the server lost....");

    break; // Once connection closed then break out from the infinite loop

    } // Catch ends

    } // End of While loop

    } // End of run()

    public void stop() // This is the method called to close connection with the server

    {

    try // adding a try & catch block as closing connection with server can throw an exception

    {

    ConnectionSocket.close(); // Close the connection with server

    } // End of try

    catch (Exception e) // Catch exception, if any, while closing connection with the server

    { // Ignore any actions to be taken

    } // Catch ends

    } // End of stop()

    // This is this the method below used for listening on the respective button being pressed by user
    // and taking the appropriate action

    public void actionPerformed(ActionEvent e)

    {

    if (e.getSource() == LeftButton) // If LeftButton pressed then do the following ....

    {

    SendToServer.println("LEFT"); // Send to the server the LEFT string

    SendToServer.flush(); // After sending the string, clear the PrintWriter
    // buffer

    } // End of if

    if (e.getSource() == ForwardButton) // If ForwardButton pressed then do the following

    {

    SendToServer.println("FORWARD"); // Send to the server the
    // FORWARD string

    SendToServer.flush(); // After sending the string, clear the PrintWriter
    // buffer

    } // End of if

    if (e.getSource() == StopButton) // If StopButton pressed then do the following ....

    {

    SendToServer.println("STOP"); // Send to the server the STOP string

    SendToServer.flush(); // After sending the string, clear the PrintWriter
    // buffer

    } // End of if

    if (e.getSource() == RightButton) // If RightButton pressed then do the following ....

    {

    SendToServer.println("RIGHT"); // Send to the server the RIGHT string

    SendToServer.flush(); // After sending the string, clear the PrintWriter
    // buffer

    } // End of if

    if (e.getSource() == ReverseButton) // If ReverseButton pressed then do the following ....

    {

    SendToServer.println("REVERSE"); // Send to the server the REVERSE
    // string

    SendToServer.flush(); // After sending the string, clear the PrintWriter
    // buffer

    } // End of if

    } // End of actionPerformed(ActionEvent e)

    } // End of class teleclient

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    wellcome :)

    please place your code into code tags first it is not clear :(

  3. #3
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Ouch. That's a heck of a lot of unformatted code to sift through. You may want to check out the forum FAQ's for instructions on using code tags so you can edit that post and help make it readable. Also, which line is causing the NPE to be thrown? Have you checked the objects used on that line to see which one is null? Often that leads to identification of the problem and discovery of a solution.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    195
    Rep Power
    5

    Default

    no..........
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

Similar Threads

  1. NullPointerException
    By GPB in forum New To Java
    Replies: 8
    Last Post: 02-21-2010, 04:05 PM
  2. NullPointerException
    By Juuno in forum New To Java
    Replies: 1
    Last Post: 02-11-2010, 06:43 PM
  3. Why do I get a NullPointerException?
    By nessa203 in forum New To Java
    Replies: 2
    Last Post: 01-12-2010, 05:02 PM
  4. Why do I get a NullPointerException?
    By nessa203 in forum New To Java
    Replies: 5
    Last Post: 01-07-2010, 02:14 PM
  5. NullPointerException
    By mensa in forum Java 2D
    Replies: 5
    Last Post: 05-04-2008, 12:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •