Page 2 of 2 FirstFirst 12
Results 21 to 23 of 23
Like Tree1Likes

Thread: Java Help Much Apreciated

  1. #21
    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

    You're welcome. The key lesson to take from this is that when you run into a NPE (NullPointerException), you must track back into your code to see why the offending variable is null. In this case the variable was a method parameter, and so you must track back to see where the method was called and what was passed into it. In your case it was a blatant issue of passing null directly into the method, but again that's less important that understanding the process of solving this in the future.

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

    Default Re: Java Help Much Apreciated

    I have adapted the code in the protocol class, realised the order of execution was incorrect as well as what was previously mentioned and things seem to work to a degree, my final issue is I must have "Another Y or N" after each description is shown giving the client an option of choosing another option or ending the program. Any ideas on how this could be implemented.

    PROTOCOL CLASS

    Java Code:
    package Version_1;
    
    import java.net.*;
    import java.io.*;
    
    public class DownloadProtocol {
    
        private static final int WAITING = 1;
        private static final int SENTTERMS = 2;
        private static final int OPTIONSSENT = 3;
        private static final int SENTDESCRIPTIONS = 4; 
        private int state = WAITING;
    
        public String processInput(String theInput) {
            String theOutput = null;
    
            switch (state) {
                case 1: {
                    theOutput = "Terms of reference. Do you accept? Y or N";
                    state = SENTTERMS;
                    break;
                }
                case 2: {
                    if (theInput.equalsIgnoreCase("Y")) {
                        theOutput = "1. computer program 2. picture 3. e-book";
                    } 
                    state = OPTIONSSENT;
                    break;
                }
                case 3: {
                    if (theInput.equalsIgnoreCase("1")) {
                        theOutput = "The program displays a message... Another Y or N?";
                    } else if (theInput.equalsIgnoreCase("2")) {
                        theOutput = "Picture shows Another Y or N?";
                    } else if (theInput.equalsIgnoreCase("3")) {
                        theOutput = "ebook Another Y or N?";
                    }
                    state = SENTDESCRIPTIONS;
                    if (theInput.equalsIgnoreCase("N")) {
                        theOutput = "Connection closed";
                        break;
                    }
                }
            }
            return theOutput;
        }
    }

  3. #23
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Java Help Much Apreciated

    A comment on your code. Why not use the final int variables in the case statement instead of the numbers?
    case 1:
    vs
    case WAITING:

    This would make your code much easier to read and understand.

Page 2 of 2 FirstFirst 12

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
  •