简体   繁体   中英

Passing value to another class in java

I have a problem in passing the value of input of user to another class. This is a webservice that's why i don't know how i will fix it and this is my first time to encounter wevservice. This is my code in getting the input of user.

PlateNumberCheck.java

    //PATH FOR CHECKING PLATE NUMBER

@Path("platecheck")  //for the url

public class PlateNumberCheck {
    public String anss;
@GET 
//To get the full url : http://Ipaddress:portnumber/@path/@getPath 
@Path("/check")
@Produces(MediaType.APPLICATION_JSON) 
//Produces is for the response of JSON.

public String check(@QueryParam("taxi_plate_no") String taxi_plate_no){
    String sagot = "";
    anss = taxi_plate_no;
    if(checkInput(taxi_plate_no)){
        display();
        sagot = JsonConstruction.JSONResponse("checked", true);
    } else{ 
        sagot = JsonConstruction.JSONResponse("checked", false, "Not in the database");
    }
    return sagot;
}

private boolean checkInput (String taxi_plate_no){
    System.out.println("Check Input");
    boolean output = false;
    if(JsonConstruction.isNotNull(taxi_plate_no)){
        try{
            output = DatabaseConnection.checkPlate(taxi_plate_no);
        } catch (Exception e){
            output = false;
        }
    } else{
        output = false;
    }

    return output;
}
public void display(){
    System.out.println(anss);

}
}

I think this class works. because as you can see i use the method first before it success and the method contains system.out.print which prints the input in console. This is my second class which i want to pass the input to this class.

DisplayTaxiDetails.java

@Path("/displays")
public class DisplayTaxiDetails {
    String plates;
    @GET
    @Path("taxidetails")
    @Produces(MediaType.APPLICATION_JSON)

    public String taxidetails (){
        String taxidetails = null;
        ArrayList<Objects> taxiDetailsList = new ArrayList<Objects>();

        try{
            PlateNumberCheck plate = new PlateNumberCheck();
            plates = plate.anss;
            taxiDetailsList = new ArrayConnection().getTaxiDetails(plates);
            System.out.println(plates);
            Gson gson = new Gson();
            taxidetails = gson.toJson(taxiDetailsList);
        } catch (Exception e){
            e.printStackTrace();
        } 
        return taxidetails;

        }

    }

As you can see i call the first class and access the anss which is the string of that class where the input was stored. but when i access it and try to pass value it doesn't work. I use again system.out.print to see if it prints the data but it always shows null which i think the value from first class is not passed to the variable of this second class.

You need to understand the basics of java runtime environment. That'll explain how your objects bind with each other and can interact with each other. In your case, you are creating a new instance of PlateNumberCheck every time your method in second class is getting called.

PlateNumberCheck plate = new PlateNumberCheck();

For that instance, you don't have any value set in the anss field (by the way, this is also not correct way, but that's for later) as this instance has just got created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM