为什么RMI没有打印出案例选择

问题描述

我对RMI系统进行了新的测试。但是,有一个问题。它没有打印出假定的选择,而是打印出了

欢迎来到CrewCuts 您是客户还是理发师? 键入退出离开。 java.lang.NullPointerException 在CrewCutsClient.main(CrewCutsClient.java:172)

它应该一直显示到出口离开为止。稍后,用户输入将确定它将返回什么功能。取决于是客户还是理发师。

客户:

import java.rmi.Naming; 
import java.rmi.remoteexception; 
import java.net.MalformedURLException; 
import java.rmi.NotBoundException;
import java.io.*;

public class CrewCutsClient {

CrewCuts cc;
int user_type;

BufferedReader input;

public CrewCutsClient(){
    try {
        CrewCuts cc = (CrewCuts)
                Naming.lookup("rmi://localhost/CrewCuts");
        BufferedReader input =
                new BufferedReader(new InputStreamReader(system.in));
    }
    catch (MalformedURLException murle) {
        System.out.println();
        System.out.println("MalformedURLException");
        System.out.println(murle);
    }
    catch (remoteexception re) {
        System.out.println();
        System.out.println("remoteexception");
        System.out.println(re);
    }
    catch (NotBoundException nbe) {
        System.out.println();
        System.out.println("NotBoundException");
        System.out.println(nbe);
    }
}

private void booking_customer() {
        try {
            boolean repeat;
            do {
                repeat = false;
                System.out.println("Please state your day of booking (Monday,Tuesday,Wednesday,Friday,Saturday,Sunday");
                String day = input.readLine();
                System.out.println("Select your type of appointment (Hair cut/hair cleansing/hair dye");
                String service = input.readLine();
                System.out.println("Your day of appointment shall be: '" + day);
                System.out.println("Your service shall be: '" + service);
            } while (repeat);
        } catch (Exception e) {
            e.printstacktrace();
        }
}

private void logout(){
    try {
        boolean isSuccess = cc.logout(user_type);
        if (isSuccess) {
            user_type = 0;
            System.out.println("Successfully logged out");
        } else {
            System.out.println("Failed to log out");
        }
    } catch (Exception e) {
        e.printstacktrace();
    }
}

private void customer_info() {
    try {
        boolean repeat;
        do {
            repeat = false;
            System.out.println("Please enter your name");
            String custname = input.readLine();
            System.out.println("Please enter your contact number");
            String custcontact = input.readLine();
            System.out.println("Hello Mr/Mrs. '" + custname);
            System.out.println("We shall contact you through this number: '" + custcontact);
        } while (repeat);
    } catch (Exception e) {
        e.printstacktrace();
    }
}

private void barber_open() {
    try {
        boolean repeat;
        do {
            repeat = false;
            System.out.println("Until what time are you open?");
            String first = input.readLine();
            System.out.println("We will update it on the server.");
        } while (repeat);
    } catch (Exception e) {
        e.printstacktrace();
    }
}

public void customer() {
    try {
        boolean repeat;
        do {
            repeat = false;
            customer();
            System.out.println("Press 1 to make a booking.");
            System.out.println("Press 2 to insert your info.");
            System.out.println("Press 3 to exit.");
            String resp = input.readLine();
            switch (resp.toLowerCase()) {
                case "3":
                case "exit":
                    break;
                case "1":
                    booking_customer();
                    break;
                case "2":
                    customer_info();
                    break;
                default:
                    System.out.println("Please enter a valid input value");
                    repeat = true;
                    break;
            }
        } while(repeat);
    } catch (Exception e) {
        e.printstacktrace();
    }
}

public void barber() {
    try {
        boolean repeat;
        do {
            repeat = false;
            barber();
            System.out.println("Are you open today? Type Y for Yes,N for No.");
            System.out.println("Enter 'exit' to return to main menu");
            String resp = input.readLine();
            switch (resp.toLowerCase()) {
                case "0":
                case "exit":
                    break;
                case "Y":
                case "yes":
                case "Yes":
                    barber_open();
                    break;
                case "N":
                case "no":
                case "No":
                    break;
                default:
                    System.out.println("Please enter a valid input value");
                    repeat = true;
                    break;
            }
        } while(repeat);
    } catch (Exception e) {
        e.printstacktrace();
    }
}

public static void main(String[] args) {
    CrewCutsClient client = new CrewCutsClient();

    try {
        System.out.println("Welcome to CrewCuts");
        System.out.flush();
        System.out.println("Are you a Customer or Barber?");
        System.out.println("Type Exit to leave.");
        String resp = client.input.readLine();
        switch (resp.toLowerCase()) {
            case "exit":
                client.logout();
                break;
            case "customer":
                client.customer();
                break;
            case "barber":
                client.barber();
                break;
            default:
                System.out.println("Please enter either Customer or Barber.");
                break;
        }
        System.out.println("Press Enter to continue");
        String ignore_input = client.input.readLine();
    } catch (remoteexception re) {
        System.out.println();
        System.out.println("remoteexception");
        System.out.println(re);
    } catch (java.lang.ArithmeticException ae) {
        System.out.println();
        System.out.println("java.lang.ArithmeticException");
        System.out.println(ae);
    } catch (Exception e) {
        e.printstacktrace();
    }}}

服务器

import java.rmi.Naming;

public class CrewCutsServer {

public CrewCutsServer() {
 try {
   CrewCuts cc = new CrewCutsImpl();
   Naming.rebind("rmi://localhost:1099/CrewCuts",cc);
 } catch (Exception e) {
   System.out.println("Trouble: " + e);
 }
}

public static void main(String args[]) {
 new CrewCutsServer();
}}

实施

public class CrewCutsImpl extends java.rmi.server.UnicastRemoteObject implements CrewCuts {

public CrewCutsImpl() throws java.rmi.remoteexception {
  super(); 
}
public long add(long a,long b) 
throws java.rmi.remoteexception { 
return a + b; 
}
public long sub(long a,long b) 
throws java.rmi.remoteexception { 
return a - b; 
}
public long mul(long a,long b) 
throws java.rmi.remoteexception { 
return a * b; 
} 
public long div(long a,long b) 
throws java.rmi.remoteexception { 
return a / b;   
}

public boolean logout (int user_type) throws java.rmi.remoteexception{
return true;
}

public int customer(int user_type) throws java.rmi.remoteexception {
return(user_type);
}

public int barber(int user_type) throws java.rmi.remoteexception {
return(user_type);
}}

接口

public interface CrewCuts extends java.rmi.Remote {

public int customer(int user_type)
        throws java.rmi.remoteexception;

public int barber(int user_type)
        throws java.rmi.remoteexception;

public boolean logout(int user_type)
        throws java.rmi.remoteexception;

public long add(long a,long b)
    throws java.rmi.remoteexception;

public long sub(long a,long b)
    throws java.rmi.remoteexception;

public long mul(long a,long b)
    throws java.rmi.remoteexception;

public long div(long a,long b)
    throws java.rmi.remoteexception;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)