Page 1 of 2 12 LastLast
Results 1 to 20 of 23
Like Tree1Likes

Thread: Java Help Much Apreciated

  1. #1
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default Java Help Much Apreciated

    I'm making a Java download centre, the client can view a description of 3 items as provided by the server. I have a protocol to control what happens. The code however, won't compile properly. Here is what I've done so far. I believe the problem lies in the protocol program.

    CLIENT

    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class Client {
    
        public static void main(String[] args) throws IOException {
    
            Socket dcSocket = null;
            PrintWriter out = null;
            BufferedReader in = null;
    
            try {
                dcSocket = new Socket("localhost", 3330);
                out = new PrintWriter(dcSocket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(dcSocket.getInputStream()));
            } catch (UnknownHostException e) {
                System.err.println("Don't know about host");
                System.exit(1);
            } catch (IOException e) {
                System.err.println("Could not get IO for connection");
                System.exit(1);
            }
    
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
            String fromServer;
            String fromUser;
    
            while ((fromServer = in.readLine()) != null) {
                System.out.println("Server: " + fromServer);
                if (fromServer.equals("Bye")) {
                    break;
                }
    
                fromUser = stdIn.readLine();
                if (fromUser != null) {
                    System.out.println("Client: " + fromUser);
                    out.println(fromUser);
                }
    
            }
            out.close();
            in.close();
            stdIn.close();
            dcSocket.close();
        }
    }
    SERVER

    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class Server {
    
        public static void main(String[] args) throws IOException {
    
            ServerSocket serverSocket = null;
            try {
                serverSocket = new ServerSocket(3330); // Creates new ServerSocket object to listen on a specific port 
            } catch (IOException e) {
                System.err.println("Message 1");
                System.exit(1);
            }
    
            Socket clientSocket = null;
            try {
                clientSocket = serverSocket.accept(); // Accepts connection from a client 
            } catch (IOException e) {
                System.out.println("Message 2");
                System.exit(-1);
            }
    
            // Opens readers and writers on socket input and output stream
            PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            String inputLine, outputLine;
    
            // Begin conversation with client program, creates protocol
            DownloadProtocol dp = new DownloadProtocol();
            outputLine = dp.processInput(null);
            out.println(outputLine);
    
            // Server reads and writes to socket 
            while ((inputLine = in.readLine()) != null) {
                outputLine = dp.processInput(inputLine);
                out.println(outputLine);
                if (outputLine.equals("N")) {
                    break;
                }
            }
            
            out.close();
            in.close(); 
            clientSocket.close();
            serverSocket.close();
        }
    }
    PROTOCOL

    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class DownloadProtocol {
    
        private int state = WAITING;
        private static final int WAITING = 1;
        private static final int REPLY = 2;
        private static final int CHOICE = 3;
    
        public String processInput(String theInput) {
            String theOutput = null;
    
            switch (state) {
                case 1: {
                    theOutput = "Terms of reference. Do you accept? Y or N";
                    if (theInput.equalsIgnoreCase("Y")) {
                        state = REPLY;
                    }
                }
                case 2: {
                    theOutput = "1. computer program 2. picture 3. e-book";
                    state = CHOICE;
                    int i = Integer.parseInt(theInput);
                    switch (i) {
                        case 1: {
                            theOutput = "The program displays a message";
                            break;
                        }
                        case 2: {
                            theOutput = "The book is about";
                            break;
                        }
                        case 3: {
                            theOutput = "The picture shows";
                            break;
                        }
                        default: {
                            theOutput = "Invalid choice";
                            break;
                        }
                    }
    
                }
            }
            return theOutput;
        }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Java Help Much Apreciated

    The code however, won't compile properly.
    If the code doesn't compile and you can't understand the compiler message, post it.

  3. #3
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default Re: Java Help Much Apreciated

    Ah yes my mistake...

    run:
    Exception in thread "main" java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream. java:168)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.j ava:264)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.ja va:306)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:1 58)
    at java.io.InputStreamReader.read(InputStreamReader.j ava:167)
    at java.io.BufferedReader.fill(BufferedReader.java:13 6)
    at java.io.BufferedReader.readLine(BufferedReader.jav a:299)
    at java.io.BufferedReader.readLine(BufferedReader.jav a:362)
    at Version_1.Client.main(Client.java:38)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

    run:
    Exception in thread "main" java.lang.NullPointerException
    at Version_1.DownloadProtocol.processInput(DownloadPr otocol.java:25)
    at Version_1.Server.main(Server.java:39)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 4 seconds)

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,584
    Rep Power
    25

    Default Re: Java Help Much Apreciated

    lang.NullPointerException
    at Version_1.DownloadProtocol.processInput( DownloadProtocol.java:25)
    at Version_1.Server.main(Server.java:39)
    There is a variable with a null value at line 25. Check the code to see what variable is null and why.
    The processInput method was called from line 39.

    BTW That is a runtime error NOT a compiler error.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Java Help Much Apreciated

    Sorry, Norm.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,584
    Rep Power
    25

    Default Re: Java Help Much Apreciated

    How are things in the new year over there in kiwi land?
    You're sure getting a lot of earthquakes down south.

  7. #7
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default Re: Java Help Much Apreciated

    There's nothing null it's
    Java Code:
    if (theInput.equalsIgnoreCase("Y"))
    when the client enteres Y the three options are displayed 1. computer program 2. etc once the client enteres a number in relation to one of these the description is given (case statement).

    On line 39
    Java Code:
    outputLine = dp.processInput(null);
    I'm not entirley sure what this would need to be set to.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Java Help Much Apreciated

    There's nothing null
    Well the runtime won't lie! Something must be null, and the only candidate is theInput. Print its value to the console and check.
    Fubarable likes this.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default (offtopic)

    @Norm: Having a quiet new year. Well, it's quiet this far south. I just checked and we get 14K earthquakes a year, but rarely with loss of life. ChCh is/was certainly the biggest in living memory. I hope things are well in your part of the world and you see the new year in well.

  10. #10
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default Re: (offtopic)

    I'm still stumped on this been at it hours, can't even work out if any-things null, sure it must be something very basic I'm missing. This is only my first year doing Java so I may sound dumb at times.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,584
    Rep Power
    25

    Default Re: Java Help Much Apreciated

    Add a println in front of the statement where the NullPointerException occurs and print out ALL of the variables used in the statement with the error. That will show you which variable is null.

  12. #12
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default Re: Java Help Much Apreciated

    There is no NullPointerException in any of the code.

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,584
    Rep Power
    25

    Default Re: Java Help Much Apreciated

    Good.
    Can you explain what your problem is now?

  14. #14
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default Re: Java Help Much Apreciated

    That says it all... the thought never crossed my mind. I don't no anything about these, could you advise where these would need to be placed.

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,584
    Rep Power
    25

    Default Re: Java Help Much Apreciated

    advise where these would need to be placed.
    What are the "these"?
    Where is the "place"?

    Are you asking how to mark this thread as solved?

  16. #16
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default Re: Java Help Much Apreciated

    I've never come across NullPointerException therefore, I'm a little unsure which part of my code these should be placed.

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,584
    Rep Power
    25

    Default Re: Java Help Much Apreciated

    Please post the full text of the error message. The source code line number is given in the error message.

  18. #18
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

    Default Re: Java Help Much Apreciated

    run:
    Exception in thread "main" java.lang.NullPointerException
    at Version_1.DownloadProtocol.processInput(DownloadPr otocol.java:25)
    at Version_1.Server.main(Server.java:39)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 3 seconds)

    Java Code:
                    if (theInput.equalsIgnoreCase("Y")) {

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

    Default Re: Java Help Much Apreciated

    It's just as pbrockway stated above -- theInput is null. He's a very smart guy, and I'd believe what he tells you if I were you.

    Backtracking, you call the processInput method by passing null into the theInput parameter, so it should be no surprise that it's null and that your code is having this error. Solution: don't call this method with null.
    Last edited by Fubarable; 01-01-2012 at 02:48 PM.

  20. #20
    Join Date
    Feb 2011
    Posts
    16
    Rep Power
    0

Page 1 of 2 12 LastLast

Similar Threads

  1. Problem! Help Apreciated
    By danielinthebed in forum Networking
    Replies: 1
    Last Post: 02-20-2011, 03:00 PM

Posting Permissions

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